Skip to content

Commit

Permalink
#3 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Oct 28, 2024
1 parent 7f36410 commit b9b8d93
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/main/java/ru/olegcherednik/zip4jvm/crypto/Encrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
public interface Encrypt {

void encrypt(byte[] buf, int offs, int len);

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public CentralDirectory.FileHeader getFileHeader(String entryName) throws IOExce
.findFirst().orElseThrow(() -> new EntryNotFoundException(entryName));
}

private BlockModel createModel() throws IOException {
public BlockModel createModel() throws IOException {
BlockZipModelReader reader = new BlockZipModelReader(srcZip,
settings.getCustomizeCharset(),
settings.getPasswordProvider());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ru/olegcherednik/zip4jvm/engine/ZipEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ private void add(ZipEntry zipEntry) {
if (fileNameWriter.containsKey(zipEntry.getFileName()))
throw new EntryDuplicationException(zipEntry.getFileName());

fileNameWriter.put(zipEntry.getFileName(), new ZipEntryWriter(zipEntry, tempZipModel));
tempZipModel.addEntry(zipEntry);
fileNameWriter.put(zipEntry.getFileName(), new ZipEntryWriter(zipEntry));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ protected EntryMetadataOutputStream(ZipEntry zipEntry, DataOutput out) {
this.out = out;
}

protected void writeLocalFileHeader() throws IOException {
public final void writeLocalFileHeader() throws IOException {
zipEntry.setLocalFileHeaderRelativeOffs(out.getRelativeOffs());
LocalFileHeader localFileHeader = new LocalFileHeaderBuilder(zipEntry).build();
new LocalFileHeaderWriter(localFileHeader).write(out);
out.mark(COMPRESSED_DATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import ru.olegcherednik.zip4jvm.io.out.data.DecoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.DecoderDataOutputDecorator;
import ru.olegcherednik.zip4jvm.model.CompressionMethod;
import ru.olegcherednik.zip4jvm.model.ZipModel;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import java.io.IOException;
Expand All @@ -36,19 +35,7 @@ public abstract class EntryOutputStream extends EntryMetadataOutputStream {

protected final DecoderDataOutput out;

public static EntryOutputStream create(ZipEntry zipEntry, ZipModel zipModel, DataOutput out) throws IOException {
EntryOutputStream os = createOutputStream(zipEntry, out);

// TODO move it to the separate method
zipModel.addEntry(zipEntry);
zipEntry.setLocalFileHeaderRelativeOffs(out.getRelativeOffs());

os.writeLocalFileHeader();
os.writeEncryptionHeader();
return os;
}

private static EntryOutputStream createOutputStream(ZipEntry zipEntry, DataOutput out) throws IOException {
public static EntryOutputStream create(ZipEntry zipEntry, DataOutput out) throws IOException {
CompressionMethod compressionMethod = zipEntry.getCompressionMethod();
zipEntry.setDiskNo(out.getDiskNo());

Expand All @@ -71,7 +58,7 @@ protected EntryOutputStream(ZipEntry zipEntry, DataOutput out) {
this.out = new DecoderDataOutputDecorator(out, zipEntry.createEncoder());
}

private void writeEncryptionHeader() throws IOException {
public final void writeEncryptionHeader() throws IOException {
out.writeEncryptionHeader();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.entry.EntryOutputStream;
import ru.olegcherednik.zip4jvm.model.ZipModel;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.ZipUtils;
import ru.olegcherednik.zip4jvm.utils.function.Writer;
Expand All @@ -37,11 +36,14 @@
public final class ZipEntryWriter implements Writer {

private final ZipEntry zipEntry;
private final ZipModel tempZipModel;

@Override
public void write(DataOutput out) throws IOException {
ZipUtils.copyLarge(zipEntry.getInputStream(), EntryOutputStream.create(zipEntry, tempZipModel, out));
EntryOutputStream eos = EntryOutputStream.create(zipEntry, out);

eos.writeLocalFileHeader();
eos.writeEncryptionHeader();
ZipUtils.copyLarge(zipEntry.getInputStream(), eos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public final class BlockModel {
private final Block endCentralDirectoryBlock;
private final Zip64Block zip64Block;
private final BaseCentralDirectoryBlock centralDirectoryBlock;

private final Map<String, ZipEntryBlock> fileNameZipEntryBlock;

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.IOException;

/**
* This is an abstraction, that knows how to write itself to the given {@link DataOutput}.
*
* @author Oleg Cherednik
* @since 15.09.2019
*/
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/ru/olegcherednik/zip4jvm/TestDataAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ public final class TestDataAssert {
fileSuzukiAssert.accept(dir.regularFile(fileNameSuzuki));
};

public static final long fileBentleySize = 1_395_362;

public static final Consumer<IRegularFileAssert<?>> fileBentleyAssert =
file -> file.exists().hasSize(1_395_362).isImage().isContentEqualTo(fileBentley);
file -> file.exists().hasSize(fileBentleySize).isImage().isContentEqualTo(fileBentley);
public static final Consumer<IRegularFileAssert<?>> fileFerrariAssert =
file -> file.exists().hasSize(320_894).isImage().isContentEqualTo(fileFerrari);
public static final Consumer<IRegularFileAssert<?>> fileWiesmannAssert =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm;

import ru.olegcherednik.zip4jvm.engine.InfoEngine;
import ru.olegcherednik.zip4jvm.model.Compression;
import ru.olegcherednik.zip4jvm.model.CompressionMethod;
import ru.olegcherednik.zip4jvm.model.LocalFileHeader;
import ru.olegcherednik.zip4jvm.model.block.BlockModel;
import ru.olegcherednik.zip4jvm.model.block.ZipEntryBlock;
import ru.olegcherednik.zip4jvm.model.settings.ZipInfoSettings;
import ru.olegcherednik.zip4jvm.model.settings.ZipSettings;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;

import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static ru.olegcherednik.zip4jvm.TestData.fileBentley;
import static ru.olegcherednik.zip4jvm.TestData.fileNameBentley;
import static ru.olegcherednik.zip4jvm.TestDataAssert.fileBentleyAssert;
import static ru.olegcherednik.zip4jvm.TestDataAssert.fileBentleySize;
import static ru.olegcherednik.zip4jvm.assertj.Zip4jvmAssertions.assertThatZipFile;

/**
* @author Oleg Cherednik
* @since 27.10.2024
*/
@Test
@SuppressWarnings("FieldNamingConvention")
public class ZipCompressionOptimizationTest {

private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(ZipCompressionOptimizationTest.class);

public void shouldNotCreateDataDescriptionWhenStoreCompression() throws IOException {
Path parent = Zip4jvmSuite.subDirNameAsMethodName(rootDir);
Path zip = parent.resolve("src.zip");
ZipIt.zip(zip).settings(ZipSettings.of(Compression.STORE)).add(fileBentley);

InfoEngine infoEngine = new InfoEngine(SrcZip.of(zip), ZipInfoSettings.builder().readEntries(true).build());
BlockModel blockModel = infoEngine.createModel();

ZipEntryBlock entryBlock = blockModel.getZipEntryBlock(fileNameBentley);
assertThat(entryBlock).isNotNull();
// assertThat(entryBlock.getDataDescriptor()).isNull();

// LocalFileHeader localFileHeader = entryBlock.getLocalFileHeader();
// assertThat(localFileHeader).isNotNull();
// assertThat(localFileHeader.getCompressionMethod()).isSameAs(CompressionMethod.STORE);
// assertThat(localFileHeader.getGeneralPurposeFlag().isDataDescriptorAvailable()).isTrue();
// assertThat(localFileHeader.getCrc32()).isEqualTo(1903786344L);
// assertThat(localFileHeader.getCompressedSize()).isEqualTo(fileBentleySize);
// assertThat(localFileHeader.getUncompressedSize()).isEqualTo(fileBentleySize);

assertThatZipFile(zip).regularFile(fileNameBentley).matches(fileBentleyAssert);
}

}

0 comments on commit b9b8d93

Please sign in to comment.