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
commit c974e4058664a99533ed931e451f1f7f683ac3f0 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Apr 9 10:13:44 2025 -0400 Sort members --- .../commons/compress/archivers/zip/ZipFile.java | 58 +++++++++++----------- .../compress/archivers/zip/ZipFileTest.java | 58 +++++++++++----------- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java index 54bbaa085..515b57759 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java @@ -172,22 +172,6 @@ public ZipFile get() throws IOException { zstdInputStreamFactory); } - /** - * Sets the factory {@link IOFunction} to create a Zstd {@link InputStream}. Defaults to - * {@link ZstdCompressorInputStream#ZstdCompressorInputStream(InputStream)}. - * <p> - * Call this method to plugin an alternate Zstd input stream implementation. - * </p> - * - * @param zstdInpStreamFactory the factory {@link IOFunction} to create a Zstd {@link InputStream}; {@code null} resets to the default. - * @return {@code this} instance. - * @since 1.28.0 - */ - public Builder setZstdInputStreamFactory(final IOFunction<InputStream, InputStream> zstdInpStreamFactory) { - this.zstdInputStreamFactory = zstdInpStreamFactory; - return this; - } - /** * Sets whether to ignore information stored inside the local file header. * @@ -232,6 +216,22 @@ public Builder setUseUnicodeExtraFields(final boolean useUnicodeExtraFields) { return this; } + /** + * Sets the factory {@link IOFunction} to create a Zstd {@link InputStream}. Defaults to + * {@link ZstdCompressorInputStream#ZstdCompressorInputStream(InputStream)}. + * <p> + * Call this method to plugin an alternate Zstd input stream implementation. + * </p> + * + * @param zstdInpStreamFactory the factory {@link IOFunction} to create a Zstd {@link InputStream}; {@code null} resets to the default. + * @return {@code this} instance. + * @since 1.28.0 + */ + public Builder setZstdInputStreamFactory(final IOFunction<InputStream, InputStream> zstdInpStreamFactory) { + this.zstdInputStreamFactory = zstdInpStreamFactory; + return this; + } + } /** @@ -1074,6 +1074,19 @@ private BoundedArchiveInputStream createBoundedInputStream(final long start, fin : new BoundedSeekableByteChannelInputStream(start, remaining, archive); } + /** + * Creates an 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. + */ + @SuppressWarnings("resource") + InputStream createZstdInputStream(final InputStream in) throws IOException { + // This method is the only location that references ZstdCompressorInputStream directly to avoid requiring the JAR for all use cases. + return zstdInputStreamFactory != null ? zstdInputStreamFactory.apply(in) : new ZstdCompressorInputStream(in); + } + private void fillNameMap() { entries.forEach(ze -> { // entries are filled in populateFromCentralDirectory and @@ -1275,19 +1288,6 @@ public void close() throws IOException { } } - /** - * Creates an 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. - */ - @SuppressWarnings("resource") - InputStream createZstdInputStream(final InputStream in) throws IOException { - // This method is the only location that references ZstdCompressorInputStream directly to avoid requiring the JAR for all use cases. - return zstdInputStreamFactory != null ? zstdInputStreamFactory.apply(in) : new ZstdCompressorInputStream(in); - } - /** * Gets the raw stream of the archive entry (compressed form). * <p> diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java index ebc4b7c0d..80d1d8dbe 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java @@ -236,6 +236,35 @@ public void tearDownClose() { ZipFile.closeQuietly(zf); } + @Test + public void testAlternativeZstdInputStream() throws Exception { + final File archive = getFile("COMPRESS-692/compress-692.zip"); + try (AirliftZstdZipFile zf = new AirliftZstdZipFile(archive)) { + final byte[] buffer = new byte[7000]; + final ZipArchiveEntry ze = zf.getEntry("dolor.txt"); + assertNotNull(ze); + try (InputStream inputStream = zf.getInputStream(ze)) { + assertNotNull(inputStream); + assertFalse(zf.isUsed()); + final int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(inputStream, buffer); + assertEquals(6066, bytesRead); + assertTrue(zf.isUsed()); + } + } + + try (ZipFile builtZipFile = ZipFile.builder().setPath(archive.getAbsolutePath()).setZstdInputStreamFactory(ZstdInputStream::new).get()) { + final byte[] buffer = new byte[7000]; + final ZipArchiveEntry ze = builtZipFile.getEntry("dolor.txt"); + assertNotNull(ze); + try (InputStream inputStream = builtZipFile.getInputStream(ze)) { + assertTrue(inputStream instanceof ZstdInputStream); + assertNotNull(inputStream); + final int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(inputStream, buffer); + assertEquals(6066, bytesRead); + } + } + } + @Test public void testCDOrder() throws Exception { readOrderTest(); @@ -410,35 +439,6 @@ public void testDuplicateEntry() throws Exception { } } - @Test - public void testAlternativeZstdInputStream() throws Exception { - final File archive = getFile("COMPRESS-692/compress-692.zip"); - try (AirliftZstdZipFile zf = new AirliftZstdZipFile(archive)) { - final byte[] buffer = new byte[7000]; - final ZipArchiveEntry ze = zf.getEntry("dolor.txt"); - assertNotNull(ze); - try (InputStream inputStream = zf.getInputStream(ze)) { - assertNotNull(inputStream); - assertFalse(zf.isUsed()); - final int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(inputStream, buffer); - assertEquals(6066, bytesRead); - assertTrue(zf.isUsed()); - } - } - - try (ZipFile builtZipFile = ZipFile.builder().setPath(archive.getAbsolutePath()).setZstdInputStreamFactory(ZstdInputStream::new).get()) { - final byte[] buffer = new byte[7000]; - final ZipArchiveEntry ze = builtZipFile.getEntry("dolor.txt"); - assertNotNull(ze); - try (InputStream inputStream = builtZipFile.getInputStream(ze)) { - assertTrue(inputStream instanceof ZstdInputStream); - assertNotNull(inputStream); - final int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(inputStream, buffer); - assertEquals(6066, bytesRead); - } - } - } - /** * Test entries alignment. */