Skip to content

Commit

Permalink
#3 Store Compression optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Oct 30, 2024
1 parent fb135f2 commit b59e2bb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.olegcherednik.zip4jvm.io.out;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;

import lombok.RequiredArgsConstructor;

import java.io.IOException;
import java.io.OutputStream;

/**
* @author Oleg Cherednik
* @since 29.10.2024
*/
@RequiredArgsConstructor
public class DataOutputStream extends OutputStream {

private final DataOutput out;

@Override
public void write(int val) throws IOException {
out.writeByte(val);
}

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
out.write(buf, offs, len);
}

@Override
public void flush() throws IOException {
out.flush();
}

@Override
public void close() throws IOException {
out.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
* @author Oleg Cherednik
* @since 11.02.2020
*/
abstract class WriteFileDataOutput extends BaseDataOutput {
public class WriteFileDataOutput extends BaseDataOutput {

private WriteFile writeFile;

protected final void createFile(Path zip) throws IOException {
public final void createFile(Path zip) throws IOException {
writeFile = LittleEndianWriteFile.create(zip);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* This class represents a compressed stream using given {@link CompressionMethod}.
* It extends from the {@link OutputStream} to be able to use standard output
* optimizations (e.g. from {@link IOUtils}).
* <p>
* This {@link OutputStream} does not close delegate {@link DataOutput} when
* method {@link CompressedEntryOutputStream#close()} is invoked.
*
* @author Oleg Cherednik
* @since 12.02.2020
Expand Down Expand Up @@ -71,7 +74,7 @@ public final void write(int b) throws IOException {

@Override
public void write(byte[] buf, int offs, int len) throws IOException {
throw new NotImplementedException("CompressedEntryOutputStream.write(byte[], int, int)");
throw new NotImplementedException(getClass().getSimpleName() + ".write(byte[], int, int)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package ru.olegcherednik.zip4jvm.io.writers;

import ru.olegcherednik.zip4jvm.io.out.DataOutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.EncryptedDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.WriteFileDataOutput;
import ru.olegcherednik.zip4jvm.io.out.entry.PayloadCalculationOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.compressed.CompressedEntryOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.DataDescriptorOut;
Expand All @@ -33,6 +35,10 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author Oleg Cherednik
Expand Down Expand Up @@ -60,7 +66,24 @@ public void write(DataOutput out) throws IOException {
[data descriptor]
*/

Path tmpFile = Paths.get("d:/zip4jvm/foo/bar/" + zipEntry.getFileName());
Files.deleteIfExists(tmpFile);
WriteFileDataOutput tmpOut = new WriteFileDataOutput();
tmpOut.createFile(tmpFile);
foo(tmpOut);

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

try (InputStream in = Files.newInputStream(tmpFile)) {
OutputStream os = new DataOutputStream(out);
IOUtils.copyLarge(in, os);
os.flush();
}

new UpdateZip64().update(zipEntry);
}

private void foo(DataOutput out) throws IOException {
out.mark(COMPRESSED_DATA);

EncryptedDataOutput encryptedDataOutput = EncryptedDataOutput.create(zipEntry, out);
Expand All @@ -71,13 +94,11 @@ public void write(DataOutput out) throws IOException {
try (InputStream in = zipEntry.getInputStream();
PayloadCalculationOutputStream os = new PayloadCalculationOutputStream(zipEntry, cos)) {
IOUtils.copyLarge(in, os);
out.close();
}

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
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.UnzipSettings;
import ru.olegcherednik.zip4jvm.model.settings.ZipInfoSettings;
import ru.olegcherednik.zip4jvm.model.settings.ZipSettings;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.UnzipParameters;
import org.testng.annotations.Test;

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

import static org.assertj.core.api.Assertions.assertThat;
import static ru.olegcherednik.zip4jvm.TestData.fileBentley;
Expand All @@ -57,7 +61,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, Encryption.AES_256, password)).add(fileOlegCherednik);
ZipIt.zip(zip).settings(ZipSettings.of(Compression.STORE, Encryption.AES_128, password)).add(fileOlegCherednik);

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

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

// ZipInfo.zip(zip).password(password).settings(ZipInfoSettings.builder()
// .readEntries(true)
// .copyPayload(true)
// .build())
// .decompose(Paths.get("d:/zip4jvm/foo/xxx/2"));
}

// public void foo() throws IOException {
// Path zip = Paths.get(
// "D:\\zip4jvm\\foo\\ZipCompressionOptimizationTest/src.zip");
// Path dest = Paths.get("D:\\zip4jvm\\foo\\xxx\\3");
// new ZipFile(zip.toFile(), password).extractAll(dest.toString());
//
//// UnzipIt.zip(zip).password(password).destDir(dest).extract();
// }

}

0 comments on commit b59e2bb

Please sign in to comment.