Skip to content

Commit

Permalink
#93 Remove DataDescriptor supplier from settings
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Nov 1, 2024
1 parent b2b7d6d commit 0eea886
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void add(ZipEntry entry) {
}

private Writer createWriter(ZipEntry entry) {
if (entry.isUseDataDescriptor())
if (entry.isDataDescriptorAvailable())
return new ZipEntryWriter(entry);

Path dir = tempZipModel.getTempDir().resolve(UUID.randomUUID().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public static EntryInputStream create(ZipEntry zipEntry,
long absoluteOffs = in.convertToAbsoluteOffs(zipEntry.getDiskNo(), zipEntry.getLocalFileHeaderRelativeOffs());

LocalFileHeader localFileHeader = new LocalFileHeaderReader(absoluteOffs, charsetCustomizer).read(in);
// TODO check why do I use Supplier here
zipEntry.setDataDescriptorAvailable(() -> localFileHeader.getGeneralPurposeFlag().isDataDescriptorAvailable());
zipEntry.setDataDescriptorAvailable(localFileHeader.getGeneralPurposeFlag().isDataDescriptorAvailable());
// TODO check that localFileHeader matches fileHeader
CompressionMethod compressionMethod = zipEntry.getCompressionMethod();
Compression compression = Compression.parseCompressionMethod(compressionMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public void copyLocalFileHeader(DataOutput out) throws IOException {
long absoluteOffs = in.convertToAbsoluteOffs(zipEntry.getDiskNo(),
zipEntry.getLocalFileHeaderRelativeOffs());
LocalFileHeader localFileHeader = new LocalFileHeaderReader(absoluteOffs, Charsets.UNMODIFIED).read(in);
zipEntry.setDataDescriptorAvailable(() -> localFileHeader.getGeneralPurposeFlag()
.isDataDescriptorAvailable());
zipEntry.setDataDescriptorAvailable(localFileHeader.getGeneralPurposeFlag().isDataDescriptorAvailable());
new LocalFileHeaderWriter(localFileHeader).write(out);
}

Expand Down
16 changes: 1 addition & 15 deletions src/main/java/ru/olegcherednik/zip4jvm/model/entry/ZipEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.function.BooleanSupplier;

/**
* Represents one single entry in zip archive, i.e. one instance of {@link LocalFileHeader} and related to
Expand Down Expand Up @@ -79,9 +78,7 @@ public class ZipEntry {
private char[] password;
private int diskNo;
private long localFileHeaderRelativeOffs;
@Getter(AccessLevel.NONE)
private BooleanSupplier dataDescriptorAvailable = () -> false;
private boolean useDataDescriptor;
private boolean dataDescriptorAvailable;
private long uncompressedSize;
private long compressedSize;
private boolean lzmaEosMarker = true;
Expand All @@ -90,10 +87,6 @@ public class ZipEntry {
private boolean utf8;
private boolean strongEncryption;

public void setDataDescriptorAvailable(BooleanSupplier dataDescriptorAvailable) {
this.dataDescriptorAvailable = dataDescriptorAvailable;
}

public boolean isSymlink() {
return externalFileAttributes.isSymlink();
}
Expand Down Expand Up @@ -135,13 +128,6 @@ public void setChecksum(long checksum) {
/* nothing to set */
}

public final boolean isDataDescriptorAvailable() {
if (dataDescriptorAvailable.getAsBoolean() != useDataDescriptor) {
throw new RuntimeException("data descriptor");
}
return dataDescriptorAvailable.getAsBoolean();
}

public ZipFile.Entry createImmutableEntry() {
if (isDirectory())
return ZipFile.Entry.directory(fileName, lastModifiedTime, externalFileAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ public static ZipEntry symlink(Path symlinkTarget,
EncryptionMethod.OFF,
inputStreamSup);

zipEntry.setDataDescriptorAvailable(() -> Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(true));
zipEntry.setUseDataDescriptor(Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(true));
zipEntry.setDataDescriptorAvailable(Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(true));
zipEntry.setComment(entrySettings.getComment());
zipEntry.setUtf8(entrySettings.isUtf8());
zipEntry.setUncompressedSize(buf.length);
Expand Down Expand Up @@ -132,11 +130,8 @@ public static ZipEntry regularFile(Path file, String fileName, ZipEntrySettings
encryptionMethod,
inputStreamSup);


zipEntry.setDataDescriptorAvailable(() -> Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(compressionMethod != CompressionMethod.STORE));
zipEntry.setUseDataDescriptor(Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(compressionMethod != CompressionMethod.STORE));
zipEntry.setDataDescriptorAvailable(Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(compressionMethod != CompressionMethod.STORE));
zipEntry.setZip64(entrySettings.isZip64());
zipEntry.setPassword(entrySettings.getPassword());
zipEntry.setComment(entrySettings.getComment());
Expand Down Expand Up @@ -182,7 +177,7 @@ private ZipEntry createSymlinkEntry() {
EncryptionMethod.OFF,
inputStreamSup);

zipEntry.setDataDescriptorAvailable(() -> true);
zipEntry.setDataDescriptorAvailable(true);
zipEntry.setComment(entrySettings.getComment());
zipEntry.setUtf8(entrySettings.isUtf8());
zipEntry.setUncompressedSize(entry.getUncompressedSize());
Expand Down Expand Up @@ -217,10 +212,8 @@ private ZipEntry createRegularFileEntry() {
encryptionMethod,
inputStreamSup);

zipEntry.setDataDescriptorAvailable(() -> Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(compressionMethod != CompressionMethod.STORE));
zipEntry.setUseDataDescriptor(Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(compressionMethod != CompressionMethod.STORE));
zipEntry.setDataDescriptorAvailable(Optional.ofNullable(entrySettings.getDataDescriptorAvailable())
.orElse(compressionMethod != CompressionMethod.STORE));
zipEntry.setZip64(entrySettings.isZip64());
zipEntry.setPassword(entrySettings.getPassword());
zipEntry.setComment(entrySettings.getComment());
Expand Down Expand Up @@ -270,8 +263,7 @@ private ZipEntry createRegularFileEntry() {
encryptionMethod,
inputStreamSup);

zipEntry.setDataDescriptorAvailable(generalPurposeFlag::isDataDescriptorAvailable);
zipEntry.setUseDataDescriptor(generalPurposeFlag.isDataDescriptorAvailable());
zipEntry.setDataDescriptorAvailable(generalPurposeFlag.isDataDescriptorAvailable());
zipEntry.setLzmaEosMarker(generalPurposeFlag.isLzmaEosMarker());
zipEntry.setZip64(fileHeader.isZip64());
zipEntry.setComment(fileHeader.getComment());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void shouldCreateLocalFileHeaderWhenZip64Entry() throws IOException {
ZipEntrySettings entrySettings = ZipEntrySettings.builder().zip64(true).utf8(true).build();
ZipEntry zipEntry = ZipEntryBuilder.regularFile(fileDucati, fileNameDucati, entrySettings);

zipEntry.setDataDescriptorAvailable(() -> false);
zipEntry.setUseDataDescriptor(false);
zipEntry.setDataDescriptorAvailable(false);

LocalFileHeader localFileHeader = new LocalFileHeaderBuilder(zipEntry).build();
assertThat(localFileHeader).isNotNull();
Expand Down

0 comments on commit 0eea886

Please sign in to comment.