Skip to content

Commit

Permalink
#434 Use system time if last modified file time is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Jun 20, 2022
1 parent 3002b03 commit 0f99010
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ public FileHeader generateFileHeader(ZipParameters zipParameters, boolean isSpli
fileHeader.setFileName(fileName);
fileHeader.setFileNameLength(determineFileNameLength(fileName, charset));
fileHeader.setDiskNumberStart(isSplitZip ? currentDiskNumberStart : 0);

if (zipParameters.getLastModifiedFileTime() > 0) {
fileHeader.setLastModifiedTime(Zip4jUtil.epochToExtendedDosTime(zipParameters.getLastModifiedFileTime()));
} else {
fileHeader.setLastModifiedTime(Zip4jUtil.epochToExtendedDosTime(System.currentTimeMillis()));
}
fileHeader.setLastModifiedTime(Zip4jUtil.epochToExtendedDosTime(zipParameters.getLastModifiedFileTime()));

boolean isDirectory = isZipEntryDirectory(fileName);
fileHeader.setDirectory(isDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public void putNextEntry(ZipParameters zipParameters) throws IOException {
clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
clonedZipParameters.setEncryptFiles(false);
clonedZipParameters.setEntrySize(0);

if (zipParameters.getLastModifiedFileTime() <= 0) {
clonedZipParameters.setLastModifiedFileTime(System.currentTimeMillis());
}
}

This comment has been minimized.

Copy link
@mintern

mintern Sep 6, 2022

Is this logic intended to apply only to directories? Since the fileHeader.setLastModifiedTime(Zip4jUtil.epochToExtendedDosTime(System.currentTimeMillis())); line was removed from generateFileHeader, a file entry with zipParameters.getLastModifiedFileTime() == 0 now maintains its 0 value when putNextEntry is called. Was that the intent of this change?

This comment has been minimized.

Copy link
@srikanth-lingala

srikanth-lingala Sep 7, 2022

Author Owner

Fixed as part of #473

initializeAndWriteFileHeader(clonedZipParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -198,15 +196,20 @@ void updateLocalFileHeader(FileHeader fileHeader, SplitOutputStream splitOutputS
headerWriter.updateLocalFileHeader(fileHeader, getZipModel(), splitOutputStream);
}

// Suppressing warning to use BasicFileAttributes as this has trouble reading symlink's attributes
@SuppressWarnings("BulkFileAttributesRead")
private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, File fileToAdd,
ProgressMonitor progressMonitor) throws IOException {
ZipParameters clonedZipParameters = new ZipParameters(zipParameters);

BasicFileAttributes fileAttributes = Files.readAttributes(fileToAdd.toPath(), BasicFileAttributes.class);
if (fileAttributes.isDirectory()) {
if (fileToAdd.isDirectory()) {
clonedZipParameters.setEntrySize(0);
} else {
clonedZipParameters.setEntrySize(fileAttributes.size());
clonedZipParameters.setEntrySize(fileToAdd.length());
}

if (zipParameters.getLastModifiedFileTime() <= 0) {
clonedZipParameters.setLastModifiedFileTime(fileToAdd.lastModified());
}

clonedZipParameters.setWriteExtendedLocalFileHeader(false);
Expand All @@ -216,7 +219,7 @@ private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, F
clonedZipParameters.setFileNameInZip(relativeFileName);
}

if (fileAttributes.isDirectory()) {
if (fileToAdd.isDirectory()) {
clonedZipParameters.setCompressionMethod(STORE);
clonedZipParameters.setEncryptionMethod(NONE);
clonedZipParameters.setEncryptFiles(false);
Expand All @@ -227,7 +230,7 @@ private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, F
progressMonitor.setCurrentTask(ADD_ENTRY);
}

if (fileAttributes.size() == 0) {
if (fileToAdd.length() == 0) {
clonedZipParameters.setCompressionMethod(STORE);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/net/lingala/zip4j/AddFilesToZipIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ public void testAddStreamToZipWithoutEncryptionForNewZipAddsSuccessfully() throw
}

@Test
public void testAddStreamToWithStoreCompressionAndWithoutEncryption() throws IOException {
public void testAddStreamToZipWithStoreCompressionAndWithoutEncryption() throws IOException {
File fileToAdd = TestUtils.getTestFileFromResources("бореиская.txt");
ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(CompressionMethod.STORE);
Expand All @@ -780,7 +780,7 @@ public void testAddStreamToWithStoreCompressionAndWithoutEncryption() throws IOE
}

@Test
public void testAddStreamToWithStoreCompressionAndZipStandardEncryption() throws IOException {
public void testAddStreamToZipWithStoreCompressionAndZipStandardEncryption() throws IOException {
File fileToAdd = TestUtils.getTestFileFromResources("sample_text_large.txt");
ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null);
zipParameters.setCompressionMethod(CompressionMethod.STORE);
Expand Down

0 comments on commit 0f99010

Please sign in to comment.