diff --git a/WHATSNEW b/WHATSNEW index a7f77d27a..3a782c5cb 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -101,6 +101,9 @@ Other changes: * mail task accepts nested header element. Bugzilla report 24713. +* zip/jar/war/ear supports level attribute for deflate compression level. + Bugzilla report 25513. + Changes from Ant 1.6.2 to current Ant 1.6 CVS version ===================================================== diff --git a/docs/manual/CoreTasks/ear.html b/docs/manual/CoreTasks/ear.html index 16277ef30..64ce70a2d 100644 --- a/docs/manual/CoreTasks/ear.html +++ b/docs/manual/CoreTasks/ear.html @@ -134,6 +134,13 @@ to a value other than its default, "add".

Defaults to true. Since Ant 1.6.2 No + + level + Non-default level at which file compression should be + performed. Valid values range from 0 (no compression/fastest) to 9 + (maximum compression/slowest). Since Ant 1.7 + No +

Nested elements

metainf

diff --git a/docs/manual/CoreTasks/jar.html b/docs/manual/CoreTasks/jar.html index ca7aa873d..bfba2a4d1 100644 --- a/docs/manual/CoreTasks/jar.html +++ b/docs/manual/CoreTasks/jar.html @@ -194,6 +194,13 @@ to a value other than its default, "add".

Defaults to true. Since Ant 1.6.2 No + + level + Non-default level at which file compression should be + performed. Valid values range from 0 (no compression/fastest) to 9 + (maximum compression/slowest). Since Ant 1.7 + No +

Nested elements

diff --git a/docs/manual/CoreTasks/war.html b/docs/manual/CoreTasks/war.html index 9bd84e3db..48ada4436 100644 --- a/docs/manual/CoreTasks/war.html +++ b/docs/manual/CoreTasks/war.html @@ -141,6 +141,13 @@ to a value other than its default, "add".

Defaults to true. Since Ant 1.6.2 No + + level + Non-default level at which file compression should be + performed. Valid values range from 0 (no compression/fastest) to 9 + (maximum compression/slowest). Since Ant 1.7 + No +

Nested elements

lib

diff --git a/docs/manual/CoreTasks/zip.html b/docs/manual/CoreTasks/zip.html index 6a0ea3412..1243904b8 100644 --- a/docs/manual/CoreTasks/zip.html +++ b/docs/manual/CoreTasks/zip.html @@ -189,6 +189,13 @@ to a value other than its default, "add".

Comment to store in the archive. Since Ant 1.6.3 No + + level + Non-default level at which file compression should be + performed. Valid values range from 0 (no compression/fastest) to 9 + (maximum compression/slowest). Since Ant 1.7 + No +

Parameters specified as nested elements

fileset

diff --git a/src/etc/testcases/taskdefs/zip.xml b/src/etc/testcases/taskdefs/zip.xml index f2acdc366..d50bb8bf6 100644 --- a/src/etc/testcases/taskdefs/zip.xml +++ b/src/etc/testcases/taskdefs/zip.xml @@ -139,6 +139,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index 5598d5b77..e0db2e9da 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -120,6 +120,8 @@ public class Zip extends MatchingTask { */ private String comment = ""; + private int level = ZipOutputStream.DEFAULT_COMPRESSION; + /** * This is the name/location of where to * create the .zip file. @@ -335,6 +337,25 @@ public class Zip extends MatchingTask { return comment; } + /** + * Set the compression level to use. Default is + * ZipOutputStream.DEFAULT_COMPRESSION. + * @param level compression level. + * @since Ant 1.7 + */ + public void setLevel(int level) { + this.level = level; + } + + /** + * Get the compression level. + * @return compression level. + * @since Ant 1.7 + */ + public int getLevel() { + return level; + } + /** * Whether the file modification times will be rounded up to the * next even number of seconds. @@ -481,16 +502,13 @@ public class Zip extends MatchingTask { ZipOutputStream zOut = null; try { - if (!skipWriting) { zOut = new ZipOutputStream(zipFile); zOut.setEncoding(encoding); - if (doCompress) { - zOut.setMethod(ZipOutputStream.DEFLATED); - } else { - zOut.setMethod(ZipOutputStream.STORED); - } + zOut.setMethod(doCompress + ? ZipOutputStream.DEFLATED : ZipOutputStream.STORED); + zOut.setLevel(level); } initZipOutputStream(zOut); @@ -1080,12 +1098,12 @@ public class Zip extends MatchingTask { ze.setMethod(doCompress ? ZipEntry.DEFLATED : ZipEntry.STORED); /* - * ZipOutputStream.putNextEntry expects the ZipEntry to - * know its size and the CRC sum before you start writing - * the data when using STORED mode - unless it is seekable. - * - * This forces us to process the data twice. - */ + * ZipOutputStream.putNextEntry expects the ZipEntry to + * know its size and the CRC sum before you start writing + * the data when using STORED mode - unless it is seekable. + * + * This forces us to process the data twice. + */ if (!zOut.isSeekable() && !doCompress) { long size = 0; CRC32 cal = new CRC32(); diff --git a/src/main/org/apache/tools/zip/ZipOutputStream.java b/src/main/org/apache/tools/zip/ZipOutputStream.java index bb530dac7..c2d9926de 100644 --- a/src/main/org/apache/tools/zip/ZipOutputStream.java +++ b/src/main/org/apache/tools/zip/ZipOutputStream.java @@ -54,6 +54,27 @@ import java.util.zip.ZipException; */ public class ZipOutputStream extends FilterOutputStream { + /** + * Compression method for deflated entries. + * + * @since 1.1 + */ + public static final int DEFLATED = java.util.zip.ZipEntry.DEFLATED; + + /** + * Default compression level for deflated entries. + * + * @since Ant 1.7 + */ + public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION; + + /** + * Compression method for stored entries. + * + * @since 1.1 + */ + public static final int STORED = java.util.zip.ZipEntry.STORED; + /** * Current entry. * @@ -73,7 +94,7 @@ public class ZipOutputStream extends FilterOutputStream { * * @since 1.1 */ - private int level = Deflater.DEFAULT_COMPRESSION; + private int level = DEFAULT_COMPRESSION; /** * Has the compression level changed when compared to the last @@ -182,7 +203,7 @@ public class ZipOutputStream extends FilterOutputStream { * * @since 1.14 */ - protected Deflater def = new Deflater(Deflater.DEFAULT_COMPRESSION, true); + protected Deflater def = new Deflater(level, true); /** * This buffer servers as a Deflater. @@ -203,20 +224,6 @@ public class ZipOutputStream extends FilterOutputStream { */ private RandomAccessFile raf = null; - /** - * Compression method for deflated entries. - * - * @since 1.1 - */ - public static final int DEFLATED = java.util.zip.ZipEntry.DEFLATED; - - /** - * Compression method for stored entries. - * - * @since 1.1 - */ - public static final int STORED = java.util.zip.ZipEntry.STORED; - /** * Creates a new ZIP OutputStream filtering the underlying stream. * @param out the outputstream to zip @@ -430,10 +437,16 @@ public class ZipOutputStream extends FilterOutputStream { * Sets the compression level for subsequent entries. * *

Default is Deflater.DEFAULT_COMPRESSION.

- * @param level the compression level + * @param level the compression level. + * @throws IllegalArgumentException if an invalid compression level is specified. * @since 1.1 */ public void setLevel(int level) { + if (level < Deflater.DEFAULT_COMPRESSION + || level > Deflater.BEST_COMPRESSION) { + throw new IllegalArgumentException( + "Invalid compression level: " + level); + } hasCompressionLevelChanged = (this.level != level); this.level = level; } @@ -843,7 +856,7 @@ public class ZipOutputStream extends FilterOutputStream { * * @since 1.14 */ - protected final void writeOut(byte [] data) throws IOException { + protected final void writeOut(byte[] data) throws IOException { writeOut(data, 0, data.length); } @@ -856,7 +869,7 @@ public class ZipOutputStream extends FilterOutputStream { * * @since 1.14 */ - protected final void writeOut(byte [] data, int offset, int length) + protected final void writeOut(byte[] data, int offset, int length) throws IOException { if (raf != null) { raf.write(data, offset, length); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/ZipTest.java b/src/testcases/org/apache/tools/ant/taskdefs/ZipTest.java index 0c9f1ac7d..57e4f4ed6 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/ZipTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/ZipTest.java @@ -143,6 +143,9 @@ public class ZipTest extends BuildFileTest { executeTarget("zipEmptyCreate"); assertTrue("archive should be created", getProject().resolveFile("test3.zip").exists()); - + } + // Bugzilla Report 25513 + public void testCompressionLevel() { + executeTarget("testCompressionLevel"); } }