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 64fa05f commit 52e0757
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ final class Bzip2EntryOutputStream extends EncryptedEntryOutputStream {

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

@Override
public void close() throws IOException {
bzip2.close();
encoderDataOutput.encodingAccomplished();
super.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ final class DeflateEntryOutputStream extends EncryptedEntryOutputStream {

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

while (!deflater.needsInput()) {
Expand Down Expand Up @@ -90,7 +89,6 @@ private void finish() throws IOException {
public void close() throws IOException {
finish();
encoderDataOutput.encodingAccomplished();
super.close();
}

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

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.NotImplementedException;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -62,14 +63,12 @@ public static EncryptedEntryOutputStream create(ZipEntry entry, EncoderDataOutpu

@Override
public final void write(int b) throws IOException {
write(new byte[] { (byte) b }, 0, 1);
}

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

@Override
public void close() throws IOException {
throw new NotImplementedException("EncryptedEntryOutputStream.write(byte[], int, int)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ private static LzmaOutputStream createOutputStream(CompressionLevel compressionL

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

if (writeHeader) {
encoderDataOutput.writeByte((byte) 19); // major version
encoderDataOutput.writeByte((byte) 0); // minor version
Expand All @@ -70,7 +68,6 @@ public void write(byte[] buf, int offs, int len) throws IOException {
public void close() throws IOException {
lzma.close();
encoderDataOutput.encodingAccomplished();
super.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ final class StoreEntryOutputStream extends EncryptedEntryOutputStream {

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

@Override
public void close() throws IOException {
encoderDataOutput.encodingAccomplished();
super.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ final class ZstdEntryOutputStream extends EncryptedEntryOutputStream {

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

@Override
public void close() throws IOException {
zstd.close();
encoderDataOutput.encodingAccomplished();
super.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.olegcherednik.zip4jvm.io.out.entry.xxx;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.writers.DataDescriptorWriter;
import ru.olegcherednik.zip4jvm.model.DataDescriptor;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import java.io.IOException;

/**
* @author Oleg Cherednik
* @since 29.10.2024
*/
public final class DataDescriptorOut {

public void write(ZipEntry zipEntry, DataOutput out) throws IOException {
if (!zipEntry.isDataDescriptorAvailable())
return;

DataDescriptor dataDescriptor = new DataDescriptor(zipEntry.getChecksum(),
zipEntry.getCompressedSize(),
zipEntry.getUncompressedSize());
DataDescriptorWriter.get(zipEntry.isZip64(), dataDescriptor).write(out);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.olegcherednik.zip4jvm.io.out.entry.xxx;

import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_ENTRY_SIZE;
import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_LOCAL_FILE_HEADER_OFFS;
import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_TOTAL_DISKS;

/**
* @author Oleg Cherednik
* @since 29.10.2024
*/
public final class UpdateZip64 {

public void update(ZipEntry zipEntry) {
if (zipEntry.getCompressedSize() > MAX_ENTRY_SIZE)
zipEntry.setZip64(true);
if (zipEntry.getUncompressedSize() > MAX_ENTRY_SIZE)
zipEntry.setZip64(true);
if (zipEntry.getDiskNo() > MAX_TOTAL_DISKS)
zipEntry.setZip64(true);
if (zipEntry.getLocalFileHeaderRelativeOffs() > MAX_LOCAL_FILE_HEADER_OFFS)
zipEntry.setZip64(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import ru.olegcherednik.zip4jvm.io.out.entry.PayloadCalculationOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.SequenceOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.encrypted.EncryptedEntryOutputStream;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.DataDescriptorOut;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.LocalFileHeaderOut;
import ru.olegcherednik.zip4jvm.model.DataDescriptor;
import ru.olegcherednik.zip4jvm.io.out.entry.xxx.UpdateZip64;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.function.Writer;

Expand All @@ -34,10 +35,6 @@
import java.io.IOException;
import java.io.InputStream;

import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_ENTRY_SIZE;
import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_LOCAL_FILE_HEADER_OFFS;
import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_TOTAL_DISKS;

/**
* @author Oleg Cherednik
* @since 26.02.2023
Expand All @@ -64,48 +61,17 @@ public void write(DataOutput out) throws IOException {

try (InputStream in = zipEntry.getInputStream();
SequenceOutputStream sos = new SequenceOutputStream(os)) {

IOUtils.copyLarge(in, sos);

// zipEntry.setCompressedSize(out.getWrittenBytesAmount(COMPRESSED_DATA));

// zipEntry.setCompressedSize(out.getWrittenBytesAmount(COMPRESSED_DATA));
// updateZip64();
// writeDataDescriptor();

int a = 0;
a++;
}

zipEntry.setCompressedSize(out.getWrittenBytesAmount(COMPRESSED_DATA));
updateZip64(zipEntry);
writeDataDescriptor(zipEntry, out);
new UpdateZip64().update(zipEntry);
new DataDescriptorOut().write(zipEntry, out);


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

private static void updateZip64(ZipEntry zipEntry) {
if (zipEntry.getCompressedSize() > MAX_ENTRY_SIZE)
zipEntry.setZip64(true);
if (zipEntry.getUncompressedSize() > MAX_ENTRY_SIZE)
zipEntry.setZip64(true);
if (zipEntry.getDiskNo() > MAX_TOTAL_DISKS)
zipEntry.setZip64(true);
if (zipEntry.getLocalFileHeaderRelativeOffs() > MAX_LOCAL_FILE_HEADER_OFFS)
zipEntry.setZip64(true);
}

private static void writeDataDescriptor(ZipEntry zipEntry, DataOutput out) throws IOException {
if (!zipEntry.isDataDescriptorAvailable())
return;

DataDescriptor dataDescriptor = new DataDescriptor(zipEntry.getChecksum(),
zipEntry.getCompressedSize(),
zipEntry.getUncompressedSize());
DataDescriptorWriter.get(zipEntry.isZip64(), dataDescriptor).write(out);
}

@Override
public String toString() {
return '+' + zipEntry.getFileName();
Expand Down

0 comments on commit 52e0757

Please sign in to comment.