Skip to content

Commit c2a027f

Browse files
Use current time as default in ZipParameters. Issue #6
1 parent 596292a commit c2a027f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/java/net/lingala/zip4j/model/ZipParameters.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ZipParameters {
3333
private long entryCRC;
3434
private String defaultFolderPath;
3535
private String fileNameInZip;
36-
private long lastModifiedFileTime;
36+
private long lastModifiedFileTime = System.currentTimeMillis();
3737
private long entrySize = -1;
3838
private boolean writeExtendedLocalFileHeader = true;
3939

@@ -145,6 +145,10 @@ public long getLastModifiedFileTime() {
145145
}
146146

147147
public void setLastModifiedFileTime(long lastModifiedFileTime) {
148+
if (lastModifiedFileTime <= 0) {
149+
return;
150+
}
151+
148152
this.lastModifiedFileTime = lastModifiedFileTime;
149153
}
150154

src/test/java/net/lingala/zip4j/io/outputstream/ZipOutputStreamIT.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public void testZipOutputStreamDeflateWithStandardEncryption() throws IOExceptio
4848
testZipOutputStream(CompressionMethod.DEFLATE, true, EncryptionMethod.ZIP_STANDARD, null);
4949
}
5050

51+
@Test
52+
public void testZipOutputStreamDeflateWithStandardEncryptionWhenModifiedFileTimeNotSet()
53+
throws IOException, ZipException {
54+
testZipOutputStream(CompressionMethod.DEFLATE, true, EncryptionMethod.ZIP_STANDARD, null, false);
55+
}
56+
5157
@Test
5258
public void testZipOutputStreamDeflateWithAES128() throws IOException, ZipException {
5359
testZipOutputStream(CompressionMethod.DEFLATE, true, EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128);
@@ -61,6 +67,13 @@ public void testZipOutputStreamDeflateWithAES256() throws IOException, ZipExcept
6167
private void testZipOutputStream(CompressionMethod compressionMethod, boolean encrypt,
6268
EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength)
6369
throws IOException, ZipException {
70+
testZipOutputStream(compressionMethod, encrypt, encryptionMethod, aesKeyStrength, true);
71+
}
72+
73+
private void testZipOutputStream(CompressionMethod compressionMethod, boolean encrypt,
74+
EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength,
75+
boolean setLastModifiedTime)
76+
throws IOException, ZipException {
6477

6578
ZipParameters zipParameters = buildZipParameters(compressionMethod, encrypt, encryptionMethod, aesKeyStrength);
6679
byte[] buff = new byte[4096];
@@ -73,7 +86,9 @@ private void testZipOutputStream(CompressionMethod compressionMethod, boolean en
7386
zipParameters.setEntrySize(fileToAdd.length());
7487
}
7588

76-
zipParameters.setLastModifiedFileTime(fileToAdd.lastModified());
89+
if (setLastModifiedTime) {
90+
zipParameters.setLastModifiedFileTime(fileToAdd.lastModified());
91+
}
7792
zipParameters.setFileNameInZip(fileToAdd.getName());
7893
zos.putNextEntry(zipParameters);
7994

0 commit comments

Comments
 (0)