Skip to content

Commit

Permalink
Add fixedEntryModificationTime attribute to AbstractZipArchiver to ov…
Browse files Browse the repository at this point in the history
…erride zipEntry times globally. closes codehaus-plexus#48
  • Loading branch information
danielwegener committed Jul 1, 2016
1 parent bb2c841 commit 7cd7ea8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Expand Up @@ -56,6 +56,11 @@ public abstract class AbstractZipArchiver
extends AbstractArchiver
{

/**
* The minimum value for MS-DOS time format: 01 Jan 1980 00:00:00 UTC
**/
private static final Long MIN_MS_DOS_TIME = 315532800000L;

private String comment;

/**
Expand Down Expand Up @@ -132,6 +137,12 @@ public abstract class AbstractZipArchiver

protected ZipArchiveOutputStream zipArchiveOutputStream;

/**
* Will always use the given modification time for zipEntries if set.
* The given modification time will be rounded up to {@link AbstractZipArchiver#MIN_MS_DOS_TIME}.
*/
private Long fixedEntryModificationTime;

private static int getJavaVersion()
{
String javaSpecVersion = System.getProperty( "java.specification.version" );
Expand Down Expand Up @@ -212,6 +223,14 @@ public boolean isFilesonly()
return doFilesonly;
}

public Long getFixedEntryModificationTime() {
return fixedEntryModificationTime;
}

public void setFixedEntryModificationTime(Long fixedEntryModificationTime) {
this.fixedEntryModificationTime = fixedEntryModificationTime;
}

@Override
protected void execute()
throws ArchiverException, IOException
Expand Down Expand Up @@ -578,7 +597,12 @@ public InputStream get()

private void setTime( java.util.zip.ZipEntry zipEntry, long lastModified )
{
zipEntry.setTime( lastModified + ( isJava7OrLower ? 1999 : 0 ) );

if (fixedEntryModificationTime != null) {
zipEntry.setTime(Math.max(MIN_MS_DOS_TIME, fixedEntryModificationTime));
} else {
zipEntry.setTime( lastModified + ( isJava7OrLower ? 1999 : 0 ) );
}

/* Consider adding extended file stamp support.....
Expand Down
Expand Up @@ -797,4 +797,22 @@ public void testForcedFileModes()
}
}

public void testFixedEntryModificationTime()
throws IOException
{
final long almostMinDosTime = 315532802000L;
final File zipFile = getTestFile( "target/output/zip-with-fixed-entry-modification-times.zip" );
final ZipArchiver archiver = getZipArchiver( zipFile );
archiver.setFixedEntryModificationTime(almostMinDosTime);
archiver.addDirectory(new File( "src/test/resources/zip-timestamp"));
archiver.createArchive();

assertTrue( zipFile.exists() );
final ZipFile zf = new ZipFile( zipFile );
assertEquals(new Date(almostMinDosTime), zf.getEntry( "file-with-even-time.txt" ).getLastModifiedDate());
assertEquals(new Date(almostMinDosTime), zf.getEntry( "file-with-odd-time.txt" ).getLastModifiedDate());
assertEquals(new Date(almostMinDosTime), zf.getEntry( "foo/" ).getLastModifiedDate());
}

}

0 comments on commit 7cd7ea8

Please sign in to comment.