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 fe315a5 commit e6a88fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@
import ru.olegcherednik.zip4jvm.model.builders.LocalFileHeaderBuilder;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import org.apache.commons.codec.digest.PureJavaCrc32;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_ENTRY_SIZE;
import static ru.olegcherednik.zip4jvm.model.ZipModel.MAX_LOCAL_FILE_HEADER_OFFS;
Expand All @@ -52,9 +48,9 @@ public final class EntryMetadataOutputStream extends OutputStream {

private final ZipEntry zipEntry;
private final DataOutput out;
private final Checksum checksum = new PureJavaCrc32();
// private final Checksum checksum = new PureJavaCrc32();

private long uncompressedSize;
// private long uncompressedSize;

public EntryMetadataOutputStream(ZipEntry zipEntry, DataOutput out) {
this.zipEntry = zipEntry;
Expand All @@ -70,20 +66,20 @@ public void writeLocalFileHeader() throws IOException {

@Override
public void write(int b) throws IOException {
checksum.update(b);
uncompressedSize++;
// checksum.update(b);
// uncompressedSize++;
}

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

@Override
public void close() throws IOException {
zipEntry.setChecksum(checksum.getValue());
zipEntry.setUncompressedSize(uncompressedSize);
// zipEntry.setChecksum(checksum.getValue());
// zipEntry.setUncompressedSize(uncompressedSize);
zipEntry.setCompressedSize(out.getWrittenBytesAmount(COMPRESSED_DATA));
updateZip64();
writeDataDescriptor();
Expand All @@ -104,7 +100,7 @@ private void writeDataDescriptor() throws IOException {
if (!zipEntry.isDataDescriptorAvailable())
return;

DataDescriptor dataDescriptor = new DataDescriptor(checksum.getValue(),
DataDescriptor dataDescriptor = new DataDescriptor(zipEntry.getChecksum(),
zipEntry.getCompressedSize(),
zipEntry.getUncompressedSize());
DataDescriptorWriter.get(zipEntry.isZip64(), dataDescriptor).write(out);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package ru.olegcherednik.zip4jvm.io.out.entry;

import ru.olegcherednik.zip4jvm.io.out.entry.encrypted.EncryptedEntryOutputStream;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.PureJavaCrc32;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

/**
Expand All @@ -17,7 +16,8 @@
@RequiredArgsConstructor
public class FooOutputStream extends OutputStream {

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

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

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void write(DataOutput out) throws IOException {
EntryMetadataOutputStream emos = new EntryMetadataOutputStream(zipEntry, out);
EncryptedEntryOutputStream eos = EncryptedEntryOutputStream.create(zipEntry, encoderDataOutput, emos);

FooOutputStream os = new FooOutputStream(eos);
FooOutputStream os = new FooOutputStream(zipEntry, eos);

zipEntry.setDiskNo(out.getDiskNo());

Expand Down

0 comments on commit e6a88fc

Please sign in to comment.