Browse Source

Added level attribute to zip & subclasses.

PR: 25513


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277577 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 20 years ago
parent
commit
1225a5a849
9 changed files with 114 additions and 32 deletions
  1. +3
    -0
      WHATSNEW
  2. +7
    -0
      docs/manual/CoreTasks/ear.html
  3. +7
    -0
      docs/manual/CoreTasks/jar.html
  4. +7
    -0
      docs/manual/CoreTasks/war.html
  5. +7
    -0
      docs/manual/CoreTasks/zip.html
  6. +17
    -0
      src/etc/testcases/taskdefs/zip.xml
  7. +30
    -12
      src/main/org/apache/tools/ant/taskdefs/Zip.java
  8. +32
    -19
      src/main/org/apache/tools/zip/ZipOutputStream.java
  9. +4
    -1
      src/testcases/org/apache/tools/ant/taskdefs/ZipTest.java

+ 3
- 0
WHATSNEW View File

@@ -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
=====================================================



+ 7
- 0
docs/manual/CoreTasks/ear.html View File

@@ -134,6 +134,13 @@ to a value other than its default, <code>&quot;add&quot;</code>.</b></p>
Defaults to true. <em>Since Ant 1.6.2</em></td>
<td align="center" valign="top">No</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 Ant 1.7</em></td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Nested elements</h3>
<h4>metainf</h4>


+ 7
- 0
docs/manual/CoreTasks/jar.html View File

@@ -194,6 +194,13 @@ to a value other than its default, <code>&quot;add&quot;</code>.</b></p>
Defaults to true. <em>Since Ant 1.6.2</em></td>
<td align="center" valign="top">No</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 Ant 1.7</em></td>
<td valign="top" align="center">No</td>
</tr>
</table>

<h3>Nested elements</h3>


+ 7
- 0
docs/manual/CoreTasks/war.html View File

@@ -141,6 +141,13 @@ to a value other than its default, <code>&quot;add&quot;</code>.</b></p>
Defaults to true. <em>Since Ant 1.6.2</em></td>
<td align="center" valign="top">No</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 Ant 1.7</em></td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Nested elements</h3>
<h4>lib</h4>


+ 7
- 0
docs/manual/CoreTasks/zip.html View File

@@ -189,6 +189,13 @@ to a value other than its default, <code>&quot;add&quot;</code>.</b></p>
<td valign="top">Comment to store in the archive. <em>Since Ant 1.6.3</em></td>
<td valign="top" align="center">No</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 Ant 1.7</em></td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Parameters specified as nested elements</h3>
<h4>fileset</h4>


+ 17
- 0
src/etc/testcases/taskdefs/zip.xml View File

@@ -139,6 +139,23 @@
<zip destfile="test3.zip" basedir="empty" whenempty="create" includes="*.xyz"/>
</target>

<target name="testCompressionLevel" depends="test6">
<length property="test6.length" file="test6.zip" />
<zip destFile="testLevel.zip" basedir="." level="9">
<include name="*.xml" />
<exclude name="zip.*" />
</zip>
<fail>
<condition>
<not>
<isfileselected file="testLevel.zip">
<size when="less" value="${test6.length}" />
</isfileselected>
</not>
</condition>
</fail>
</target>

<target name="cleanup">
<delete file="test3.zip"/>
<delete file="test4.zip"/>


+ 30
- 12
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -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();


+ 32
- 19
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -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.
*
* <p>Default is Deflater.DEFAULT_COMPRESSION.</p>
* @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);


+ 4
- 1
src/testcases/org/apache/tools/ant/taskdefs/ZipTest.java View File

@@ -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");
}
}

Loading…
Cancel
Save