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 2023/03/17 19:17:43 UTC

[commons-compress] 02/02: [COMPRESS-600] Add capability to configure Deflater strategy in GzipCompressorOutputStream: GzipParameters.setDeflateStrategy(int).

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 7ed71e86c5fb6c38ab14eb53e511244909c6ba4d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 17 15:17:36 2023 -0400

    [COMPRESS-600] Add capability to configure Deflater strategy in
    GzipCompressorOutputStream: GzipParameters.setDeflateStrategy(int).
---
 src/changes/changes.xml                            |  1 +
 .../gzip/GzipCompressorOutputStream.java           |  1 +
 .../compress/compressors/gzip/GzipParameters.java  | 26 ++++++++++++++
 .../compressors/gzip/GzipParametersTest.java       | 40 ++++++++++++++++++++++
 4 files changed, 68 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index aa43075b..66b3d9e0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -69,6 +69,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" issue="COMPRESS-633" dev="ggregory" due-to="Daniel Santos, Bruno P. Kinoshita, Gary Gregory">Add encryption support for SevenZ #332.</action>
       <action type="add" issue="COMPRESS-613" dev="ggregory" due-to="Andre Brait, Gary Gregory, Bruno P. Kinoshita">Support for extra time data in Zip archives #345.</action>      
       <action type="add" issue="COMPRESS-621" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.archivers.zip.DefaultBackingStoreSupplier to write to a custom folder instead of the default temporary folder.</action>
+      <action type="add" issue="COMPRESS-600" dev="ggregory" due-to="Gary Gregory, Pascal Davoust">Add capability to configure Deflater strategy in GzipCompressorOutputStream: GzipParameters.setDeflateStrategy(int).</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Dependabot">Bump mockito.version from 4.8.0 to 4.11.0 #328, #331, #340, #348.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump pmd from 6.50.0 to 6.53.0.</action>
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 7b1975ae..ee7a74a5 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
@@ -83,6 +83,7 @@ public class GzipCompressorOutputStream extends CompressorOutputStream {
     public GzipCompressorOutputStream(final OutputStream out, final GzipParameters parameters) throws IOException {
         this.out = out;
         this.deflater = new Deflater(parameters.getCompressionLevel(), true);
+        this.deflater.setStrategy(parameters.getDeflateStrategy());
         this.deflateBuffer = new byte[parameters.getBufferSize()];
         writeHeader(parameters);
     }
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 33e22518..7b5927e4 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
@@ -25,6 +25,8 @@ import java.util.zip.Deflater;
 /**
  * Parameters for the GZIP compressor.
  *
+ * @see GzipCompressorInputStream
+ * @see GzipCompressorOutputStream
  * @since 1.7
  */
 public class GzipParameters {
@@ -35,6 +37,7 @@ public class GzipParameters {
     private String comment;
     private int operatingSystem = 255; // Unknown OS by default
     private int bufferSize = 512;
+    private int deflateStrategy = Deflater.DEFAULT_STRATEGY;
 
     /**
      * Gets size of the buffer used to retrieve compressed data.
@@ -54,6 +57,18 @@ public class GzipParameters {
         return compressionLevel;
     }
 
+    /**
+     * Gets the deflater strategy.
+     * 
+     * @return the deflater strategy, {@link Deflater#DEFAULT_STRATEGY} by default.
+     * @see #setDeflateStrategy(int)
+     * @see Deflater#setStrategy(int)
+     * @since 1.23
+     */
+    public int getDeflateStrategy() {
+        return deflateStrategy;
+    }
+
     public String getFilename() {
         return filename;
     }
@@ -100,6 +115,17 @@ public class GzipParameters {
         this.compressionLevel = compressionLevel;
     }
 
+    /**
+     * Sets the deflater strategy.
+     *
+     * @param deflateStrategy the new compression strategy
+     * @see Deflater#setStrategy(int)
+     * @since 1.23
+     */
+    public void setDeflateStrategy(int deflateStrategy) {
+        this.deflateStrategy = deflateStrategy;
+    }
+
     /**
      * Sets the name of the compressed file.
      *
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java
new file mode 100644
index 00000000..866bc3d8
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress.compressors.gzip;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.zip.Deflater;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link GzipParameters}.
+ */
+public class GzipParametersTest {
+
+    @Test
+    public void testDeflaterStrategy() {
+        final GzipParameters gzipParameters = new GzipParameters();
+        assertEquals(Deflater.DEFAULT_STRATEGY, gzipParameters.getDeflateStrategy());
+        gzipParameters.setDeflateStrategy(Deflater.HUFFMAN_ONLY);
+        assertEquals(Deflater.HUFFMAN_ONLY, gzipParameters.getDeflateStrategy());
+    }
+}