diff --git a/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java b/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java index b6d25e5ff..0287954a1 100644 --- a/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java @@ -829,18 +829,18 @@ else if ( !name.contains( "/" ) ) * Override the behavior of the Zip Archiver to match the output of the JAR tool. * * @param zipEntry to set the last modified time - * @param lastModified to set in the zip entry only if the {@link #getLastModifiedTime()} returns null. + * @param lastModifiedTime to set in the zip entry only if {@link #getLastModifiedTime()} returns null */ @Override - protected void setTime( ZipArchiveEntry zipEntry, long lastModified ) + protected void setZipEntryTime( ZipArchiveEntry zipEntry, long lastModifiedTime ) { if ( getLastModifiedTime() != null ) { - lastModified = getLastModifiedTime().toMillis(); + lastModifiedTime = getLastModifiedTime().toMillis(); } // The JAR tool does not round up, so we keep that behavior here (JDK-8277755). - zipEntry.setTime( lastModified ); + zipEntry.setTime( lastModifiedTime ); } } diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java index 6ddfd02f0..8195c6258 100755 --- a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java @@ -463,7 +463,7 @@ protected void zipFile( InputStreamSupplier in, ConcurrentJarCreator zOut, Strin if ( !skipWriting ) { ZipArchiveEntry ze = new ZipArchiveEntry( vPath ); - setTime( ze, lastModified ); + setZipEntryTime( ze, lastModified ); ze.setMethod( doCompress ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED ); ze.setUnixMode( UnixStat.FILE_FLAG | mode ); @@ -534,35 +534,26 @@ public InputStream get() } } - protected void setTime( ZipArchiveEntry zipEntry, long lastModified ) + /** + * Set the ZipEntry dostime using the date if specified otherwise the original time. + * + *
Zip archives store file modification times with a granularity of two seconds, so the times will either be + * rounded up or down. If you round down, the archive will always seem out-of-date when you rerun the task, so the + * default is to round up. Rounding up may lead to a different type of problems like JSPs inside a web archive that + * seem to be slightly more recent than precompiled pages, rendering precompilation useless. + * plexus-archiver chooses to round up. + * + * @param zipEntry to set the last modified time + * @param lastModifiedTime to set in the zip entry only if {@link #getLastModifiedTime()} returns null + */ + protected void setZipEntryTime( ZipArchiveEntry zipEntry, long lastModifiedTime ) { if ( getLastModifiedTime() != null ) { - lastModified = getLastModifiedTime().toMillis(); + lastModifiedTime = getLastModifiedTime().toMillis(); } - // Zip archives store file modification times with a - // granularity of two seconds, so the times will either be rounded - // up or down. If you round down, the archive will always seem - // out-of-date when you rerun the task, so the default is to round - // up. Rounding up may lead to a different type of problems like - // JSPs inside a web archive that seem to be slightly more recent - // than precompiled pages, rendering precompilation useless. - // plexus-archiver chooses to round up. - zipEntry.setTime( lastModified + 1999 ); - - /* Consider adding extended file stamp support..... - - X5455_ExtendedTimestamp ts = new X5455_ExtendedTimestamp(); - ts.setModifyJavaTime(new Date(lastModified)); - if (zipEntry.getExtra() != null){ - // Uh-oh. What do we do now. - throw new IllegalStateException("DIdnt expect to see xtradata here ?"); - - } else { - zipEntry.setExtra(ts.getLocalFileDataData()); - } - */ + zipEntry.setTime( lastModifiedTime + 1999 ); } protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String vPath, int mode, @@ -601,12 +592,12 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v if ( dir != null && dir.isExisting() ) { - setTime( ze, dir.getLastModified() ); + setZipEntryTime( ze, dir.getLastModified() ); } else { // ZIPs store time with a granularity of 2 seconds, round up - setTime( ze, System.currentTimeMillis() ); + setZipEntryTime( ze, System.currentTimeMillis() ); } if ( !isSymlink ) { diff --git a/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java index d0b9471ae..55c7af298 100644 --- a/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java @@ -10,9 +10,7 @@ import java.nio.file.attribute.FileTime; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; import java.util.Random; import java.util.TimeZone; import java.util.zip.ZipEntry; diff --git a/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java index 23c134366..b57d9feba 100644 --- a/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java @@ -31,6 +31,7 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.attribute.FileTime; import java.text.DateFormat; import java.text.ParseException; @@ -76,6 +77,7 @@ import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.Os; +import org.junit.Test; /** * @author Emmanuel Venisse @@ -631,27 +633,34 @@ public void testSymlinkArchivedFileSet() * Zip archives store file modification times with a granularity of two seconds. * Verify that ZipArchiver rounds up the last modified time. */ + @Test public void testLastModifiedTimeRounding() throws Exception { - File oddSecondsTimestampFile = File.createTempFile( "odd-seconds-timestamp", null ); - oddSecondsTimestampFile.deleteOnExit(); + Path oddSecondsTimestampFile = Files.createTempFile( "odd-seconds-timestamp", null ); + oddSecondsTimestampFile.toFile().deleteOnExit(); // The milliseconds part is set to zero as not all filesystem support timestamp more granular than second. - Files.setLastModifiedTime( oddSecondsTimestampFile.toPath(), FileTime.fromMillis( 1534189011_000L ) ); - File evenSecondsTimestampFile = File.createTempFile( "even-seconds-timestamp", null ); - evenSecondsTimestampFile.deleteOnExit(); - Files.setLastModifiedTime( evenSecondsTimestampFile.toPath(), FileTime.fromMillis( 1534189012_000L ) ); + Files.setLastModifiedTime( oddSecondsTimestampFile, FileTime.fromMillis( 1534189011_000L ) ); + Path evenSecondsTimestampFile = Files.createTempFile( "even-seconds-timestamp", null ); + evenSecondsTimestampFile.toFile().deleteOnExit(); + Files.setLastModifiedTime( evenSecondsTimestampFile, FileTime.fromMillis( 1534189012_000L ) ); File destFile = getTestFile( "target/output/last-modified-time.zip" ); ZipArchiver archiver = getZipArchiver( destFile ); - archiver.addFile( oddSecondsTimestampFile, "odd-seconds" ); - archiver.addFile( evenSecondsTimestampFile, "even-seconds" ); + archiver.addFile( oddSecondsTimestampFile.toFile(), "odd-seconds" ); + archiver.addFile( evenSecondsTimestampFile.toFile(), "even-seconds" ); archiver.createArchive(); // verify that the last modified time of the entry is equal or newer than the original file - ZipFile resultingZipFile = new ZipFile( destFile ); - assertEquals( 1534189012_000L, resultingZipFile.getEntry( "odd-seconds" ).getTime() ); - assertEquals( 1534189012_000L, resultingZipFile.getEntry( "even-seconds" ).getTime() ); + try ( ZipFile resultingZipFile = new ZipFile( destFile ) ) + { + assertEquals( 1534189012_000L, resultingZipFile.getEntry( "odd-seconds" ).getTime() ); + assertEquals( 1534189012_000L, resultingZipFile.getEntry( "even-seconds" ).getTime() ); + + FileTime expected = FileTime.fromMillis( 1534189012_000L ); + assertEquals( expected, resultingZipFile.getEntry( "odd-seconds" ).getLastModifiedTime() ); + assertEquals( expected, resultingZipFile.getEntry( "even-seconds" ).getLastModifiedTime() ); + } } /*