diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java index 318b86974cf..4586d53f7cf 100644 --- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java @@ -61,8 +61,8 @@ *
* If you use the {@code Builder.setDecompressConcatenated(true)} or {@code GzipCompressorInputStream(in, true)} then {@link #read} will return -1 only after * the entire input stream has been exhausted; any bytes that follow an encoded GZIP member must constitute a new encoded GZIP member, otherwise an - * {@link IOException} is thrown. The data read from a stream constructed this way will consist of the concatenated data of all of the encoded GZIP members in - * order. + * {@link IOException} is thrown. Trailing zero padding after the last encoded GZIP member is ignored, like GNU gzip does. The data read from a stream + * constructed this way will consist of the concatenated data of all of the encoded GZIP members in order. *
** To build an instance, use {@link Builder}. @@ -358,6 +358,23 @@ public GzipParameters getMetaData() { return parameters; } + /** + * Consumes the remaining input and tests whether it consists solely of zero bytes. + *
+ * Trailing zero padding after the last member commonly appears when .gz files are stored on or copied from media that pads them to a block boundary; GNU + * gzip silently accepts such input. + *
+ * + * @return true if the input ended while only zero bytes were read, false if a non-zero byte was found. + */ + private boolean isTrailingZeroPadding() throws IOException { + int b; + do { + b = in.read(); + } while (b == 0); + return b == -1; + } + private boolean init(final boolean isFirstMember) throws IOException { if (!isFirstMember && !decompressConcatenated) { // at least one must be true throw new CompressorException("Unexpected: isFirstMember and decompressConcatenated are both false."); @@ -370,6 +387,10 @@ private boolean init(final boolean isFirstMember) throws IOException { return false; } if (magic0 != GzipUtils.ID1 || in.read() != GzipUtils.ID2) { + // Like GNU gzip, trailing zero padding after the last member is not an error; anything else is. + if (!isFirstMember && magic0 == 0 && isTrailingZeroPadding()) { + return false; + } throw new CompressorException(isFirstMember ? "Input is not in the .gz format." : "Unexpected data after a valid .gz stream."); } parameters = new GzipParameters(); diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java index 06ef3f232db..2ca1c52fac5 100644 --- a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java @@ -411,4 +411,63 @@ void testUnencodableFileNameThrowsCompressorException() throws IOException { assertEquals("name.txt", gis.getMetaData().getFileName()); } } + + /** + * Tests COMPRESS-709: trailing zero padding after a valid member is ignored, like GNU gzip does, instead of failing with + * {@code "Unexpected data after a valid .gz stream."}. + * + * @throws IOException on test failure. + */ + @Test + void testCompress709TrailingZeroPaddingIsIgnored() throws IOException { + final byte[] payload = "Hello1\n".getBytes(StandardCharsets.ISO_8859_1); + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (GzipCompressorOutputStream gos = new GzipCompressorOutputStream(bos)) { + gos.write(payload); + } + final byte[] member = bos.toByteArray(); + // Padding spanning more than one internal read buffer. + final byte[] zeroPadding = new byte[16384]; + final ByteArrayOutputStream padded = new ByteArrayOutputStream(); + padded.write(member); + padded.write(zeroPadding); + final byte[] gz = padded.toByteArray(); + // Concatenated mode: the padding terminates the stream cleanly. + try (GzipCompressorInputStream gis = GzipCompressorInputStream.builder() + .setInputStream(new ByteArrayInputStream(gz)) + .setDecompressConcatenated(true) + .get()) { + assertArrayEquals(payload, IOUtils.toByteArray(gis)); + } + // Non-concatenated mode never looked at the tail. + try (GzipCompressorInputStream gis = GzipCompressorInputStream.builder() + .setInputStream(new ByteArrayInputStream(gz)) + .setDecompressConcatenated(false) + .get()) { + assertArrayEquals(payload, IOUtils.toByteArray(gis)); + } + } + + /** + * Tests COMPRESS-709: trailing input that is not zero padding is still rejected in concatenated mode. + * + * @throws IOException on test failure. + */ + @Test + void testCompress709TrailingGarbageIsStillRejected() throws IOException { + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (GzipCompressorOutputStream gos = new GzipCompressorOutputStream(bos)) { + gos.write("Hello1\n".getBytes(StandardCharsets.ISO_8859_1)); + } + final ByteArrayOutputStream withGarbage = new ByteArrayOutputStream(); + withGarbage.write(bos.toByteArray()); + withGarbage.write("garbage".getBytes(StandardCharsets.ISO_8859_1)); + try (GzipCompressorInputStream gis = GzipCompressorInputStream.builder() + .setInputStream(new ByteArrayInputStream(withGarbage.toByteArray())) + .setDecompressConcatenated(true) + .get()) { + final CompressorException e = assertThrows(CompressorException.class, () -> IOUtils.toByteArray(gis)); + assertEquals("Unexpected data after a valid .gz stream.", e.getMessage()); + } + } }