This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch feat/archive-builders in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 4aa829245a420ee5f23b808590ead9d76a136d35 Author: Piotr P. Karwasz <pkarwasz-git...@apache.org> AuthorDate: Fri Sep 12 20:18:43 2025 +0200 feat: add builder for `JarArchiveInputStream` and `ZipArchiveInputStream` Introduce a builder for `JarArchiveInputStream` and `ZipArchiveInputStream` and refactor existing constructor calls to use it. --- .../compress/archivers/ArchiveStreamFactory.java | 11 +- .../archivers/jar/JarArchiveInputStream.java | 35 ++++ .../archivers/zip/ZipArchiveInputStream.java | 173 ++++++++++++++++++-- .../apache/commons/compress/archivers/ZipTest.java | 14 +- ...fd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java | 12 +- .../archivers/zip/EncryptedArchiveTest.java | 11 +- .../compress/archivers/zip/ExplodeSupportTest.java | 4 +- .../commons/compress/archivers/zip/Lister.java | 19 ++- .../archivers/zip/Maven221MultiVolumeTest.java | 7 +- .../compress/archivers/zip/UTF8ZipFilesTest.java | 34 ++-- .../compress/archivers/zip/Zip64SupportIT.java | 8 +- .../zip/ZipArchiveInputStreamMalformedTest.java | 7 +- .../archivers/zip/ZipArchiveInputStreamTest.java | 182 +++++++++++++-------- 13 files changed, 375 insertions(+), 142 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java index d80a2766f..7dd47f7a7 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java +++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java @@ -440,10 +440,11 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp return (I) arjBuilder.get(); } if (ZIP.equalsIgnoreCase(archiverName)) { + final ZipArchiveInputStream.Builder zipBuilder = ZipArchiveInputStream.builder().setInputStream(in); if (actualEncoding != null) { - return (I) new ZipArchiveInputStream(in, actualEncoding); + zipBuilder.setCharset(actualEncoding); } - return (I) new ZipArchiveInputStream(in); + return (I) zipBuilder.get(); } if (TAR.equalsIgnoreCase(archiverName)) { if (actualEncoding != null) { @@ -452,10 +453,12 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp return (I) new TarArchiveInputStream(in); } if (JAR.equalsIgnoreCase(archiverName) || APK.equalsIgnoreCase(archiverName)) { + final JarArchiveInputStream.Builder jarBuilder = + JarArchiveInputStream.jarInputStreamBuilder().setInputStream(in); if (actualEncoding != null) { - return (I) new JarArchiveInputStream(in, actualEncoding); + jarBuilder.setCharset(actualEncoding); } - return (I) new JarArchiveInputStream(in); + return (I) jarBuilder.get(); } if (CPIO.equalsIgnoreCase(archiverName)) { final CpioArchiveInputStream.Builder cpioBuilder = CpioArchiveInputStream.builder().setInputStream(in); diff --git a/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java index f8d9d180e..14ba94c01 100644 --- a/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java @@ -31,6 +31,27 @@ */ public class JarArchiveInputStream extends ZipArchiveInputStream { + /** + * Builds a new {@link JarArchiveInputStream}. + * <p> + * For example: + * </p> + * <pre>{@code + * JarArchiveInputStream in = JarArchiveInputStream.builder() + * .setPath(inputPath) + * .setCharset(StandardCharsets.UTF_8) + * .setUseUnicodeExtraFields(false) + * .get(); + * }</pre> + * @since 1.29.0 + */ + public static class Builder extends ZipArchiveInputStream.AbstractBuilder<JarArchiveInputStream, Builder> { + @Override + public JarArchiveInputStream get() throws IOException { + return new JarArchiveInputStream(this); + } + } + /** * Checks if the signature matches what is expected for a jar file (in this case it is the same as for a ZIP file). * @@ -42,6 +63,16 @@ public static boolean matches(final byte[] signature, final int length) { return ZipArchiveInputStream.matches(signature, length); } + /** + * Creates a new builder. + * + * @return A new builder + * @since 1.29.0 + */ + public static Builder jarInputStreamBuilder() { + return new Builder(); + } + /** * Creates an instance from the input stream using the default encoding. * @@ -62,6 +93,10 @@ public JarArchiveInputStream(final InputStream inputStream, final String encodin super(inputStream, encoding); } + private JarArchiveInputStream(final Builder builder) throws IOException { + super(builder); + } + @Override public JarArchiveEntry getNextEntry() throws IOException { return getNextJarEntry(); 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 ea7544772..e5b6bd8b0 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 @@ -77,6 +77,112 @@ */ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> implements InputStreamStatistics { + /** + * Abstract builder for derived classes of {@link ZipArchiveInputStream}. + * + * @param <T> The type of the {@link ZipArchiveInputStream}. + * @param <B> The type of the builder itself. + * @since 1.29.0 + */ + public abstract static class AbstractBuilder<T extends ZipArchiveInputStream, B extends AbstractBuilder<T, B>> + extends ArchiveInputStream.Builder<T, B> { + private boolean useUnicodeExtraFields = true; + private boolean allowStoredEntriesWithDataDescriptor; + private boolean skipSplitSig; + + protected AbstractBuilder() { + setCharset(StandardCharsets.UTF_8); + } + + /** + * Controls whether to use InfoZIP Unicode Extra Fields (if present) to set the file names. + * + * <p>This feature is enabled by default.</p> + * + * @param useUnicodeExtraFields If {@code true} Unicode Extra Fields should be used. + * @return this + */ + public B setUseUnicodeExtraFields(final boolean useUnicodeExtraFields) { + this.useUnicodeExtraFields = useUnicodeExtraFields; + return asThis(); + } + + protected boolean isUseUnicodeExtraFields() { + return useUnicodeExtraFields; + } + + /** + * Controls whether the stream attempts to read STORED entries that use a data descriptor. + * + * <p>If set to {@code true}, the stream will not stop reading an entry at the + * declared compressed size. Instead, it will continue until a data descriptor + * is encountered (by detecting the Data Descriptor Signature). This may cause + * issues in certain cases, such as JARs embedded in WAR files.</p> + * + * <p>See <a href="https://issues.apache.org/jira/browse/COMPRESS-555">COMPRESS-555</a> + * for details.</p> + * + * <p>This feature is disabled by default.</p> + * + * @param allowStoredEntriesWithDataDescriptor {@code true} to read STORED entries with data descriptors, + * {@code false} to stop at the compressed size. + * @return this + */ + + public B setAllowStoredEntriesWithDataDescriptor(final boolean allowStoredEntriesWithDataDescriptor) { + this.allowStoredEntriesWithDataDescriptor = allowStoredEntriesWithDataDescriptor; + return asThis(); + } + + protected boolean isAllowStoredEntriesWithDataDescriptor() { + return allowStoredEntriesWithDataDescriptor; + } + + /** + * Configures whether the stream should skip the ZIP split signature + * ({@code 08074B50}) at the beginning of the input. + * + * <p>Disabled by default.</p> + * + * @param skipSplitSig {@code true} to skip the ZIP split signature, {@code false} otherwise + * @return this + */ + public B setSkipSplitSig(final boolean skipSplitSig) { + this.skipSplitSig = skipSplitSig; + return asThis(); + } + + protected boolean isSkipSplitSig() { + return skipSplitSig; + } + } + + /** + * Builds a new {@link ZipArchiveInputStream}. + * <p> + * For example: + * </p> + * <pre>{@code + * ZipArchiveInputStream in = ZipArchiveInputStream.builder() + * .setPath(inputPath) + * .setCharset(StandardCharsets.UTF_8) + * .setUseUnicodeExtraFields(false) + * .get(); + * }</pre> + * @since 1.29.0 + */ + public static final class Builder extends AbstractBuilder<ZipArchiveInputStream, Builder> { + + private Builder() { + // empty + } + + @Override + public ZipArchiveInputStream get() throws IOException { + return new ZipArchiveInputStream(this); + } + } + /** * Input stream adapted from commons-io. */ @@ -220,6 +326,16 @@ public static boolean matches(final byte[] buffer, final int length) { || ArrayUtils.startsWith(buffer, ZipLong.SINGLE_SEGMENT_SPLIT_MARKER.getBytes()); } + /** + * Creates a new builder. + * + * @return A new builder + * @since 1.29.0 + */ + public static Builder builder() { + return new Builder(); + } + /** The ZIP encoding to use for file names and the file comment. */ private final ZipEncoding zipEncoding; /** Whether to look for and use Unicode extra fields. */ @@ -270,7 +386,7 @@ public static boolean matches(final byte[] buffer, final int length) { * @param inputStream the stream to wrap */ public ZipArchiveInputStream(final InputStream inputStream) { - this(inputStream, StandardCharsets.UTF_8.name()); + this(inputStream, builder()); } /** @@ -281,7 +397,7 @@ public ZipArchiveInputStream(final InputStream inputStream) { * @since 1.5 */ public ZipArchiveInputStream(final InputStream inputStream, final String encoding) { - this(inputStream, encoding, true); + this(inputStream, builder().setCharset(encoding)); } /** @@ -291,8 +407,9 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin * @param encoding the encoding to use for file names, use null for the platform's default encoding * @param useUnicodeExtraFields whether to use InfoZIP Unicode Extra Fields (if present) to set the file names. */ - public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields) { - this(inputStream, encoding, useUnicodeExtraFields, false); + public ZipArchiveInputStream( + final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields) { + this(inputStream, builder().setCharset(encoding).setUseUnicodeExtraFields(useUnicodeExtraFields)); } /** @@ -304,9 +421,17 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin * @param allowStoredEntriesWithDataDescriptor whether the stream will try to read STORED entries that use a data descriptor * @since 1.1 */ - public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields, + public ZipArchiveInputStream( + final InputStream inputStream, + final String encoding, + final boolean useUnicodeExtraFields, final boolean allowStoredEntriesWithDataDescriptor) { - this(inputStream, encoding, useUnicodeExtraFields, allowStoredEntriesWithDataDescriptor, false); + this( + inputStream, + builder() + .setCharset(encoding) + .setUseUnicodeExtraFields(useUnicodeExtraFields) + .setAllowStoredEntriesWithDataDescriptor(allowStoredEntriesWithDataDescriptor)); } /** @@ -320,14 +445,32 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin * this to true if you want to read a split archive. * @since 1.20 */ - public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields, - final boolean allowStoredEntriesWithDataDescriptor, final boolean skipSplitSig) { - super(inputStream, encoding); + public ZipArchiveInputStream( + final InputStream inputStream, + final String encoding, + final boolean useUnicodeExtraFields, + final boolean allowStoredEntriesWithDataDescriptor, + final boolean skipSplitSig) { + this( + inputStream, + builder() + .setCharset(encoding) + .setUseUnicodeExtraFields(useUnicodeExtraFields) + .setAllowStoredEntriesWithDataDescriptor(allowStoredEntriesWithDataDescriptor) + .setSkipSplitSig(skipSplitSig)); + } + + protected ZipArchiveInputStream(final AbstractBuilder<?, ?> builder) throws IOException { + this(builder.getInputStream(), builder); + } + + private ZipArchiveInputStream(final InputStream inputStream, final AbstractBuilder<?, ?> builder) { + super(inputStream, builder.getCharset()); this.in = new PushbackInputStream(inputStream, buf.capacity()); - this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); - this.useUnicodeExtraFields = useUnicodeExtraFields; - this.allowStoredEntriesWithDataDescriptor = allowStoredEntriesWithDataDescriptor; - this.skipSplitSig = skipSplitSig; + this.zipEncoding = ZipEncodingHelper.getZipEncoding(builder.getCharset()); + this.useUnicodeExtraFields = builder.isUseUnicodeExtraFields(); + this.allowStoredEntriesWithDataDescriptor = builder.isAllowStoredEntriesWithDataDescriptor(); + this.skipSplitSig = builder.isSkipSplitSig(); // haven't read anything so far buf.limit(0); } @@ -339,7 +482,9 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin * If it contains such a signature, reads the data descriptor and positions the stream right after the data descriptor. * </p> */ - private boolean bufferContainsSignature(final ByteArrayOutputStream bos, final int offset, final int lastRead, final int expectedDDLen) throws IOException { + private boolean bufferContainsSignature( + final ByteArrayOutputStream bos, final int offset, final int lastRead, final int expectedDDLen) + throws IOException { boolean done = false; for (int i = 0; !done && i < offset + lastRead - 4; i++) { if (buf.array()[i] == LFH[0] && buf.array()[i + 1] == LFH[1]) { diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java index 9e54b5452..df2071d66 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java @@ -18,7 +18,6 @@ */ package org.apache.commons.compress.archivers; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -353,7 +352,10 @@ void testBuildSplitZipTest() throws IOException { final File lastFile = newTempFile("splitZip.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, UTF_8.toString(), true, false, true)) { + ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder() + .setInputStream(inputStream) + .setSkipSplitSig(true) + .get()) { ArchiveEntry entry; final int filesNum = countNonDirectories(directoryToZip); @@ -653,7 +655,7 @@ void testListAllFilesWithNestedArchive() throws Exception { */ @Test void testSkipEntryWithUnsupportedCompressionMethod() throws IOException { - try (ZipArchiveInputStream zip = new ZipArchiveInputStream(newInputStream("moby.zip"))) { + try (ZipArchiveInputStream zip = ZipArchiveInputStream.builder().setURI(getURI("moby.zip")).get()) { final ZipArchiveEntry entry = zip.getNextZipEntry(); assertEquals(ZipMethod.TOKENIZATION.getCode(), entry.getMethod(), "method"); assertEquals("README", entry.getName()); @@ -667,12 +669,12 @@ void testSkipEntryWithUnsupportedCompressionMethod() throws IOException { */ @Test void testSkipsPK00Prefix() throws Exception { - final File input = getFile("COMPRESS-208.zip"); final ArrayList<String> al = new ArrayList<>(); al.add("test1.xml"); al.add("test2.xml"); - try (InputStream fis = Files.newInputStream(input.toPath()); - ZipArchiveInputStream inputStream = new ZipArchiveInputStream(fis)) { + try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder() + .setURI(getURI("COMPRESS-208.zip")) + .get()) { checkArchiveContent(inputStream, al); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java b/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java index b2faabd64..f96f0f1ba 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java @@ -20,25 +20,23 @@ import static org.junit.jupiter.api.Assertions.assertThrows; -import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; +import org.apache.commons.compress.AbstractTest; import org.apache.commons.compress.archivers.ArchiveEntry; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; /** * Tests https://issues.apache.org/jira/browse/COMPRESS-598 */ -class Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test { +class Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test extends AbstractTest { @Test void test() throws IOException { - final byte[] input = FileUtils - .readFileToByteArray(new File("src/test/resources/org/apache/commons/compress/fuzz/crash-f2efd9eaeb86cda597d07b5e3c3d81363633c2da")); - try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new ByteArrayInputStream(input))) { + try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder() + .setURI(getURI("org/apache/commons/compress/fuzz/crash-f2efd9eaeb86cda597d07b5e3c3d81363633c2da")) + .get()) { assertThrows(IOException.class, () -> { for (;;) { final ArchiveEntry zipEntry = zis.getNextEntry(); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java index 888cefdad..9fb7b9aad 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java @@ -19,16 +19,14 @@ package org.apache.commons.compress.archivers.zip; -import static org.apache.commons.compress.AbstractTest.getFile; +import static org.apache.commons.compress.AbstractTest.getURI; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.File; import java.io.IOException; -import java.nio.file.Files; import org.junit.jupiter.api.Test; @@ -36,8 +34,9 @@ class EncryptedArchiveTest { @Test void testReadPasswordEncryptedEntryViaStream() throws IOException { - final File file = getFile("password-encrypted.zip"); - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(file.toPath()))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder() + .setURI(getURI("password-encrypted.zip")) + .get()) { final ZipArchiveEntry zae = zin.getNextZipEntry(); assertEquals("LICENSE.txt", zae.getName()); assertTrue(zae.getGeneralPurposeBit().usesEncryption()); @@ -53,7 +52,7 @@ void testReadPasswordEncryptedEntryViaStream() throws IOException { @Test void testReadPasswordEncryptedEntryViaZipFile() throws IOException { - try (ZipFile zf = ZipFile.builder().setFile(getFile("password-encrypted.zip")).get()) { + try (ZipFile zf = ZipFile.builder().setURI(getURI("password-encrypted.zip")).get()) { final ZipArchiveEntry zae = zf.getEntry("LICENSE.txt"); assertTrue(zae.getGeneralPurposeBit().usesEncryption()); assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption()); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java index 60451e946..ff9ea106a 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java @@ -25,10 +25,8 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; @@ -85,7 +83,7 @@ void testTikaTestStream() throws IOException { } private void testZipStreamWithImplodeCompression(final String fileName, final String entryName) throws IOException { - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(new File(fileName).toPath()))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setFile(fileName).get()) { final ZipArchiveEntry entry = zin.getNextZipEntry(); assertEquals(entryName, entry.getName(), "entry name"); assertTrue(zin.canReadEntryData(entry), "entry can't be read"); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java b/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java index 07e2f90f9..e3ead9ae4 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java @@ -19,7 +19,6 @@ package org.apache.commons.compress.archivers.zip; -import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -70,14 +69,16 @@ public static void main(final String[] args) throws IOException { usage(); } if (cl.useStream) { - try (BufferedInputStream fs = new BufferedInputStream(Files.newInputStream(f.toPath()))) { - final ZipArchiveInputStream zs = new ZipArchiveInputStream(fs, cl.encoding, true, cl.allowStoredEntriesWithDataDescriptor); - for (ArchiveEntry entry = zs.getNextEntry(); entry != null; entry = zs.getNextEntry()) { - final ZipArchiveEntry ze = (ZipArchiveEntry) entry; - list(ze); - if (cl.dir != null) { - extract(cl.dir, ze, zs); - } + final ZipArchiveInputStream zs = ZipArchiveInputStream.builder() + .setFile(f) + .setCharset(cl.encoding) + .setAllowStoredEntriesWithDataDescriptor(cl.allowStoredEntriesWithDataDescriptor) + .get(); + for (ArchiveEntry entry = zs.getNextEntry(); entry != null; entry = zs.getNextEntry()) { + final ZipArchiveEntry ze = (ZipArchiveEntry) entry; + list(ze); + if (cl.dir != null) { + extract(cl.dir, ze, zs); } } } else { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java index 48b37731b..fa9dd532c 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java @@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; -import java.io.InputStream; import org.apache.commons.compress.AbstractTest; import org.apache.commons.compress.archivers.ArchiveEntry; @@ -71,8 +70,10 @@ void testRead7ZipMultiVolumeArchiveForFile() { @Test void testRead7ZipMultiVolumeArchiveForStream() throws IOException { - try (InputStream archive = newInputStream("apache-maven-2.2.1.zip.001"); - ZipArchiveInputStream zi = new ZipArchiveInputStream(archive, null, false)) { + try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder() + .setURI(getURI("apache-maven-2.2.1.zip.001")) + .setUseUnicodeExtraFields(false) + .get()) { // these are the entries that are supposed to be processed correctly without any problems. for (final String element : ENTRIES) { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java index b028a2d29..aceae288b 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java @@ -31,7 +31,6 @@ import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.util.zip.CRC32; import org.apache.commons.compress.AbstractTest; @@ -178,7 +177,11 @@ private void testFileRoundtrip(final String encoding, final boolean withEFS, fin @Test void testRawNameReadFromStream() throws IOException { - try (ZipArchiveInputStream zi = new ZipArchiveInputStream(newInputStream("utf8-7zip-test.zip"), CP437, false)) { + try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder() + .setURI(getURI("utf8-7zip-test.zip")) + .setCharset(CP437) + .setUseUnicodeExtraFields(false) + .get()) { assertRawNameOfAcsiiTxt(zi.getNextEntry()); } } @@ -208,7 +211,11 @@ void testRead7ZipArchive() throws IOException { @Test void testRead7ZipArchiveForStream() throws IOException { - try (ZipArchiveInputStream zi = new ZipArchiveInputStream(newInputStream("utf8-7zip-test.zip"), CP437, false)) { + try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder() + .setURI(getURI("utf8-7zip-test.zip")) + .setCharset(CP437) + .setUseUnicodeExtraFields(false) + .get()) { assertEquals(ASCII_TXT, zi.getNextEntry().getName()); assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName()); assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName()); @@ -235,13 +242,10 @@ void testReadWinZipArchive() throws IOException { @Test void testReadWinZipArchiveForStream() throws IOException { - // fix for test fails on Windows with default charset that is not UTF-8 - String encoding = null; - if (Charset.defaultCharset() != UTF_8) { - encoding = UTF_8.name(); - } - try (InputStream archive = newInputStream("utf8-winzip-test.zip"); - ZipArchiveInputStream zi = new ZipArchiveInputStream(archive, encoding, true)) { + try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder() + .setURI(getURI("utf8-winzip-test.zip")) + .setCharset(UTF_8) + .get()) { assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName()); assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName()); assertEquals(ASCII_TXT, zi.getNextEntry().getName()); @@ -253,8 +257,9 @@ void testReadWinZipArchiveForStream() throws IOException { */ @Test void testStreamSkipsOverUnicodeExtraFieldWithUnsupportedVersion() throws IOException { - try (InputStream archive = newInputStream("COMPRESS-479.zip"); - ZipArchiveInputStream zi = new ZipArchiveInputStream(archive)) { + try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder() + .setURI(getURI("COMPRESS-479.zip")) + .get()) { assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName()); assertEquals("%U20AC_for_Dollar.txt", zi.getNextEntry().getName()); assertEquals(ASCII_TXT, zi.getNextEntry().getName()); @@ -304,7 +309,10 @@ void testZipArchiveInputStreamReadsUnicodeFields() throws IOException { void testZipFileReadsUnicodeFields() throws IOException { final File file = createTempFile("unicode-test", ".zip"); createTestFile(file, StandardCharsets.US_ASCII.name(), false, true); - try (ZipArchiveInputStream zi = new ZipArchiveInputStream(Files.newInputStream(file.toPath()), StandardCharsets.US_ASCII.name(), true)) { + try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder() + .setFile(file) + .setCharset(StandardCharsets.US_ASCII) + .get()) { assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName()); assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName()); assertEquals(ASCII_TXT, zi.getNextEntry().getName()); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java b/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java index 43cb2d805..7e070fba3 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java @@ -152,8 +152,8 @@ private static File getTempFile(final String testName) throws Throwable { } private static void read100KFilesImpl(final File f) throws IOException { - try (InputStream fin = Files.newInputStream(f.toPath()); - ZipArchiveInputStream zin = new ZipArchiveInputStream(fin)) { + try (ZipArchiveInputStream zin = + ZipArchiveInputStream.builder().setFile(f).get()) { int files = 0; ZipArchiveEntry zae; while ((zae = zin.getNextZipEntry()) != null) { @@ -182,8 +182,8 @@ private static void read100KFilesUsingZipFileImpl(final File file) throws IOExce } private static void read5GBOfZerosImpl(final File f, final String expectedName) throws IOException { - try (InputStream fin = Files.newInputStream(f.toPath()); - ZipArchiveInputStream zin = new ZipArchiveInputStream(fin)) { + try (ZipArchiveInputStream zin = + ZipArchiveInputStream.builder().setFile(f).get()) { ZipArchiveEntry zae = zin.getNextZipEntry(); while (zae.isDirectory()) { zae = zin.getNextZipEntry(); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java index 5dec0aef9..233fe852d 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java @@ -21,12 +21,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.apache.commons.compress.CompressException; @@ -69,7 +67,10 @@ void test() throws IOException { fos.write(chunk); } } - try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(fixture), StandardCharsets.UTF_8.name(), true, true)) { + try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder() + .setFile(fixture) + .setAllowStoredEntriesWithDataDescriptor(true) + .get()) { zis.getNextEntry(); assertThrows(CompressException.class, () -> IOUtils.toByteArray(zis)); } 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 8fdd0b739..e73cc0304 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 @@ -32,7 +32,6 @@ import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -42,7 +41,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.time.Instant; import java.util.Arrays; import java.util.zip.ZipEntry; @@ -90,7 +88,7 @@ public boolean isUsed() { } 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()))) { + try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder().setURI(getURI(archive)).get()) { ZipArchiveEntry ze; do { ze = zis.getNextZipEntry(); @@ -116,7 +114,9 @@ private void extractZipInputStream(final ZipArchiveInputStream inputStream) thro ZipArchiveEntry zae = inputStream.getNextZipEntry(); while (zae != null) { if (zae.getName().endsWith(".zip")) { - try (ZipArchiveInputStream innerInputStream = new ZipArchiveInputStream(inputStream)) { + try (ZipArchiveInputStream innerInputStream = ZipArchiveInputStream.builder() + .setInputStream(inputStream) + .get()) { extractZipInputStream(innerInputStream); } } @@ -181,8 +181,8 @@ private void getAllZipEntries(final ZipArchiveInputStream zipInputStream) throws private void multiByteReadConsistentlyReturnsMinusOneAtEof(final File file) throws Exception { final byte[] buf = new byte[2]; - try (InputStream in = newInputStream("bla.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) { + try (ZipArchiveInputStream archive = + ZipArchiveInputStream.builder().setFile(file).get()) { assertEquals(-1, archive.getCompressedCount()); assertNotNull(archive.getNextEntry()); IOUtils.toByteArray(archive); @@ -192,8 +192,8 @@ private void multiByteReadConsistentlyReturnsMinusOneAtEof(final File file) thro } private void singleByteReadConsistentlyReturnsMinusOneAtEof(final File file) throws Exception { - try (InputStream in = Files.newInputStream(file.toPath()); - ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) { + try (ZipArchiveInputStream archive = + ZipArchiveInputStream.builder().setFile(file).get()) { assertNotNull(archive.getNextEntry()); IOUtils.toByteArray(archive); assertEquals(-1, archive.read()); @@ -203,8 +203,9 @@ private void singleByteReadConsistentlyReturnsMinusOneAtEof(final File file) thr @Test void testDecompressNextSymbol() throws IOException { - try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream( - Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/zip/decompressNextSymbol.bin")))) { + try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder() + .setURI(getURI("org/apache/commons/compress/zip/decompressNextSymbol.bin")) + .get()) { inputStream.getNextEntry(); assertThrows(IOException.class, inputStream::getNextEntry); } @@ -212,14 +213,14 @@ void testDecompressNextSymbol() throws IOException { @Test void testGetCompressedCountEmptyZip() throws IOException { - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ByteUtils.EMPTY_BYTE_ARRAY).get()) { assertEquals(-1, zin.getCompressedCount()); } } @Test void testGetFirstEntryEmptyZip() throws IOException { - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ByteUtils.EMPTY_BYTE_ARRAY).get()) { final ZipArchiveEntry entry = zin.getNextEntry(); assertNull(entry); } @@ -227,15 +228,16 @@ void testGetFirstEntryEmptyZip() throws IOException { @Test void testGetNextZipEntry() throws IOException { - try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream( - Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/zip/getNextZipEntry.bin")))) { + try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder() + .setURI(getURI("org/apache/commons/compress/zip/getNextZipEntry.bin")) + .get()) { assertThrows(IOException.class, () -> inputStream.forEach(IOConsumer.noop())); } } @Test void testGetUncompressedCountEmptyZip() throws IOException { - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ByteUtils.EMPTY_BYTE_ARRAY).get()) { assertEquals(0, zin.getUncompressedCount()); } } @@ -245,7 +247,7 @@ void testGetUncompressedCountEmptyZip() throws IOException { */ @Test void testMessageWithCorruptFileName() throws Exception { - try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("COMPRESS-351.zip"))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("COMPRESS-351.zip")).get()) { final EOFException ex = assertThrows(EOFException.class, () -> in.forEach(IOConsumer.noop())); final String m = ex.getMessage(); assertTrue(m.startsWith("Truncated ZIP entry: ?2016")); // the first character is not printable @@ -288,8 +290,8 @@ void testMultiByteReadThrowsAtEofForCorruptedStoredEntry() throws Exception { // make size much bigger than entry's real size Arrays.fill(content, 17, 26, (byte) 0xff); final byte[] buf = new byte[2]; - try (ByteArrayInputStream in = new ByteArrayInputStream(content); - ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) { + try (ZipArchiveInputStream archive = + ZipArchiveInputStream.builder().setByteArray(content).get()) { assertNotNull(archive.getNextEntry()); final IOException ex1 = assertThrows(ArchiveException.class, () -> IOUtils.toByteArray(archive), "expected exception"); assertEquals("Truncated ZIP file", ex1.getMessage()); @@ -321,8 +323,8 @@ void testNameSourceIsSetToUnicodeExtraField() throws Exception { @Test void testOffsets() throws Exception { // mixed.zip contains both inflated and stored files - try (InputStream archiveStream = ZipArchiveInputStream.class.getResourceAsStream("/mixed.zip"); - ZipArchiveInputStream zipStream = new ZipArchiveInputStream(archiveStream)) { + try (ZipArchiveInputStream zipStream = + ZipArchiveInputStream.builder().setURI(getURI("mixed.zip")).get()) { final ZipArchiveEntry inflatedEntry = zipStream.getNextZipEntry(); assertEquals("inflated.txt", inflatedEntry.getName()); assertEquals(0x0000, inflatedEntry.getLocalHeaderOffset()); @@ -338,7 +340,9 @@ void testOffsets() throws Exception { @Test void testProperlyMarksEntriesAsUnreadableIfUncompressedSizeIsUnknown() throws Exception { // we never read any data - try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { + try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder() + .setByteArray(ByteUtils.EMPTY_BYTE_ARRAY) + .get()) { final ZipArchiveEntry e = new ZipArchiveEntry("test"); e.setMethod(ZipMethod.DEFLATED.getCode()); assertTrue(zis.canReadEntryData(e)); @@ -351,8 +355,8 @@ void testProperlyMarksEntriesAsUnreadableIfUncompressedSizeIsUnknown() throws Ex @Test void testProperlyReadsStoredEntries() throws IOException { - try (InputStream fs = newInputStream("bla-stored.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs)) { + try (ZipArchiveInputStream archive = + ZipArchiveInputStream.builder().setURI(getURI("bla-stored.zip")).get()) { ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -373,8 +377,10 @@ void testProperlyReadsStoredEntries() throws IOException { @Test void testProperlyReadsStoredEntryWithDataDescriptorWithoutSignature() throws IOException { - try (InputStream fs = newInputStream("bla-stored-dd-nosig.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) { + try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder() + .setURI(getURI("bla-stored-dd-nosig.zip")) + .setAllowStoredEntriesWithDataDescriptor(true) + .get()) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -389,8 +395,10 @@ void testProperlyReadsStoredEntryWithDataDescriptorWithoutSignature() throws IOE @Test void testProperlyReadsStoredEntryWithDataDescriptorWithSignature() throws IOException { - try (InputStream fs = newInputStream("bla-stored-dd.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) { + try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder() + .setURI(getURI("bla-stored-dd.zip")) + .setAllowStoredEntriesWithDataDescriptor(true) + .get()) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -410,7 +418,9 @@ void testProperlyReadsStoredEntryWithDataDescriptorWithSignature() throws IOExce void testProperUseOfInflater() throws Exception { try (ZipFile zf = ZipFile.builder().setFile(getFile("COMPRESS-189.zip")).get()) { final ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP"); - try (ZipArchiveInputStream in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder() + .setInputStream(new BufferedInputStream(zf.getInputStream(zae))) + .get()) { ZipArchiveEntry innerEntry; while ((innerEntry = in.getNextZipEntry()) != null) { if (innerEntry.getName().endsWith("XML")) { @@ -428,7 +438,7 @@ void testProperUseOfInflater() throws Exception { void testReadDeflate64CompressedStream() throws Exception { final byte[] orig = readAllBytes("COMPRESS-380/COMPRESS-380-input"); final File archive = getFile("COMPRESS-380/COMPRESS-380.zip"); - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(archive.toPath()))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setFile(archive).get()) { assertNotNull(zin.getNextZipEntry()); final byte[] fromZip = IOUtils.toByteArray(zin); assertArrayEquals(orig, fromZip); @@ -439,7 +449,7 @@ void testReadDeflate64CompressedStream() throws Exception { void testReadDeflate64CompressedStreamWithDataDescriptor() throws Exception { // this is a copy of bla.jar with META-INF/MANIFEST.MF's method manually changed to ENHANCED_DEFLATED final File archive = getFile("COMPRESS-380/COMPRESS-380-dd.zip"); - try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(archive.toPath()))) { + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setFile(archive).get()) { final ZipArchiveEntry e = zin.getNextZipEntry(); assertEquals(-1, e.getSize()); assertEquals(ZipMethod.ENHANCED_DEFLATED.getCode(), e.getMethod()); @@ -458,7 +468,7 @@ void testReadDeflate64CompressedStreamWithDataDescriptor() throws Exception { @Test void testReadingOfFirstStoredEntry() throws Exception { - try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("COMPRESS-264.zip"))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("COMPRESS-264.zip")).get()) { final ZipArchiveEntry ze = in.getNextZipEntry(); assertEquals(5, ze.getSize()); assertArrayEquals(new byte[] { 'd', 'a', 't', 'a', '\n' }, IOUtils.toByteArray(in)); @@ -467,8 +477,9 @@ void testReadingOfFirstStoredEntry() throws Exception { @Test void testRejectsStoredEntriesWithDataDescriptorByDefault() throws IOException { - try (InputStream fs = newInputStream("bla-stored-dd.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs)) { + try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder() + .setURI(getURI("bla-stored-dd.zip")) + .get()) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -481,7 +492,8 @@ void testRejectsStoredEntriesWithDataDescriptorByDefault() throws IOException { @Test void testShouldConsumeArchiveCompletely() throws Exception { try (InputStream is = ZipArchiveInputStreamTest.class.getResourceAsStream("/archive_with_trailer.zip"); - ZipArchiveInputStream zip = new ZipArchiveInputStream(is)) { + ZipArchiveInputStream zip = + ZipArchiveInputStream.builder().setInputStream(is).get()) { getAllZipEntries(zip); final byte[] expected = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' }; final byte[] actual = new byte[expected.length]; @@ -495,7 +507,7 @@ void testShouldConsumeArchiveCompletely() throws Exception { */ @Test void testShouldReadNestedZip() throws IOException { - try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("COMPRESS-219.zip"))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("COMPRESS-219.zip")).get()) { extractZipInputStream(in); } } @@ -537,8 +549,8 @@ void testSingleByteReadThrowsAtEofForCorruptedStoredEntry() throws Exception { for (int i = 17; i < 26; i++) { content[i] = (byte) 0xff; } - try (ByteArrayInputStream in = new ByteArrayInputStream(content); - ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) { + try (ZipArchiveInputStream archive = + ZipArchiveInputStream.builder().setByteArray(content).get()) { assertNotNull(archive.getNextEntry()); final IOException ex1 = assertThrows(ArchiveException.class, () -> IOUtils.toByteArray(archive), "expected exception"); assertEquals("Truncated ZIP file", ex1.getMessage()); @@ -554,12 +566,16 @@ void testSplitZipCreatedByWinrar() throws IOException { final File lastFile = getFile("COMPRESS-477/split_zip_created_by_winrar/split_zip_created_by_winrar.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, false, true)) { + ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder() + .setInputStream(inputStream) + .setSkipSplitSig(true) + .get()) { final File fileToCompare = getFile("COMPRESS-477/split_zip_created_by_winrar/zip_to_compare_created_by_winrar.zip"); - try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), - StandardCharsets.UTF_8.name(), true, false, true)) { - + try (ZipArchiveInputStream inputStreamToCompare = ZipArchiveInputStream.builder() + .setFile(fileToCompare) + .setSkipSplitSig(true) + .get()) { ArchiveEntry entry; while ((entry = splitInputStream.getNextEntry()) != null && inputStreamToCompare.getNextEntry() != null) { if (entry.isDirectory()) { @@ -576,7 +592,10 @@ void testSplitZipCreatedByZip() throws IOException { final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, false, true)) { + ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder() + .setInputStream(inputStream) + .setSkipSplitSig(true) + .get()) { final Path fileToCompare = getPath("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip.zip"); try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare), StandardCharsets.UTF_8.name(), @@ -598,11 +617,16 @@ void testSplitZipCreatedByZipOfZip64() throws IOException { final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip_zip64.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, false, true)) { + ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder() + .setInputStream(inputStream) + .setSkipSplitSig(true) + .get()) { final Path fileToCompare = getPath("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip_zip64.zip"); - try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare), StandardCharsets.UTF_8.name(), - true, false, true)) { + try (ZipArchiveInputStream inputStreamToCompare = ZipArchiveInputStream.builder() + .setPath(fileToCompare) + .setSkipSplitSig(true) + .get()) { ArchiveEntry entry; while ((entry = splitInputStream.getNextEntry()) != null && inputStreamToCompare.getNextEntry() != null) { @@ -617,9 +641,10 @@ void testSplitZipCreatedByZipOfZip64() throws IOException { @Test void testSplitZipCreatedByZipThrowsException() throws IOException { - final File zipSplitFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01"); - try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(Files.newInputStream(zipSplitFile.toPath()), StandardCharsets.UTF_8.name(), true, - false, true)) { + try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder() + .setURI(getURI("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01")) + .setSkipSplitSig(true) + .get()) { assertThrows(EOFException.class, () -> { ArchiveEntry entry = inputStream.getNextEntry(); @@ -635,7 +660,9 @@ void testSplitZipCreatedByZipThrowsException() throws IOException { */ @Test void testThrowOnInvalidEntry() throws Exception { - try (ZipArchiveInputStream zip = new ZipArchiveInputStream(ZipArchiveInputStreamTest.class.getResourceAsStream("/invalid-zip.zip"))) { + try (ZipArchiveInputStream zip = ZipArchiveInputStream.builder() + .setURI(getURI("invalid-zip.zip")) + .get()) { final ZipException expected = assertThrows(ZipException.class, zip::getNextZipEntry, "IOException expected"); assertTrue(expected.getMessage().contains("Cannot find zip signature")); } @@ -643,8 +670,10 @@ void testThrowOnInvalidEntry() throws Exception { @Test void testThrowsIfStoredDDIsDifferentFromLengthRead() throws IOException { - try (InputStream fs = newInputStream("bla-stored-dd-contradicts-actualsize.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) { + try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder() + .setURI(getURI("bla-stored-dd-contradicts-actualsize.zip")) + .setAllowStoredEntriesWithDataDescriptor(true) + .get()) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -656,8 +685,10 @@ void testThrowsIfStoredDDIsDifferentFromLengthRead() throws IOException { @Test void testThrowsIfStoredDDIsInconsistent() throws IOException { - try (InputStream fs = newInputStream("bla-stored-dd-sizes-differ.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) { + try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder() + .setURI(getURI("bla-stored-dd-sizes-differ.zip")) + .setAllowStoredEntriesWithDataDescriptor(true) + .get()) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -689,15 +720,16 @@ void testThrowsIfZip64ExtraCouldNotBeUnderstood() { @Test void testThrowsIOExceptionIfThereIsCorruptedZip64Extra() throws IOException { - try (InputStream fis = newInputStream("COMPRESS-546.zip"); - ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(fis)) { + try (ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder() + .setURI(getURI("COMPRESS-546.zip")) + .get()) { assertThrows(IOException.class, () -> getAllZipEntries(zipInputStream)); } } @Test void testUnshrinkEntry() throws Exception { - try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("SHRUNK.ZIP"))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("SHRUNK.ZIP")).get()) { ZipArchiveEntry entry = in.getNextZipEntry(); assertEquals(ZipMethod.UNSHRINKING.getCode(), entry.getMethod(), "method"); assertTrue(in.canReadEntryData(entry)); @@ -721,7 +753,7 @@ void testUnshrinkEntry() throws Exception { @Test void testUnzipBZip2CompressedEntry() throws Exception { - try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("bzip2-zip.zip"))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("bzip2-zip.zip")).get()) { final ZipArchiveEntry ze = in.getNextZipEntry(); assertEquals(42, ze.getSize()); final byte[] expected = ArrayFill.fill(new byte[42], (byte) 'a'); @@ -734,7 +766,7 @@ void testUnzipBZip2CompressedEntry() throws Exception { */ @Test void testWinzipBackSlashWorkaround() throws Exception { - try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("test-winzip.zip"))) { + try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("test-winzip.zip")).get()) { ZipArchiveEntry zae = in.getNextZipEntry(); zae = in.getNextZipEntry(); zae = in.getNextZipEntry(); @@ -748,8 +780,9 @@ void testWinzipBackSlashWorkaround() throws Exception { @Test void testWithBytesAfterData() throws Exception { final int expectedNumEntries = 2; - try (InputStream is = ZipArchiveInputStreamTest.class.getResourceAsStream("/archive_with_bytes_after_data.zip"); - ZipArchiveInputStream zip = new ZipArchiveInputStream(is)) { + try (ZipArchiveInputStream zip = ZipArchiveInputStream.builder() + .setURI(getURI("archive_with_bytes_after_data.zip")) + .get()) { int actualNumEntries = 0; ZipArchiveEntry zae = zip.getNextZipEntry(); while (zae != null) { @@ -785,7 +818,9 @@ void testWriteZipWithLinks() throws IOException { assertFalse(zipFile.getEntry("original").isUnixSymlink(), "'original' detected but it's not sym link"); } // Doesn't reads the central directory - try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new FileInputStream("target/zipWithLinks.zip"))) { + try (ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder() + .setFile("target/zipWithLinks.zip") + .get()) { ZipArchiveEntry entry; int entriesCount = 0; while ((entry = zipInputStream.getNextEntry()) != null) { @@ -830,8 +865,11 @@ void testZipArchiveInputStreamSubclassReplacement() throws IOException { @ParameterizedTest @ValueSource(booleans = { true, false }) void testZipInputStream(final boolean allowStoredEntriesWithDataDescriptor) { - try (ZipArchiveInputStream zIn = new ZipArchiveInputStream(Files.newInputStream(Paths.get("src/test/resources/COMPRESS-647/test.zip")), - StandardCharsets.UTF_8.name(), false, allowStoredEntriesWithDataDescriptor)) { + try (ZipArchiveInputStream zIn = ZipArchiveInputStream.builder() + .setURI(getURI("COMPRESS-647/test.zip")) + .setUseUnicodeExtraFields(false) + .setAllowStoredEntriesWithDataDescriptor(allowStoredEntriesWithDataDescriptor) + .get()) { ZipArchiveEntry zae = zIn.getNextEntry(); while (zae != null) { zae = zIn.getNextEntry(); @@ -844,15 +882,19 @@ void testZipInputStream(final boolean allowStoredEntriesWithDataDescriptor) { @Test void testZipUsingStoredWithDDAndNoDDSignature() throws IOException { try (InputStream inputStream = forgeZipInputStream(); - ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, true)) { + ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder() + .setInputStream(inputStream) + .setAllowStoredEntriesWithDataDescriptor(true) + .get()) { getAllZipEntries(zipInputStream); } } @Test void testZipWithBadExtraFields() throws IOException { - try (InputStream fis = newInputStream("COMPRESS-548.zip"); - ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(fis)) { + try (ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder() + .setURI(getURI("COMPRESS-548.zip")) + .get()) { getAllZipEntries(zipInputStream); } } @@ -872,8 +914,8 @@ void testZipWithLongerBeginningGarbage() throws IOException { } } - try (InputStream is = Files.newInputStream(path); - ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) { + try (ZipArchiveInputStream zis = + ZipArchiveInputStream.builder().setPath(path).get()) { final ZipArchiveEntry entry = zis.getNextEntry(); assertEquals("file-1.txt", entry.getName()); final byte[] content = IOUtils.toByteArray(zis); @@ -896,8 +938,8 @@ void testZipWithShortBeginningGarbage() throws IOException { } } - try (InputStream is = Files.newInputStream(path); - ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) { + try (ZipArchiveInputStream zis = + ZipArchiveInputStream.builder().setPath(path).get()) { final ZipArchiveEntry entry = zis.getNextEntry(); assertEquals("file-1.txt", entry.getName()); final byte[] content = IOUtils.toByteArray(zis);