ZipArchiveOutputStream knows some tricks if it is writing to a file - take advantage of it

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/compress/trunk@1154124 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java b/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
index 309ff8f..b9c4594 100644
--- a/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
@@ -42,6 +42,7 @@
 import org.apache.ant.compress.resources.ZipResource;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
+import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
@@ -59,6 +60,7 @@
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.ArchiveResource;
+import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.types.resources.MappedResource;
 import org.apache.tools.ant.types.resources.Resources;
@@ -513,11 +515,21 @@
         ArchiveOutputStream out = null;
         Set addedDirectories = new HashSet();
         try {
-            out =
-                factory.getArchiveStream(new BufferedOutputStream(getDest()
-                                                                  .getOutputStream()),
-                                         Expand.NATIVE_ENCODING.equals(getEncoding())
-                                         ? null : getEncoding());
+            String enc = Expand.NATIVE_ENCODING.equals(getEncoding())
+                ? null : getEncoding();
+            if (factory instanceof FileAwareArchiveStreamFactory
+                && getDest().as(FileProvider.class) != null) {
+                FileProvider p =
+                    (FileProvider) getDest().as(FileProvider.class);
+                FileAwareArchiveStreamFactory f =
+                    (FileAwareArchiveStreamFactory) factory;
+                out = f.getArchiveOutputStream(p.getFile(), enc);
+            } else {
+                out =
+                    factory.getArchiveStream(new BufferedOutputStream(getDest()
+                                                                      .getOutputStream()),
+                                             enc);
+            }
             for (Iterator i = src.iterator(); i.hasNext(); ) {
                 ResourceWithFlags r = (ResourceWithFlags) i.next();
 
diff --git a/src/main/org/apache/ant/compress/taskdefs/Zip.java b/src/main/org/apache/ant/compress/taskdefs/Zip.java
index 892916e..5d80926 100644
--- a/src/main/org/apache/ant/compress/taskdefs/Zip.java
+++ b/src/main/org/apache/ant/compress/taskdefs/Zip.java
@@ -18,6 +18,7 @@
 
 package org.apache.ant.compress.taskdefs;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.HashMap;
@@ -53,12 +54,15 @@
                     ZipArchiveOutputStream o =
                         (ZipArchiveOutputStream) super.getArchiveStream(stream,
                                                                         encoding);
-                    o.setLevel(level);
-                    o.setComment(comment);
-                    o.setFallbackToUTF8(fallBackToUTF8);
-                    o.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
-                    o.setCreateUnicodeExtraFields(createUnicodeExtraFields
-                                                  .getPolicy());
+                    configure(o);
+                    return o;
+                }
+                public ArchiveOutputStream getArchiveOutputStream(File f,
+                                                                  String encoding)
+                    throws IOException {
+                    ZipArchiveOutputStream o = (ZipArchiveOutputStream)
+                        super.getArchiveOutputStream(f, encoding);
+                    configure(o);
                     return o;
                 }
             });
@@ -158,6 +162,14 @@
         createUnicodeExtraFields = b;
     }
 
+    private void configure(ZipArchiveOutputStream o) {
+        o.setLevel(level);
+        o.setComment(comment);
+        o.setFallbackToUTF8(fallBackToUTF8);
+        o.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
+        o.setCreateUnicodeExtraFields(createUnicodeExtraFields.getPolicy());
+    }
+
     /**
      * Policiy for creation of Unicode extra fields: never, always or
      * not-encodeable.
diff --git a/src/main/org/apache/ant/compress/util/FileAwareArchiveStreamFactory.java b/src/main/org/apache/ant/compress/util/FileAwareArchiveStreamFactory.java
new file mode 100644
index 0000000..aca33bd
--- /dev/null
+++ b/src/main/org/apache/ant/compress/util/FileAwareArchiveStreamFactory.java
@@ -0,0 +1,55 @@
+/*
+ *  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.ant.compress.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+
+/**
+ * Creates streams for the supported archive formats that may take
+ * advantage of writing to/reading from a file.
+ *
+ * @since Apache Compress Antlib 1.1
+ */
+public interface FileAwareArchiveStreamFactory extends ArchiveStreamFactory {
+    /**
+     * @param file the file to read from
+     * @param encoding the encoding of the entry names, ignored by all
+     * formats except zip
+     */
+    public ArchiveInputStream getArchiveInputStream(File file,
+                                                    String encoding)
+        throws IOException;
+
+
+    /**
+     * @param file the file to write to
+     * @param encoding the encoding of the entry names, ignored by all
+     * formats except zip
+     */
+    public ArchiveOutputStream getArchiveOutputStream(File file,
+                                                      String encoding)
+        throws IOException;
+
+}
\ No newline at end of file
diff --git a/src/main/org/apache/ant/compress/util/ZipStreamFactory.java b/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
index 64ba927..9be34de 100644
--- a/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
+++ b/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
@@ -18,8 +18,11 @@
 
 package org.apache.ant.compress.util;
 
-import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 
 import org.apache.commons.compress.archivers.ArchiveInputStream;
@@ -27,7 +30,7 @@
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 
-public class ZipStreamFactory implements ArchiveStreamFactory {
+public class ZipStreamFactory implements FileAwareArchiveStreamFactory {
 
     /**
      * @param stream the stream to read from, should be buffered
@@ -50,4 +53,30 @@
         o.setEncoding(encoding);
         return o;
     }
+
+    /**
+     * @param file the file to read from
+     * @param encoding the encoding of the entry names, ignored by all
+     * formats except zip
+     */
+    public ArchiveInputStream getArchiveInputStream(File file,
+                                                    String encoding)
+        throws IOException {
+        return
+            getArchiveStream(new BufferedInputStream(new FileInputStream(file)),
+                             encoding);
+    }
+
+    /**
+     * @param file the file to write to
+     * @param encoding the encoding of the entry names, ignored by all
+     * formats except zip
+     */
+    public ArchiveOutputStream getArchiveOutputStream(File file,
+                                                      String encoding)
+        throws IOException {
+        ZipArchiveOutputStream o = new ZipArchiveOutputStream(file);
+        o.setEncoding(encoding);
+        return o;
+    }
 }
\ No newline at end of file