diff --git a/src/main/java/net/lingala/zip4j/headers/FileHeaderFactory.java b/src/main/java/net/lingala/zip4j/headers/FileHeaderFactory.java index fbbaae2a..b26dceaf 100644 --- a/src/main/java/net/lingala/zip4j/headers/FileHeaderFactory.java +++ b/src/main/java/net/lingala/zip4j/headers/FileHeaderFactory.java @@ -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); diff --git a/src/main/java/net/lingala/zip4j/io/outputstream/ZipOutputStream.java b/src/main/java/net/lingala/zip4j/io/outputstream/ZipOutputStream.java index d76c8b72..3840440d 100755 --- a/src/main/java/net/lingala/zip4j/io/outputstream/ZipOutputStream.java +++ b/src/main/java/net/lingala/zip4j/io/outputstream/ZipOutputStream.java @@ -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()); + } } initializeAndWriteFileHeader(clonedZipParameters); diff --git a/src/main/java/net/lingala/zip4j/tasks/AbstractAddFileToZipTask.java b/src/main/java/net/lingala/zip4j/tasks/AbstractAddFileToZipTask.java index 22b1fc1a..03d5ffc7 100644 --- a/src/main/java/net/lingala/zip4j/tasks/AbstractAddFileToZipTask.java +++ b/src/main/java/net/lingala/zip4j/tasks/AbstractAddFileToZipTask.java @@ -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; @@ -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); @@ -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); @@ -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); } } diff --git a/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java b/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java index 3eff38b6..fcaa9519 100644 --- a/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java +++ b/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java @@ -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); @@ -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);