Skip to content

Add fixedEntryModificationTime attribute to AbstractZipArchiver #49

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really think this should be some kind of Date object, not a long. I am also contemplating if this setter should be on AbstractArchiver, others could just throw UnsupportedOepratiionException

Copy link
Member

@plamentotev plamentotev Jul 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually wouldn't it be even better if it works the same way as forced file/dir modes? I mean they are logically related - forced file/dir modes are options to override the file permissions. This is option to override the last modified date.

Copy link
Author

@danielwegener danielwegener Jul 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summarizing:

  • a method setModificationTime(Date modificationTime) (+getter) on Archiver
  • a field private Date forcedModificationTime = null on AbstractArchiver
  • The field is only used by TarArchiver, AbstractZipArchiver and DirArchiver. Other implementations would just ignore it (like with filemode)
  • Question then: do we also need overloaded methods for addFile/addResource to allow overriding the modificationTime on an entry basis (and having a modificationTime field in ArchiveEntry)?

Copy link
Member

@krosenvold krosenvold Jul 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me like ArchiveEntry needs modifcationTime with that is overridden much the same way as the "mode" field. As for per-entry modifiers I suggest you only add the ones you need for the patch. So please do like @plamentotev suggests

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));
Copy link
Author

@danielwegener danielwegener Jul 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lower values like 0L produce weird Dates when reading (seen something like 8. Nov 1979, which normally cannot be expressed by MS-DOS time). Not sure if we should do the round-up here.

} else {
zipEntry.setTime( lastModified + ( isJava7OrLower ? 1999 : 0 ) );
}

/* Consider adding extended file stamp support.....

Expand Down
Original file line number Diff line number Diff line change
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());
}

}