Skip to content

Commit 6a62131

Browse files
Copilotslachiewicz
authored andcommitted
Fix handling of zip entries with unspecified modification time (-1)
1 parent a99931b commit 6a62131

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public InputStream getContents() throws IOException {
117117
@Override
118118
public long getLastModified() {
119119
final long l = zipEntry.getTime();
120-
return l == 0 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l;
120+
return l <= 0 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l;
121121
}
122122

123123
@Override
@@ -174,12 +174,16 @@ protected void execute(final String path, final File outputDirectory) throws Arc
174174
.setInputStream(in)
175175
.setMaxCount(remainingSpace + 1)
176176
.get();
177+
long time = ze.getTime();
178+
if (time <= 0) {
179+
time = PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
180+
}
177181
extractFile(
178182
getSourceFile(),
179183
outputDirectory,
180184
bis,
181185
ze.getName(),
182-
new Date(ze.getTime()),
186+
new Date(time),
183187
ze.isDirectory(),
184188
ze.getUnixMode() != 0 ? ze.getUnixMode() : null,
185189
resolveSymlink(zipFile, ze),

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,36 @@ private ZipArchiver getZipArchiver(File destFile) {
263263
zipArchiver.setDestFile(destFile);
264264
return zipArchiver;
265265
}
266+
267+
@Test
268+
void testZipWithNegativeModificationTime() throws Exception {
269+
// Create a zip file with an entry that has -1 modification time
270+
File zipFile = new File("target/output/zip-with-negative-time.zip");
271+
zipFile.getParentFile().mkdirs();
272+
273+
// Create a simple zip file using Apache Commons Compress
274+
try (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos =
275+
new org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream(zipFile)) {
276+
org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry =
277+
new org.apache.commons.compress.archivers.zip.ZipArchiveEntry("test-file.txt");
278+
// Set modification time to -1 to simulate unspecified modification time
279+
entry.setTime(-1);
280+
zos.putArchiveEntry(entry);
281+
zos.write("Test content".getBytes());
282+
zos.closeArchiveEntry();
283+
}
284+
285+
// Now try to extract it - this should not throw an IllegalArgumentException
286+
File outputDirectory = new File("target/output/zip-negative-time-extract");
287+
FileUtils.deleteDirectory(outputDirectory);
288+
outputDirectory.mkdirs();
289+
290+
ZipUnArchiver zu = getZipUnArchiver(zipFile);
291+
zu.extract("", outputDirectory);
292+
293+
// Verify the file was extracted
294+
File extractedFile = new File(outputDirectory, "test-file.txt");
295+
assertTrue(extractedFile.exists());
296+
assertEquals("Test content", new String(java.nio.file.Files.readAllBytes(extractedFile.toPath())));
297+
}
266298
}

0 commit comments

Comments
 (0)