This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push: new f6b23c072 [COMPRESS-695] Ability to use different InputStreams for Zstd (#649) f6b23c072 is described below commit f6b23c07269a027429114ddd2fe7babab4b3ebc7 Author: mehmet-karaman <mehmet.kara...@advantest.com> AuthorDate: Fri Mar 21 01:03:24 2025 +0100 [COMPRESS-695] Ability to use different InputStreams for Zstd (#649) * [COMPRESS-695] Ability to use different InputStreams for Zstd * Spaces, no tabs * Javadoc - Fix throws tag - rename odd parameter * Remove unused import --------- Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com> --- pom.xml | 6 +++ .../archivers/zip/ZipArchiveInputStream.java | 14 +++++- .../archivers/zip/ZipArchiveInputStreamTest.java | 52 ++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6b42da93a..b4ea45120 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,12 @@ Brotli, Zstandard and ar, cpio, jar, tar, zip, dump, 7z, arj. <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>io.airlift</groupId> + <artifactId>aircompressor</artifactId> + <version>0.26</version> + <scope>test</scope> + </dependency> <dependency> <groupId>com.github.luben</groupId> <artifactId>zstd-jni</artifactId> diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index 138f396a4..7e6acfe83 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -773,7 +773,7 @@ public ZipArchiveEntry getNextZipEntry() throws IOException { break; case ZSTD: case ZSTD_DEPRECATED: - current.inputStream = new ZstdCompressorInputStream(bis); + current.inputStream = createZstdInputStream(bis); break; default: // we should never get here as all supported methods have been covered @@ -790,6 +790,18 @@ public ZipArchiveEntry getNextZipEntry() throws IOException { return current.entry; } + /** + * Creates the appropriate InputStream for the ZSTD compression method. + * + * @param in the input stream which should be used for compression. + * @return the {@link InputStream} for handling the Zstd compression. + * @throws IOException if an I/O error occurs. + * @since 1.28.0 + */ + protected InputStream createZstdInputStream(final InputStream in) throws IOException { + return new ZstdCompressorInputStream(in); + } + /** * Gets the uncompressed count. * diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java index 39cf291b6..f289fba26 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java @@ -58,8 +58,34 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import io.airlift.compress.zstd.ZstdInputStream; + public class ZipArchiveInputStreamTest extends AbstractTest { + private final class ZipArchiveInputStreamAltZstd extends ZipArchiveInputStream { + + boolean used; + + private ZipArchiveInputStreamAltZstd(InputStream inputStream) { + super(inputStream); + } + + @Override + protected InputStream createZstdInputStream(InputStream bis) throws IOException { + return new ZstdInputStream(bis) { + @Override + public int read(byte[] outputBuffer, int outputOffset, int outputLength) throws IOException { + used = true; + return super.read(outputBuffer, outputOffset, outputLength); + } + }; + } + + public boolean isUsed() { + return used; + } + } + private static void nameSource(final String archive, final String entry, int entryNo, final ZipArchiveEntry.NameSource expected) throws Exception { try (ZipArchiveInputStream zis = new ZipArchiveInputStream(Files.newInputStream(getFile(archive).toPath()))) { ZipArchiveEntry ze; @@ -317,6 +343,32 @@ public void testProperlyReadsStoredEntries() throws IOException { } } + @Test + public void testAlternativeZstdImplementation() throws IOException { + try (InputStream fs = newInputStream("COMPRESS-692/compress-692.zip"); + ZipArchiveInputStreamAltZstd archive = new ZipArchiveInputStreamAltZstd(fs)) { + assertFalse(archive.isUsed()); + ZipArchiveEntry e = archive.getNextEntry(); + assertNotNull(e); + assertEquals(ZipMethod.ZSTD.getCode(), e.getMethod()); + assertEquals("dolor.txt", e.getName()); + assertEquals(635, e.getCompressedSize()); + assertEquals(6066, e.getSize()); + byte[] data = IOUtils.toByteArray(archive); + assertEquals(6066, data.length); + assertTrue(archive.isUsed()); + e = archive.getNextEntry(); + assertNotNull(e); + assertEquals(ZipMethod.ZSTD.getCode(), e.getMethod()); + assertEquals("ipsum.txt", e.getName()); + assertEquals(636, e.getCompressedSize()); + assertEquals(6072, e.getSize()); + data = IOUtils.toByteArray(archive); + assertEquals(6072, data.length); + assertNotNull(archive.getNextEntry()); + } + } + @Test public void testProperlyReadsStoredEntryWithDataDescriptorWithoutSignature() throws IOException { try (InputStream fs = newInputStream("bla-stored-dd-nosig.zip");