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 29, 2024
1 parent fa03abc commit fb135f2
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/main/java/ru/olegcherednik/zip4jvm/engine/ZipEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ru.olegcherednik.zip4jvm.io.out.data.SolidZipDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.SplitZipDataOutput;
import ru.olegcherednik.zip4jvm.io.writers.ExistedEntryWriter;
import ru.olegcherednik.zip4jvm.io.writers.ZipEntryNoDataDescriptorWriter;
import ru.olegcherednik.zip4jvm.io.writers.ZipEntryWriter;
import ru.olegcherednik.zip4jvm.model.ZipModel;
import ru.olegcherednik.zip4jvm.model.builders.ZipModelBuilder;
Expand Down Expand Up @@ -141,8 +142,10 @@ private void add(ZipEntry zipEntry) {
if (fileNameWriter.containsKey(zipEntry.getFileName()))
throw new EntryDuplicationException(zipEntry.getFileName());

Writer writer = zipEntry.isDataDescriptorAvailable() ? new ZipEntryWriter(zipEntry)
: new ZipEntryNoDataDescriptorWriter(zipEntry);
tempZipModel.addEntry(zipEntry);
fileNameWriter.put(zipEntry.getFileName(), new ZipEntryWriter(zipEntry));
fileNameWriter.put(zipEntry.getFileName(), writer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

import ru.olegcherednik.zip4jvm.io.bzip2.Bzip2OutputStream;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

import ru.olegcherednik.zip4jvm.exception.CompressionNotSupportedException;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

import ru.olegcherednik.zip4jvm.io.lzma.LzmaInputStream;
import ru.olegcherednik.zip4jvm.io.lzma.LzmaOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package ru.olegcherednik.zip4jvm.io.out.entry.encrypted;
package ru.olegcherednik.zip4jvm.io.out.entry.compressed;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.zstd.ZstdOutputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.io.writers;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.EncryptedDataOutput;
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;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.LocalFileHeaderOut;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.UpdateZip64;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.function.Writer;

import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
* @author Oleg Cherednik
* @since 26.02.2023
*/
@RequiredArgsConstructor
public final class ZipEntryNoDataDescriptorWriter implements Writer {

private static final String COMPRESSED_DATA =
ZipEntryNoDataDescriptorWriter.class.getSimpleName() + ".entryCompressedDataOffs";

private final ZipEntry zipEntry;

@Override
public void write(DataOutput out) throws IOException {
// 1. compression
// 2. encryption
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);

EncryptedDataOutput encryptedDataOutput = EncryptedDataOutput.create(zipEntry, out);
CompressedEntryOutputStream cos = CompressedEntryOutputStream.create(zipEntry, encryptedDataOutput);

encryptedDataOutput.writeEncryptionHeader();

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

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

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

@Override
public String toString() {
return '+' + zipEntry.getFileName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.EncryptedDataOutput;
import ru.olegcherednik.zip4jvm.io.out.entry.PayloadCalculationOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.encrypted.CompressedEntryOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.compressed.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

0 comments on commit fb135f2

Please sign in to comment.