Skip to content

Commit

Permalink
#434 Use last modified file time from ZipParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Jun 20, 2022
1 parent 48355c9 commit 3002b03
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/lingala/zip4j/model/ZipParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum SymbolicLinkAction {
private long entryCRC;
private String defaultFolderPath;
private String fileNameInZip;
private long lastModifiedFileTime = System.currentTimeMillis();
private long lastModifiedFileTime = 0;
private long entrySize = -1;
private boolean writeExtendedLocalFileHeader = true;
private boolean overrideExistingFilesInZip = true;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/net/lingala/zip4j/tasks/AbstractAddFileToZipTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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 All @@ -39,7 +41,6 @@
import static net.lingala.zip4j.util.CrcUtil.computeFileCrc;
import static net.lingala.zip4j.util.FileUtils.assertFilesExist;
import static net.lingala.zip4j.util.FileUtils.getRelativeFileName;
import static net.lingala.zip4j.util.Zip4jUtil.epochToExtendedDosTime;

public abstract class AbstractAddFileToZipTask<T> extends AsyncZipTask<T> {

Expand Down Expand Up @@ -200,25 +201,24 @@ void updateLocalFileHeader(FileHeader fileHeader, SplitOutputStream splitOutputS
private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, File fileToAdd,
ProgressMonitor progressMonitor) throws IOException {
ZipParameters clonedZipParameters = new ZipParameters(zipParameters);
clonedZipParameters.setLastModifiedFileTime(epochToExtendedDosTime((fileToAdd.lastModified())));

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

clonedZipParameters.setWriteExtendedLocalFileHeader(false);
clonedZipParameters.setLastModifiedFileTime(fileToAdd.lastModified());

if (!Zip4jUtil.isStringNotNullAndNotEmpty(zipParameters.getFileNameInZip())) {
String relativeFileName = getRelativeFileName(fileToAdd, zipParameters);
clonedZipParameters.setFileNameInZip(relativeFileName);
}

if (fileToAdd.isDirectory()) {
clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
clonedZipParameters.setEncryptionMethod(EncryptionMethod.NONE);
if (fileAttributes.isDirectory()) {
clonedZipParameters.setCompressionMethod(STORE);
clonedZipParameters.setEncryptionMethod(NONE);
clonedZipParameters.setEncryptFiles(false);
} else {
if (clonedZipParameters.isEncryptFiles() && clonedZipParameters.getEncryptionMethod() == ZIP_STANDARD) {
Expand All @@ -227,8 +227,8 @@ private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, F
progressMonitor.setCurrentTask(ADD_ENTRY);
}

if (fileToAdd.length() == 0) {
clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
if (fileAttributes.size() == 0) {
clonedZipParameters.setCompressionMethod(STORE);
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/net/lingala/zip4j/MiscZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -30,6 +32,7 @@
import static java.util.Collections.singletonList;
import static net.lingala.zip4j.testutils.TestUtils.getFileNamesOfFiles;
import static net.lingala.zip4j.testutils.TestUtils.getTestFileFromResources;
import static net.lingala.zip4j.util.Zip4jUtil.epochToExtendedDosTime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

Expand Down Expand Up @@ -652,6 +655,24 @@ public void testAddFilesWithoutUt8PasswordAndExtractFilesWithUtf8PasswordFails()
testAddFilesWithUt8PasswordAndExtractFilesWithoutUtf8PasswordFails(false, true);
}

@Test
public void testAddFileWithCustomLastModifiedFileTimeSetsInputTime() throws IOException, ParseException {
ZipFile zipFile = new ZipFile(generatedZipFile);
String string_date = "20-January-2020";
long expectedLastModifiedTimeInMillis = new SimpleDateFormat("dd-MMM-yyyy").parse(string_date).getTime();
ZipParameters zipParameters = new ZipParameters();
zipParameters.setLastModifiedFileTime(expectedLastModifiedTimeInMillis);
String fileToTestWith = "sample.pdf";

zipFile.addFile(getTestFileFromResources(fileToTestWith), zipParameters);

verifyLastModifiedFileTime(zipFile, fileToTestWith, expectedLastModifiedTimeInMillis);
// Test again by instantiating zip file again to make sure that the last modified file time is correctly stored and
// read via HeaderReader as well
zipFile = new ZipFile(generatedZipFile);
verifyLastModifiedFileTime(zipFile, fileToTestWith, expectedLastModifiedTimeInMillis);
}

private void testAddAndExtractWithPasswordUtf8Encoding(boolean useUtf8ForPassword) throws IOException {
char[] password = "hun 焰".toCharArray();
ZipFile zipFile = new ZipFile(generatedZipFile, password);
Expand Down Expand Up @@ -746,4 +767,15 @@ private List<Thread> filterForThreadsByName(String threadName) {
}
return filteredThreads;
}

private void verifyLastModifiedFileTime(ZipFile zipFile, String entryNameInZipToVerify,
long expectedLastModifiedTimeInMillis) throws IOException {
FileHeader fileHeader = zipFile.getFileHeader(entryNameInZipToVerify);
assertThat(fileHeader.getLastModifiedTimeEpoch()).isEqualTo(expectedLastModifiedTimeInMillis);
assertThat(fileHeader.getLastModifiedTime()).isEqualTo(epochToExtendedDosTime(expectedLastModifiedTimeInMillis));
zipFile.extractAll(outputFolder.getPath());
File extractedFile = Paths.get(outputFolder.getPath(), entryNameInZipToVerify).toFile();
assertThat(extractedFile.exists()).isTrue();
assertThat(extractedFile.lastModified()).isEqualTo(expectedLastModifiedTimeInMillis);
}
}

0 comments on commit 3002b03

Please sign in to comment.