Skip to content

Commit

Permalink
Rename setTime method to setZipEntryTime
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Solórzano <jorsol@gmail.com>
  • Loading branch information
jorsol authored and hboutemy committed Jun 5, 2022
1 parent ee2f99d commit 50c5fc0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,18 +818,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 );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,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 );
Expand Down Expand Up @@ -528,35 +528,26 @@ protected void zipFile( final ArchiveEntry entry, ConcurrentJarCreator zOut, Str
}
}

protected void setTime( ZipArchiveEntry zipEntry, long lastModified )
/**
* Set the ZipEntry dostime using the date if specified otherwise the original time.
*
* <p>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,
Expand Down Expand Up @@ -595,12 +586,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 )
{
Expand Down
31 changes: 20 additions & 11 deletions src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() );
}
}

/*
Expand Down

0 comments on commit 50c5fc0

Please sign in to comment.