Skip to content

Rename setTime method to setZipEntryTime #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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.
*
* <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 @@ -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 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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