You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/12/22 20:37:49 UTC
(commons-compress) branch master updated (f01814445 -> a0e96aa4d)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
from f01814445 Optionally add FHCRC to GZIP header. (#627)
new ea8deb281 Add optional FHCRC to GZIP header #627
new 3b60dd273 Add optional FHCRC to GZIP header
new a0e96aa4d Refactor duplicate constant declarations
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 1 +
.../gzip/GzipCompressorInputStream.java | 18 ++++----------
.../gzip/GzipCompressorOutputStream.java | 22 ++++-------------
.../compress/compressors/gzip/GzipParameters.java | 20 ++++++++--------
.../compress/compressors/gzip/GzipUtils.java | 28 +++++++++++++++-------
.../gzip/GzipCompressorOutputStreamTest.java | 10 ++++----
6 files changed, 45 insertions(+), 54 deletions(-)
(commons-compress) 01/03: Add optional FHCRC to GZIP header #627
Posted by gg...@apache.org.
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 ea8deb28186230328aaca761b0421657de9fd9cc
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 22 15:29:54 2024 -0500
Add optional FHCRC to GZIP header #627
---
src/changes/changes.xml | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2d27634ad..109d66223 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ZipFile.stream().</action>
<action type="add" dev="ggregory" due-to="Danny Deschenes, Gary Gregory">GzipCompressorInputStream reads the modification time (MTIME) and stores its value incorrectly multiplied by 1,000.</action>
<action type="add" dev="ggregory" due-to="Danny Deschenes, Gary Gregory">GzipCompressorInputStream writes the modification time (MTIME) the value incorrectly divided by 1,000.</action>
+ <action type="add" dev="ggregory" due-to="Danny Deschenes, Gary Gregory">Add optional FHCRC to GZIP header #627.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 72 to 78 #563, #567, #574, #582, #587, #595.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump com.github.luben:zstd-jni from 1.5.6-4 to 1.5.6-8 #565, #578, #601, #616.</action>
(commons-compress) 02/03: Add optional FHCRC to GZIP header
Posted by gg...@apache.org.
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 3b60dd27311ea3e8f4b385db31793684e43cf4b5
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 22 15:32:18 2024 -0500
Add optional FHCRC to GZIP header
- Rename some new methods
- Sort members
- Normalize whitespace
---
.../compressors/gzip/GzipCompressorOutputStream.java | 4 ++--
.../compress/compressors/gzip/GzipParameters.java | 20 ++++++++++----------
.../gzip/GzipCompressorOutputStreamTest.java | 10 +++++-----
3 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java
index 086547760..2a15f8993 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java
@@ -183,7 +183,7 @@ public class GzipCompressorOutputStream extends CompressorOutputStream<OutputStr
buffer.put((byte) ((extra != null ? FEXTRA : 0)
| (fileName != null ? FNAME : 0)
| (comment != null ? FCOMMENT : 0)
- | (parameters.hasHeaderCRC() ? FHCRC : 0)
+ | (parameters.getHeaderCRC() ? FHCRC : 0)
)); // flags
buffer.putInt((int) parameters.getModificationInstant().getEpochSecond());
// extra flags
@@ -208,7 +208,7 @@ public class GzipCompressorOutputStream extends CompressorOutputStream<OutputStr
}
writeC(fileName, parameters.getFileNameCharset());
writeC(comment, parameters.getFileNameCharset());
- if (parameters.hasHeaderCRC()) {
+ if (parameters.getHeaderCRC()) {
final int v = (int) crc.getValue() & 0xffff;
out.write(v & 0xff);
out.write((v >>> 8) & 0xff);
diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java
index f241745fd..6f7662be8 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java
@@ -390,6 +390,16 @@ public class GzipParameters {
}
+ /**
+ * Returns if the header CRC is to be added (when writing) or was present (when reading).
+ *
+ * @return true is header CRC will be added (on write) or was found (after read).
+ * @since 1.28.0
+ */
+ public boolean getHeaderCRC() {
+ return headerCRC;
+ }
+
/**
* Gets the most recent modification time (MTIME) of the original file being compressed.
*
@@ -433,16 +443,6 @@ public class GzipParameters {
return operatingSystem;
}
- /**
- * Returns if the header CRC is to be added (when writing) or was present (when reading).
- *
- * @return true is header CRC will be added (on write) or was found (after read).
- * @since 1.28.0
- */
- public boolean hasHeaderCRC() {
- return headerCRC;
- }
-
private String requireNonNulByte(final String text) {
if (StringUtils.isNotEmpty(text) && ArrayUtils.contains(text.getBytes(fileNameCharset), (byte) 0)) {
throw new IllegalArgumentException("String encoded in Charset '" + fileNameCharset + "' contains the nul byte 0 which is not supported in gzip.");
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java
index 08b08f4be..f6fb70953 100644
--- a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java
@@ -229,7 +229,7 @@ public class GzipCompressorOutputStreamTest {
* @throws IOException When the test has issues with the underlying file system or unexpected gzip operations.
*/
@Test
- public void testHcrc() throws IOException, DecoderException {
+ public void testHeaderCrc() throws IOException, DecoderException {
final GzipParameters parameters = new GzipParameters();
parameters.setHeaderCRC(true);
parameters.setModificationTime(0x66554433); // avoid changing time
@@ -249,10 +249,10 @@ public class GzipCompressorOutputStreamTest {
+ "1e" // flg(FEXTRA|FNAME|FCOMMENT|FHCRC)
+ "33445566" // mtime little endian
+ "00" + "03" // xfl os
- + "0800" + "4242" + "0400" + "43434343" //xlen sfid sflen "CCCC"
+ + "0800" + "4242" + "0400" + "43434343" // xlen sfid sflen "CCCC"
+ "4141414100" // "AAAA" with \0
+ "5a5a5a5a00" // "ZZZZ" with \0
- + "d842" //crc32 = 839242d8
+ + "d842" // crc32 = 839242d8
+ "0300" // empty deflate stream
+ "00000000" // crs32
+ "00000000" // isize
@@ -265,7 +265,7 @@ public class GzipCompressorOutputStreamTest {
});
try (GzipCompressorInputStream gis = new GzipCompressorInputStream(new ByteArrayInputStream(result))) {
final GzipParameters metaData = gis.getMetaData();
- assertTrue(metaData.hasHeaderCRC());
+ assertTrue(metaData.getHeaderCRC());
assertEquals(0x66554433, metaData.getModificationTime());
assertEquals(1, metaData.getExtraField().size());
final SubField sf = metaData.getExtraField().iterator().next();
@@ -277,7 +277,7 @@ public class GzipCompressorOutputStreamTest {
}
// verify that the constructor normally fails on bad HCRC
assertThrows(ZipException.class, () -> {
- result[30] = 0x77; //corrupt the low byte of header CRC
+ result[30] = 0x77; // corrupt the low byte of header CRC
try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(result))) {
// if it does not fail, the hcrc is good.
}
(commons-compress) 03/03: Refactor duplicate constant declarations
Posted by gg...@apache.org.
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 a0e96aa4da27b70ddfc83140b5b3456812310f5b
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 22 15:37:37 2024 -0500
Refactor duplicate constant declarations
---
.../gzip/GzipCompressorInputStream.java | 18 ++++----------
.../gzip/GzipCompressorOutputStream.java | 20 ++++------------
.../compress/compressors/gzip/GzipUtils.java | 28 +++++++++++++++-------
3 files changed, 28 insertions(+), 38 deletions(-)
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 f95727c74..cd7ee4b98 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
@@ -66,14 +66,6 @@ import org.apache.commons.io.input.BoundedInputStream;
*/
public class GzipCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
- // Header flags
- // private static final int FTEXT = 0x01; // Uninteresting for us
- private static final int FHCRC = 0x02;
- private static final int FEXTRA = 0x04;
- private static final int FNAME = 0x08;
- private static final int FCOMMENT = 0x10;
- private static final int FRESERVED = 0xE0;
-
/**
* Checks if the signature matches what is expected for a .gz file.
*
@@ -223,7 +215,7 @@ public class GzipCompressorInputStream extends CompressorInputStream implements
}
final int flg = inData.readUnsignedByte();
- if ((flg & FRESERVED) != 0) {
+ if ((flg & GzipUtils.FRESERVED) != 0) {
throw new IOException("Reserved flags are set in the .gz header");
}
@@ -242,7 +234,7 @@ public class GzipCompressorInputStream extends CompressorInputStream implements
parameters.setOperatingSystem(inData.readUnsignedByte());
// Extra field
- if ((flg & FEXTRA) != 0) {
+ if ((flg & GzipUtils.FEXTRA) != 0) {
int xlen = inData.readUnsignedByte();
xlen |= inData.readUnsignedByte() << 8;
final byte[] extra = new byte[xlen];
@@ -251,12 +243,12 @@ public class GzipCompressorInputStream extends CompressorInputStream implements
}
// Original file name
- if ((flg & FNAME) != 0) {
+ if ((flg & GzipUtils.FNAME) != 0) {
parameters.setFileName(new String(readToNull(inData), GzipUtils.GZIP_ENCODING));
}
// Comment
- if ((flg & FCOMMENT) != 0) {
+ if ((flg & GzipUtils.FCOMMENT) != 0) {
parameters.setComment(new String(readToNull(inData), GzipUtils.GZIP_ENCODING));
}
@@ -265,7 +257,7 @@ public class GzipCompressorInputStream extends CompressorInputStream implements
// sets this, so it's not worth trying to verify it. GNU gzip 1.4
// doesn't support this field, but zlib seems to be able to at least
// skip over it.
- if ((flg & FHCRC) != 0) {
+ if ((flg & GzipUtils.FHCRC) != 0) {
parameters.setHeaderCRC(true);
inData.readShort();
}
diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java
index 2a15f8993..81c84ba42 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java
@@ -38,18 +38,6 @@ import org.apache.commons.compress.compressors.CompressorOutputStream;
*/
public class GzipCompressorOutputStream extends CompressorOutputStream<OutputStream> {
- /** Header flag indicating an EXTRA subfields collection follows the header */
- private static final int FEXTRA = 1 << 2;
-
- /** Header flag indicating a file name follows the header */
- private static final int FNAME = 1 << 3;
-
- /** Header flag indicating a comment follows the header */
- private static final int FCOMMENT = 1 << 4;
-
- /** Header flag indicating a header CRC follows the header */
- private static final int FHCRC = 1 << 1;
-
/** Deflater used to compress the data */
private final Deflater deflater;
@@ -180,10 +168,10 @@ public class GzipCompressorOutputStream extends CompressorOutputStream<OutputStr
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putShort((short) GZIPInputStream.GZIP_MAGIC);
buffer.put((byte) Deflater.DEFLATED); // compression method (8: deflate)
- buffer.put((byte) ((extra != null ? FEXTRA : 0)
- | (fileName != null ? FNAME : 0)
- | (comment != null ? FCOMMENT : 0)
- | (parameters.getHeaderCRC() ? FHCRC : 0)
+ buffer.put((byte) ((extra != null ? GzipUtils.FEXTRA : 0)
+ | (fileName != null ? GzipUtils.FNAME : 0)
+ | (comment != null ? GzipUtils.FCOMMENT : 0)
+ | (parameters.getHeaderCRC() ? GzipUtils.FHCRC : 0)
)); // flags
buffer.putInt((int) parameters.getModificationInstant().getEpochSecond());
// extra flags
diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java
index 2aeae2a3f..faddae4a2 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java
@@ -33,8 +33,27 @@ import org.apache.commons.compress.compressors.FileNameUtil;
*/
public class GzipUtils {
+ /** Header flag indicating a comment follows the header. */
+ static final int FCOMMENT = 0x10;
+
+ /** Header flag indicating an EXTRA subfields collection follows the header. */
+ static final int FEXTRA = 0x04;
+
+ /** Header flag indicating a header CRC follows the header. */
+ static final int FHCRC = 0x02;
+
+ /** Header flag indicating a file name follows the header. */
+ static final int FNAME = 0x08;
+
+ static final int FRESERVED = 0xE0;
+
private static final FileNameUtil fileNameUtil;
+ /**
+ * Charset for file name and comments per the <a href="https://tools.ietf.org/html/rfc1952">GZIP File Format Specification</a>.
+ */
+ static final Charset GZIP_ENCODING = StandardCharsets.ISO_8859_1;
+
static {
// using LinkedHashMap so .tgz is preferred over .taz as
// compressed extension of .tar as FileNameUtil will use the
@@ -54,11 +73,6 @@ public class GzipUtils {
fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
}
- /**
- * Charset for file name and comments per the <a href="https://tools.ietf.org/html/rfc1952">GZIP File Format Specification</a>.
- */
- static final Charset GZIP_ENCODING = StandardCharsets.ISO_8859_1;
-
/**
* Maps the given file name to the name that the file should have after compression with gzip. Common file types with custom suffixes for compressed
* versions are automatically detected and correctly mapped. For example the name "package.tar" is mapped to "package.tgz". If no custom mapping is
@@ -85,7 +99,6 @@ public class GzipUtils {
public static String getCompressedFileName(final String fileName) {
return fileNameUtil.getCompressedFileName(fileName);
}
-
/**
* Maps the given name of a gzip-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like
* ".tgz" or ".svgz" are automatically detected and correctly mapped. For example the name "package.tgz" is mapped to "package.tar". And any file names with
@@ -100,7 +113,6 @@ public class GzipUtils {
public static String getUncompressedFilename(final String fileName) {
return fileNameUtil.getUncompressedFileName(fileName);
}
-
/**
* Maps the given name of a gzip-compressed file to the name that the file should have after uncompression. Commonly used file type specific suffixes like
* ".tgz" or ".svgz" are automatically detected and correctly mapped. For example the name "package.tgz" is mapped to "package.tar". And any file names with
@@ -114,7 +126,6 @@ public class GzipUtils {
public static String getUncompressedFileName(final String fileName) {
return fileNameUtil.getUncompressedFileName(fileName);
}
-
/**
* Detects common gzip suffixes in the given file name.
*
@@ -126,7 +137,6 @@ public class GzipUtils {
public static boolean isCompressedFilename(final String fileName) {
return fileNameUtil.isCompressedFileName(fileName);
}
-
/**
* Detects common gzip suffixes in the given file name.
*