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 29, 2024
1 parent ffccf75 commit c57e515
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ private void endCompression() throws IOException {

@Override
public void write(final byte[] buf, int offs, final int len) throws IOException {
System.out.println(Bzip2OutputStream.class.getSimpleName() + ".write()");
if (offs < 0)
throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
if (len < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final void write(int b) throws IOException {

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
System.out.println(EncryptedOutputStream.class.getSimpleName() + ".write()");
delegate.write(buf, offs, len);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class PayloadCalculationOutputStream extends OutputStream {

private final ZipEntry zipEntry;
private final OutputStream out;
private final OutputStream delegate;
private final Checksum checksum = new PureJavaCrc32();

private long uncompressedSize;
Expand All @@ -29,26 +29,26 @@ public class PayloadCalculationOutputStream extends OutputStream {
public final void write(int b) throws IOException {
checksum.update(b);
uncompressedSize++;
out.write(b);
delegate.write(b);
}

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
checksum.update(buf, offs, len);
uncompressedSize += Math.max(0, len);
out.write(buf, offs, len);
delegate.write(buf, offs, len);
}

@Override
public void close() throws IOException {
zipEntry.setChecksum(checksum.getValue());
zipEntry.setUncompressedSize(uncompressedSize);
out.close();
delegate.close();
}

@Override
public String toString() {
return out.toString();
return delegate.toString();
}

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

import ru.olegcherednik.zip4jvm.io.bzip2.Bzip2OutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.entry.EncryptedOutputStream;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;

import java.io.IOException;
Expand All @@ -38,6 +39,7 @@ final class Bzip2EntryOutputStream extends EncryptedEntryOutputStream {

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
System.out.println(Bzip2EntryOutputStream.class.getSimpleName() + ".write()");
bzip2.write(buf, offs, len);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ public final class ZipEntryWriter implements Writer {

@Override
public void write(DataOutput out) throws IOException {
// 1. compression
// 2. encryption
EncoderDataOutput encoderDataOutput = new EncoderDataOutput(zipEntry.createEncoder(), out);
EncryptedEntryOutputStream eeos = EncryptedEntryOutputStream.create(zipEntry, encoderDataOutput);
EncryptedOutputStream eos = new EncryptedOutputStream(encoderDataOutput, eeos);

zipEntry.setDiskNo(out.getDiskNo());

/*
The series of [local file header][encryption header]
[file data][data descriptor]
*/

new LocalFileHeaderOut().write(zipEntry, out);
out.mark(COMPRESSED_DATA);
encoderDataOutput.writeEncryptionHeader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
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.TestData.fileNameOlegCherednik;
import static ru.olegcherednik.zip4jvm.TestData.fileOlegCherednik;
import static ru.olegcherednik.zip4jvm.TestDataAssert.fileBentleyAssert;
import static ru.olegcherednik.zip4jvm.TestDataAssert.fileBentleySize;
import static ru.olegcherednik.zip4jvm.assertj.Zip4jvmAssertions.assertThatZipFile;
Expand All @@ -53,12 +55,12 @@ public class ZipCompressionOptimizationTest {
public void shouldNotCreateDataDescriptionWhenStoreCompression() throws IOException {
Path parent = Zip4jvmSuite.subDirNameAsMethodName(rootDir);
Path zip = parent.resolve("src.zip");
ZipIt.zip(zip).settings(ZipSettings.of(Compression.BZIP2)).add(fileBentley);
ZipIt.zip(zip).settings(ZipSettings.of(Compression.BZIP2)).add(fileOlegCherednik);

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

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

Expand All @@ -70,7 +72,7 @@ public void shouldNotCreateDataDescriptionWhenStoreCompression() throws IOExcept
// assertThat(localFileHeader.getCompressedSize()).isEqualTo(fileBentleySize);
// assertThat(localFileHeader.getUncompressedSize()).isEqualTo(fileBentleySize);

assertThatZipFile(zip).regularFile(fileNameBentley).matches(fileBentleyAssert);
assertThatZipFile(zip).regularFile(fileNameOlegCherednik).hasContent("Oleg Cherednik Олег Чередник");//.matches(fileBentleyAssert);
}

}

0 comments on commit c57e515

Please sign in to comment.