Add level attribute to gzip task.  PR 52414

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/compress/trunk@1577923 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/changes.xml b/changes.xml
index ac5efea..1fe9e6c 100644
--- a/changes.xml
+++ b/changes.xml
@@ -51,6 +51,10 @@
         Multiple content compression/encryption/filter methods can now
         be specified via nested elements of the sevenz task.
       </action>
+      <action type="add" issue="52414">
+        The gzip task has a new attribute that controls the level of
+        compression.
+      </action>
     </release>
 
     <release version="1.4" date="2014-01-29">
diff --git a/docs/pack.html b/docs/pack.html
index 40ebb00..f878a16 100644
--- a/docs/pack.html
+++ b/docs/pack.html
@@ -103,6 +103,24 @@
   <p>Is a <a href="#pack">compressing task</a> that uses the GZIP
     compression algorithm.</p>
 
+  <p>This task supports the following additional attributes:</p>
+
+  <table border="1" cellpadding="2" cellspacing="0">
+    <tr>
+      <td valign="top"><b>Attribute</b></td>
+      <td valign="top"><b>Description</b></td>
+      <td align="center" valign="top"><b>Required</b></td>
+    </tr>
+    <tr>
+      <td valign="top">level</td>
+      <td valign="top">Non-default level at which file compression
+        should be performed. Valid values range from 0 (no
+        compression/fastest) to 9 (maximum
+        compression/slowest).  <em>Since Compress Antlib 1.5</em></td>
+      <td valign="top" align="center">No</td>
+    </tr>
+  </table>
+
   <h3><a name="pack200">Pack200</a></h3>
 
   <p>Is a <a href="#pack">compressing task</a> that uses
diff --git a/src/main/org/apache/ant/compress/taskdefs/GZip.java b/src/main/org/apache/ant/compress/taskdefs/GZip.java
index 05b1b10..97b9b00 100644
--- a/src/main/org/apache/ant/compress/taskdefs/GZip.java
+++ b/src/main/org/apache/ant/compress/taskdefs/GZip.java
@@ -18,23 +18,48 @@
 
 package org.apache.ant.compress.taskdefs;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.Deflater;
+
 import org.apache.ant.compress.resources.CommonsCompressCompressorResource;
 import org.apache.ant.compress.resources.GZipResource;
 import org.apache.ant.compress.util.GZipStreamFactory;
+import org.apache.commons.compress.compressors.CompressorOutputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
+import org.apache.commons.compress.compressors.gzip.GzipParameters;
 import org.apache.tools.ant.types.Resource;
 
 /**
  * Compresses using gzip.
  */
 public final class GZip extends PackBase {
+    private int level = Deflater.DEFAULT_COMPRESSION;
 
     public GZip() {
-        super(new GZipStreamFactory(),
-              new PackBase.ResourceWrapper() {
+        super(new PackBase.ResourceWrapper() {
                 public CommonsCompressCompressorResource wrap(Resource dest) {
                     return new GZipResource(dest);
                 }
             });
+        setFactory(new GZipStreamFactory() {
+                public CompressorOutputStream getCompressorStream(OutputStream stream)
+                    throws IOException {
+                    GzipParameters params = new GzipParameters();
+                    params.setCompressionLevel(level);
+                    return new GzipCompressorOutputStream(stream, params);
+                }
+            });
     }
 
-}
\ No newline at end of file
+    /**
+     * Set the compression level to use.  Default is
+     * Deflater.DEFAULT_COMPRESSION.
+     * @param level compression level.
+     * @since 1.5
+     */
+    public void setLevel(int level) {
+        this.level = level;
+    }
+
+}
diff --git a/src/tests/antunit/gunzip-test.xml b/src/tests/antunit/gunzip-test.xml
index b00c511..77fb8a8 100644
--- a/src/tests/antunit/gunzip-test.xml
+++ b/src/tests/antunit/gunzip-test.xml
@@ -54,6 +54,14 @@
                          actual="${output}/asf-logo.gif"/>
   </target>
 
+  <target name="testAntlibGzipTaskWithLevel" depends="setUp">
+    <cmp:gzip src="../resources/asf-logo.gif" level="9"
+              destfile="${output}/asf-logo.gif.gz"/>
+    <cmp:gunzip src="${output}/asf-logo.gif.gz" dest="${output}/asf-logo.gif" />
+    <au:assertFilesMatch expected="../resources/asf-logo.gif"
+                         actual="${output}/asf-logo.gif"/>
+  </target>
+
   <target name="testNativeGzip" depends="setUp">
     <cmp:gunzip src="../resources/asf-logo.gif.gz"
                 dest="${output}/asf-logo.gif" />