From 10081938511fffc1c7d494aa27d5d4a443bff95c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 5 Oct 2025 13:59:06 +0000 Subject: [PATCH] Fix handling of zip entries with unspecified modification time (-1) --- .../archiver/zip/AbstractZipUnArchiver.java | 8 +++-- .../archiver/zip/ZipUnArchiverTest.java | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipUnArchiver.java b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipUnArchiver.java index c2f3f550..0ecf65fe 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipUnArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipUnArchiver.java @@ -117,7 +117,7 @@ public InputStream getContents() throws IOException { @Override public long getLastModified() { final long l = zipEntry.getTime(); - return l == 0 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l; + return l <= 0 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l; } @Override @@ -174,12 +174,16 @@ protected void execute(final String path, final File outputDirectory) throws Arc .setInputStream(in) .setMaxCount(remainingSpace + 1) .get(); + long time = ze.getTime(); + if (time <= 0) { + time = PlexusIoResource.UNKNOWN_MODIFICATION_DATE; + } extractFile( getSourceFile(), outputDirectory, bis, ze.getName(), - new Date(ze.getTime()), + new Date(time), ze.isDirectory(), ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zipFile, ze), diff --git a/src/test/java/org/codehaus/plexus/archiver/zip/ZipUnArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/zip/ZipUnArchiverTest.java index 222661a0..e86953d0 100644 --- a/src/test/java/org/codehaus/plexus/archiver/zip/ZipUnArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/zip/ZipUnArchiverTest.java @@ -263,4 +263,36 @@ private ZipArchiver getZipArchiver(File destFile) { zipArchiver.setDestFile(destFile); return zipArchiver; } + + @Test + void testZipWithNegativeModificationTime() throws Exception { + // Create a zip file with an entry that has -1 modification time + File zipFile = new File("target/output/zip-with-negative-time.zip"); + zipFile.getParentFile().mkdirs(); + + // Create a simple zip file using Apache Commons Compress + try (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos = + new org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream(zipFile)) { + org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry = + new org.apache.commons.compress.archivers.zip.ZipArchiveEntry("test-file.txt"); + // Set modification time to -1 to simulate unspecified modification time + entry.setTime(-1); + zos.putArchiveEntry(entry); + zos.write("Test content".getBytes()); + zos.closeArchiveEntry(); + } + + // Now try to extract it - this should not throw an IllegalArgumentException + File outputDirectory = new File("target/output/zip-negative-time-extract"); + FileUtils.deleteDirectory(outputDirectory); + outputDirectory.mkdirs(); + + ZipUnArchiver zu = getZipUnArchiver(zipFile); + zu.extract("", outputDirectory); + + // Verify the file was extracted + File extractedFile = new File(outputDirectory, "test-file.txt"); + assertTrue(extractedFile.exists()); + assertEquals("Test content", new String(java.nio.file.Files.readAllBytes(extractedFile.toPath()))); + } }