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 9443a75 commit 9e6b55e
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ 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 @@ -64,6 +64,7 @@ protected void writeInternal(byte[] buf, int offs, int len) throws IOException {

@Override
public void close() throws IOException {
System.out.println(EncoderDataOutput.class.getSimpleName() + ".close()");
out.close();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@
* @author Oleg Cherednik
* @since 12.04.2020
*/
final class Bzip2ZipEntryOutputStream extends CompressedZipEntryOutputStream {
final class Bzip2EntryOutputStream extends CompressedEntryOutputStream {

private final Bzip2OutputStream bzip2;

Bzip2ZipEntryOutputStream(DataOutput out, CompressionLevel compressionLevel) throws IOException {
Bzip2EntryOutputStream(DataOutput out, CompressionLevel compressionLevel) throws IOException {
bzip2 = new Bzip2OutputStream(out, compressionLevel);
}

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

@Override
public void close() throws IOException {
System.out.println(Bzip2EntryOutputStream.class.getSimpleName() + ".close()");
bzip2.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@
* @since 12.02.2020
*/
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class CompressedZipEntryOutputStream extends OutputStream {
public abstract class CompressedEntryOutputStream extends OutputStream {

public static CompressedZipEntryOutputStream create(ZipEntry entry, DataOutput out) throws IOException {
public static CompressedEntryOutputStream create(ZipEntry entry, DataOutput out) throws IOException {
CompressionMethod compressionMethod = entry.getCompressionMethod();
CompressionLevel compressionLevel = entry.getCompressionLevel();

if (compressionMethod == CompressionMethod.STORE)
return new StoreZipEntryOutputStream(out);
return new StoreEntryOutputStream(out);
if (compressionMethod == CompressionMethod.DEFLATE)
return new DeflateZipEntryOutputStream(out, compressionLevel);
return new DeflateEntryOutputStream(out, compressionLevel);
if (compressionMethod == CompressionMethod.BZIP2)
return new Bzip2ZipEntryOutputStream(out, compressionLevel);
return new Bzip2EntryOutputStream(out, compressionLevel);
if (compressionMethod == CompressionMethod.LZMA)
return new LzmaZipEntryOutputStream(out,
compressionLevel,
entry.isLzmaEosMarker(),
entry.getUncompressedSize());
return new LzmaEntryOutputStream(out,
compressionLevel,
entry.isLzmaEosMarker(),
entry.getUncompressedSize());
if (compressionMethod == CompressionMethod.ZSTD)
return new ZstdZipEntryOutputStream(out, compressionLevel);
return new ZstdEntryOutputStream(out, compressionLevel);

throw new CompressionNotSupportedException(compressionMethod);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Oleg Cherednik
* @since 26.07.2019
*/
final class DeflateZipEntryOutputStream extends CompressedZipEntryOutputStream {
final class DeflateEntryOutputStream extends CompressedEntryOutputStream {

private static final int FOUR = 4;

Expand All @@ -38,7 +38,7 @@ final class DeflateZipEntryOutputStream extends CompressedZipEntryOutputStream {

public boolean firstBytesRead;

DeflateZipEntryOutputStream(DataOutput out, CompressionLevel compressionLevel) {
DeflateEntryOutputStream(DataOutput out, CompressionLevel compressionLevel) {
this.out = out;
deflater.setLevel(compressionLevel.getCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@
* @author Oleg Cherednik
* @since 09.02.2020
*/
final class LzmaZipEntryOutputStream extends CompressedZipEntryOutputStream {
final class LzmaEntryOutputStream extends CompressedEntryOutputStream {

private final DataOutput out;
private final LzmaOutputStream lzma;
private boolean writeHeader = true;

LzmaZipEntryOutputStream(DataOutput out,
CompressionLevel compressionLevel,
boolean eosMarker,
long uncompressedSize)
LzmaEntryOutputStream(DataOutput out, CompressionLevel compressionLevel, boolean eosMarker, long uncompressedSize)
throws IOException {
this.out = out;
lzma = createOutputStream(out, compressionLevel, eosMarker, uncompressedSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @since 04.08.2019
*/
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
final class StoreZipEntryOutputStream extends CompressedZipEntryOutputStream {
final class StoreEntryOutputStream extends CompressedEntryOutputStream {

private final DataOutput out;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
* @author Oleg Cherednik
* @since 07.11.2021
*/
final class ZstdZipEntryOutputStream extends CompressedZipEntryOutputStream {
final class ZstdEntryOutputStream extends CompressedEntryOutputStream {

private final DataOutput out;
private final ZstdOutputStream zstd;

ZstdZipEntryOutputStream(DataOutput out, CompressionLevel compressionLevel) throws IOException {
ZstdEntryOutputStream(DataOutput out, CompressionLevel compressionLevel) throws IOException {
this.out = out;
zstd = new ZstdOutputStream(out, compressionLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.EncoderDataOutput;
import ru.olegcherednik.zip4jvm.io.out.entry.EncryptedOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.PayloadCalculationOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.encrypted.CompressedZipEntryOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.encrypted.CompressedEntryOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.DataDescriptorOut;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.LocalFileHeaderOut;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.UpdateZip64;
Expand Down Expand Up @@ -52,8 +51,7 @@ public void write(DataOutput out) throws IOException {
// 1. compression
// 2. encryption
EncoderDataOutput encoderDataOutput = new EncoderDataOutput(zipEntry.createEncoder(), out);
CompressedZipEntryOutputStream czeos = CompressedZipEntryOutputStream.create(zipEntry, encoderDataOutput);
EncryptedOutputStream eos = new EncryptedOutputStream(encoderDataOutput, czeos);
CompressedEntryOutputStream czeos = CompressedEntryOutputStream.create(zipEntry, encoderDataOutput);

zipEntry.setDiskNo(out.getDiskNo());

Expand All @@ -67,17 +65,15 @@ public void write(DataOutput out) throws IOException {
encoderDataOutput.writeEncryptionHeader();

try (InputStream in = zipEntry.getInputStream();
PayloadCalculationOutputStream os = new PayloadCalculationOutputStream(zipEntry, eos)) {
PayloadCalculationOutputStream os = new PayloadCalculationOutputStream(zipEntry, czeos)) {
IOUtils.copyLarge(in, os);
}

encoderDataOutput.encodingAccomplished();
zipEntry.setCompressedSize(out.getWrittenBytesAmount(COMPRESSED_DATA));
new UpdateZip64().update(zipEntry);

new DataDescriptorOut().write(zipEntry, out);


// ZipUtils.copyLarge(zipEntry.getInputStream(), sos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ru.olegcherednik.zip4jvm.engine.InfoEngine;
import ru.olegcherednik.zip4jvm.model.Compression;
import ru.olegcherednik.zip4jvm.model.CompressionMethod;
import ru.olegcherednik.zip4jvm.model.Encryption;
import ru.olegcherednik.zip4jvm.model.LocalFileHeader;
import ru.olegcherednik.zip4jvm.model.block.BlockModel;
import ru.olegcherednik.zip4jvm.model.block.ZipEntryBlock;
Expand All @@ -40,6 +41,7 @@
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.Zip4jvmSuite.password;
import static ru.olegcherednik.zip4jvm.assertj.Zip4jvmAssertions.assertThatZipFile;

/**
Expand All @@ -55,7 +57,7 @@ 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(fileOlegCherednik);
ZipIt.zip(zip).settings(ZipSettings.of(Compression.BZIP2, Encryption.AES_256, password)).add(fileOlegCherednik);

InfoEngine infoEngine = new InfoEngine(SrcZip.of(zip), ZipInfoSettings.builder().readEntries(true).build());
BlockModel blockModel = infoEngine.createModel();
Expand All @@ -72,7 +74,8 @@ public void shouldNotCreateDataDescriptionWhenStoreCompression() throws IOExcept
// assertThat(localFileHeader.getCompressedSize()).isEqualTo(fileBentleySize);
// assertThat(localFileHeader.getUncompressedSize()).isEqualTo(fileBentleySize);

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

}

0 comments on commit 9e6b55e

Please sign in to comment.