Skip to content

Commit 50c5fc0

Browse files
jorsolhboutemy
authored andcommitted
Rename setTime method to setZipEntryTime
Signed-off-by: Jorge Solórzano <jorsol@gmail.com>
1 parent ee2f99d commit 50c5fc0

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,18 +818,18 @@ else if ( !name.contains( "/" ) )
818818
* Override the behavior of the Zip Archiver to match the output of the JAR tool.
819819
*
820820
* @param zipEntry to set the last modified time
821-
* @param lastModified to set in the zip entry only if the {@link #getLastModifiedTime()} returns null.
821+
* @param lastModifiedTime to set in the zip entry only if {@link #getLastModifiedTime()} returns null
822822
*/
823823
@Override
824-
protected void setTime( ZipArchiveEntry zipEntry, long lastModified )
824+
protected void setZipEntryTime( ZipArchiveEntry zipEntry, long lastModifiedTime )
825825
{
826826
if ( getLastModifiedTime() != null )
827827
{
828-
lastModified = getLastModifiedTime().toMillis();
828+
lastModifiedTime = getLastModifiedTime().toMillis();
829829
}
830830

831831
// The JAR tool does not round up, so we keep that behavior here (JDK-8277755).
832-
zipEntry.setTime( lastModified );
832+
zipEntry.setTime( lastModifiedTime );
833833
}
834834

835835
}

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ protected void zipFile( InputStreamSupplier in, ConcurrentJarCreator zOut, Strin
465465
if ( !skipWriting )
466466
{
467467
ZipArchiveEntry ze = new ZipArchiveEntry( vPath );
468-
setTime( ze, lastModified );
468+
setZipEntryTime( ze, lastModified );
469469

470470
ze.setMethod( doCompress ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED );
471471
ze.setUnixMode( UnixStat.FILE_FLAG | mode );
@@ -528,35 +528,26 @@ protected void zipFile( final ArchiveEntry entry, ConcurrentJarCreator zOut, Str
528528
}
529529
}
530530

531-
protected void setTime( ZipArchiveEntry zipEntry, long lastModified )
531+
/**
532+
* Set the ZipEntry dostime using the date if specified otherwise the original time.
533+
*
534+
* <p>Zip archives store file modification times with a granularity of two seconds, so the times will either be
535+
* rounded up or down. If you round down, the archive will always seem out-of-date when you rerun the task, so the
536+
* default is to round up. Rounding up may lead to a different type of problems like JSPs inside a web archive that
537+
* seem to be slightly more recent than precompiled pages, rendering precompilation useless.
538+
* plexus-archiver chooses to round up.
539+
*
540+
* @param zipEntry to set the last modified time
541+
* @param lastModifiedTime to set in the zip entry only if {@link #getLastModifiedTime()} returns null
542+
*/
543+
protected void setZipEntryTime( ZipArchiveEntry zipEntry, long lastModifiedTime )
532544
{
533545
if ( getLastModifiedTime() != null )
534546
{
535-
lastModified = getLastModifiedTime().toMillis();
547+
lastModifiedTime = getLastModifiedTime().toMillis();
536548
}
537549

538-
// Zip archives store file modification times with a
539-
// granularity of two seconds, so the times will either be rounded
540-
// up or down. If you round down, the archive will always seem
541-
// out-of-date when you rerun the task, so the default is to round
542-
// up. Rounding up may lead to a different type of problems like
543-
// JSPs inside a web archive that seem to be slightly more recent
544-
// than precompiled pages, rendering precompilation useless.
545-
// plexus-archiver chooses to round up.
546-
zipEntry.setTime( lastModified + 1999 );
547-
548-
/* Consider adding extended file stamp support.....
549-
550-
X5455_ExtendedTimestamp ts = new X5455_ExtendedTimestamp();
551-
ts.setModifyJavaTime(new Date(lastModified));
552-
if (zipEntry.getExtra() != null){
553-
// Uh-oh. What do we do now.
554-
throw new IllegalStateException("DIdnt expect to see xtradata here ?");
555-
556-
} else {
557-
zipEntry.setExtra(ts.getLocalFileDataData());
558-
}
559-
*/
550+
zipEntry.setTime( lastModifiedTime + 1999 );
560551
}
561552

562553
protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String vPath, int mode,
@@ -595,12 +586,12 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
595586

596587
if ( dir != null && dir.isExisting() )
597588
{
598-
setTime( ze, dir.getLastModified() );
589+
setZipEntryTime( ze, dir.getLastModified() );
599590
}
600591
else
601592
{
602593
// ZIPs store time with a granularity of 2 seconds, round up
603-
setTime( ze, System.currentTimeMillis() );
594+
setZipEntryTime( ze, System.currentTimeMillis() );
604595
}
605596
if ( !isSymlink )
606597
{

src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.Writer;
3232
import java.nio.charset.StandardCharsets;
3333
import java.nio.file.Files;
34+
import java.nio.file.Path;
3435
import java.nio.file.attribute.FileTime;
3536
import java.text.DateFormat;
3637
import java.text.ParseException;
@@ -76,6 +77,7 @@
7677
import org.codehaus.plexus.util.FileUtils;
7778
import org.codehaus.plexus.util.IOUtil;
7879
import org.codehaus.plexus.util.Os;
80+
import org.junit.Test;
7981

8082
/**
8183
* @author Emmanuel Venisse
@@ -631,27 +633,34 @@ public void testSymlinkArchivedFileSet()
631633
* Zip archives store file modification times with a granularity of two seconds.
632634
* Verify that ZipArchiver rounds up the last modified time.
633635
*/
636+
@Test
634637
public void testLastModifiedTimeRounding()
635638
throws Exception
636639
{
637-
File oddSecondsTimestampFile = File.createTempFile( "odd-seconds-timestamp", null );
638-
oddSecondsTimestampFile.deleteOnExit();
640+
Path oddSecondsTimestampFile = Files.createTempFile( "odd-seconds-timestamp", null );
641+
oddSecondsTimestampFile.toFile().deleteOnExit();
639642
// The milliseconds part is set to zero as not all filesystem support timestamp more granular than second.
640-
Files.setLastModifiedTime( oddSecondsTimestampFile.toPath(), FileTime.fromMillis( 1534189011_000L ) );
641-
File evenSecondsTimestampFile = File.createTempFile( "even-seconds-timestamp", null );
642-
evenSecondsTimestampFile.deleteOnExit();
643-
Files.setLastModifiedTime( evenSecondsTimestampFile.toPath(), FileTime.fromMillis( 1534189012_000L ) );
643+
Files.setLastModifiedTime( oddSecondsTimestampFile, FileTime.fromMillis( 1534189011_000L ) );
644+
Path evenSecondsTimestampFile = Files.createTempFile( "even-seconds-timestamp", null );
645+
evenSecondsTimestampFile.toFile().deleteOnExit();
646+
Files.setLastModifiedTime( evenSecondsTimestampFile, FileTime.fromMillis( 1534189012_000L ) );
644647

645648
File destFile = getTestFile( "target/output/last-modified-time.zip" );
646649
ZipArchiver archiver = getZipArchiver( destFile );
647-
archiver.addFile( oddSecondsTimestampFile, "odd-seconds" );
648-
archiver.addFile( evenSecondsTimestampFile, "even-seconds" );
650+
archiver.addFile( oddSecondsTimestampFile.toFile(), "odd-seconds" );
651+
archiver.addFile( evenSecondsTimestampFile.toFile(), "even-seconds" );
649652
archiver.createArchive();
650653

651654
// verify that the last modified time of the entry is equal or newer than the original file
652-
ZipFile resultingZipFile = new ZipFile( destFile );
653-
assertEquals( 1534189012_000L, resultingZipFile.getEntry( "odd-seconds" ).getTime() );
654-
assertEquals( 1534189012_000L, resultingZipFile.getEntry( "even-seconds" ).getTime() );
655+
try ( ZipFile resultingZipFile = new ZipFile( destFile ) )
656+
{
657+
assertEquals( 1534189012_000L, resultingZipFile.getEntry( "odd-seconds" ).getTime() );
658+
assertEquals( 1534189012_000L, resultingZipFile.getEntry( "even-seconds" ).getTime() );
659+
660+
FileTime expected = FileTime.fromMillis( 1534189012_000L );
661+
assertEquals( expected, resultingZipFile.getEntry( "odd-seconds" ).getLastModifiedTime() );
662+
assertEquals( expected, resultingZipFile.getEntry( "even-seconds" ).getLastModifiedTime() );
663+
}
655664
}
656665

657666
/*

0 commit comments

Comments
 (0)