Skip to content

Commit 2dada78

Browse files
committed
Rename setTime method to setZipEntryTime
Signed-off-by: Jorge Solórzano <jorsol@gmail.com>
1 parent ae29b75 commit 2dada78

File tree

4 files changed

+42
-44
lines changed

4 files changed

+42
-44
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
@@ -829,18 +829,18 @@ else if ( !name.contains( "/" ) )
829829
* Override the behavior of the Zip Archiver to match the output of the JAR tool.
830830
*
831831
* @param zipEntry to set the last modified time
832-
* @param lastModified to set in the zip entry only if the {@link #getLastModifiedTime()} returns null.
832+
* @param lastModifiedTime to set in the zip entry only if {@link #getLastModifiedTime()} returns null
833833
*/
834834
@Override
835-
protected void setTime( ZipArchiveEntry zipEntry, long lastModified )
835+
protected void setZipEntryTime( ZipArchiveEntry zipEntry, long lastModifiedTime )
836836
{
837837
if ( getLastModifiedTime() != null )
838838
{
839-
lastModified = getLastModifiedTime().toMillis();
839+
lastModifiedTime = getLastModifiedTime().toMillis();
840840
}
841841

842842
// The JAR tool does not round up, so we keep that behavior here (JDK-8277755).
843-
zipEntry.setTime( lastModified );
843+
zipEntry.setTime( lastModifiedTime );
844844
}
845845

846846
}

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

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

468468
ze.setMethod( doCompress ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED );
469469
ze.setUnixMode( UnixStat.FILE_FLAG | mode );
@@ -534,35 +534,26 @@ public InputStream get()
534534
}
535535
}
536536

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

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

568559
protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String vPath, int mode,
@@ -601,12 +592,12 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
601592

602593
if ( dir != null && dir.isExisting() )
603594
{
604-
setTime( ze, dir.getLastModified() );
595+
setZipEntryTime( ze, dir.getLastModified() );
605596
}
606597
else
607598
{
608599
// ZIPs store time with a granularity of 2 seconds, round up
609-
setTime( ze, System.currentTimeMillis() );
600+
setZipEntryTime( ze, System.currentTimeMillis() );
610601
}
611602
if ( !isSymlink )
612603
{

src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import java.nio.file.attribute.FileTime;
1111
import java.text.ParseException;
1212
import java.text.SimpleDateFormat;
13-
import java.util.ArrayList;
1413
import java.util.Enumeration;
15-
import java.util.List;
1614
import java.util.Random;
1715
import java.util.TimeZone;
1816
import java.util.zip.ZipEntry;

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)