ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r794177 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Zip.java
Date Wed, 15 Jul 2009 08:26:38 GMT
Author: bodewig
Date: Wed Jul 15 08:26:37 2009
New Revision: 794177

URL: http://svn.apache.org/viewvc?rev=794177&view=rev
Log:
refactor zip archive creation logic into smaller chunks

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Zip.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Zip.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Zip.java?rev=794177&r1=794176&r2=794177&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Zip.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Zip.java Wed Jul 15 08:26:37 2009
@@ -894,83 +894,33 @@
                 if ("".equals(name)) {
                     continue;
                 }
-                if (resources[i].isDirectory() && !name.endsWith("/")) {
-                    name = name + "/";
-                }
 
-                if (!doFilesonly && !dealingWithFiles
-                    && resources[i].isDirectory()
-                    && !zfs.hasDirModeBeenSet()) {
-                    int nextToLastSlash = name.lastIndexOf("/",
-                                                           name.length() - 2);
-                    if (nextToLastSlash != -1) {
-                        addParentDirs(base, name.substring(0,
-                                                           nextToLastSlash + 1),
-                                      zOut, prefix, dirMode);
-                    }
-                    if (zf != null) {
-                        ZipEntry ze = zf.getEntry(resources[i].getName());
-                        int unixMode = ze.getUnixMode();
-                        if ((unixMode == 0 || unixMode == UnixStat.DIR_FLAG)
-                            && !preserve0Permissions) {
-                            unixMode = dirMode;
-                        }
-                        addParentDirs(base, name, zOut, prefix,
-                                      unixMode);
-                    } else {
-                        ArchiveResource tr = (ArchiveResource) resources[i];
-                        addParentDirs(base, name, zOut, prefix,
-                                      tr.getMode());
+                if (resources[i].isDirectory()) {
+                    if (doFilesonly) {
+                        continue;
                     }
+                    int thisDirMode = zfs != null && zfs.hasDirModeBeenSet()
+                        ? dirMode : getUnixMode(resources[i], zf, dirMode);
+                    addDirectoryResource(resources[i], name, prefix,
+                                         base, zOut,
+                                         dirMode, thisDirMode);
+
+                } else { // !isDirectory
 
-                } else {
                     addParentDirs(base, name, zOut, prefix, dirMode);
-                }
 
-                if (!resources[i].isDirectory() && dealingWithFiles) {
-                    File f = FILE_UTILS.resolveFile(base,
-                                                   resources[i].getName());
-                    zipFile(f, zOut, prefix + name, fileMode);
-                } else if (!resources[i].isDirectory()) {
-                    if (zf != null) {
-                    ZipEntry ze = zf.getEntry(resources[i].getName());
-
-                    if (ze != null) {
-                        boolean oldCompress = doCompress;
-                        if (keepCompression) {
-                            doCompress = (ze.getMethod() == ZipEntry.DEFLATED);
-                        }
-                        InputStream is = null;
-                        try {
-                            is = zf.getInputStream(ze);
-                            int unixMode = ze.getUnixMode();
-                            if (zfs.hasFileModeBeenSet()
-                                || ((unixMode == 0
-                                     || unixMode == UnixStat.FILE_FLAG)
-                                    && !preserve0Permissions)) {
-                                unixMode = fileMode;
-                            }
-                            zipFile(is, zOut, prefix + name,
-                                    ze.getTime(), zfs.getSrc(getProject()),
-                                    unixMode);
-                        } finally {
-                            doCompress = oldCompress;
-                            FileUtils.close(is);
-                        }
-                    }
+                    if (dealingWithFiles) {
+                        File f = FILE_UTILS.resolveFile(base,
+                                                        resources[i].getName());
+                        zipFile(f, zOut, prefix + name, fileMode);
                     } else {
-                        ArchiveResource tr = (ArchiveResource) resources[i];
-                        InputStream is = null;
-                        try {
-                            is = tr.getInputStream();
-                            zipFile(is, zOut, prefix + name,
-                                    resources[i].getLastModified(),
-                                    zfs.getSrc(getProject()),
-                                    zfs.hasFileModeBeenSet() ? fileMode
-                                    : tr.getMode());
-                        } finally {
-                            FileUtils.close(is);
-                        }
+                        int thisFileMode =
+                            zfs != null && zfs.hasFileModeBeenSet()
+                            ? fileMode : getUnixMode(resources[i], zf,
+                                                     fileMode);
+                        addResource(resources[i], name, prefix,
+                                    zOut, thisFileMode,
+                                    zf, zfs.getSrc(getProject()));
                     }
                 }
             }
@@ -982,6 +932,87 @@
     }
 
     /**
+     * Add a directory entry to the archive using a specified
+     * Unix-mode and the default mode for its parent directories (if
+     * necessary).
+     */
+    private void addDirectoryResource(Resource r, String name, String prefix,
+                                      File base, ZipOutputStream zOut,
+                                      int defaultDirMode, int thisDirMode)
+        throws IOException {
+
+        if (!name.endsWith("/")) {
+            name = name + "/";
+        }
+
+        int nextToLastSlash = name.lastIndexOf("/", name.length() - 2);
+        if (nextToLastSlash != -1) {
+            addParentDirs(base, name.substring(0, nextToLastSlash + 1),
+                          zOut, prefix, defaultDirMode);
+        }
+        addParentDirs(base, name, zOut, prefix, thisDirMode);
+    }
+
+    /**
+     * Determine a Resource's Unix mode or return the given default
+     * value if not available.
+     */
+    private int getUnixMode(Resource r, ZipFile zf, int defaultMode)
+        throws IOException {
+
+        int unixMode = defaultMode;
+        if (zf != null) {
+            ZipEntry ze = zf.getEntry(r.getName());
+            unixMode = ze.getUnixMode();
+            if ((unixMode == 0 || unixMode == UnixStat.DIR_FLAG)
+                && !preserve0Permissions) {
+                unixMode = defaultMode;
+            }
+        } else if (r instanceof ArchiveResource) {
+            unixMode = ((ArchiveResource) r).getMode();
+        }
+        return unixMode;
+    }
+
+    /**
+     * Add a file entry.
+     */
+    private void addResource(Resource r, String name, String prefix,
+                             ZipOutputStream zOut, int mode,
+                             ZipFile zf, File sourceArchive)
+        throws IOException {
+
+        if (zf != null) {
+            ZipEntry ze = zf.getEntry(r.getName());
+
+            if (ze != null) {
+                boolean oldCompress = doCompress;
+                if (keepCompression) {
+                    doCompress = (ze.getMethod() == ZipEntry.DEFLATED);
+                }
+                InputStream is = null;
+                try {
+                    is = zf.getInputStream(ze);
+                    zipFile(is, zOut, prefix + name, ze.getTime(),
+                            sourceArchive, mode);
+                } finally {
+                    doCompress = oldCompress;
+                    FileUtils.close(is);
+                }
+            }
+        } else {
+            InputStream is = null;
+            try {
+                is = r.getInputStream();
+                zipFile(is, zOut, prefix + name, r.getLastModified(),
+                        sourceArchive, mode);
+            } finally {
+                FileUtils.close(is);
+            }
+        }
+    }
+
+    /**
      * Add the given resources.
      *
      * @param rc may give additional information like fullpath or
@@ -1014,29 +1045,23 @@
             if (fp != null) {
                 base = ResourceUtils.asFileResource(fp).getBaseDir();
             }
+
             if (resources[i].isDirectory()) {
-                if (!name.endsWith("/")) {
-                    name = name + "/";
-                }
-            }
+                addDirectoryResource(resources[i], name, "", base, zOut,
+                                     ArchiveFileSet.DEFAULT_DIR_MODE,
+                                     ArchiveFileSet.DEFAULT_DIR_MODE);
 
-            addParentDirs(base, name, zOut, "",
-                          ArchiveFileSet.DEFAULT_DIR_MODE);
+            } else {
+                addParentDirs(base, name, zOut, "",
+                              ArchiveFileSet.DEFAULT_DIR_MODE);
 
-            if (!resources[i].isDirectory()) {
                 if (fp != null) {
                     File f = (fp).getFile();
                     zipFile(f, zOut, name, ArchiveFileSet.DEFAULT_FILE_MODE);
                 } else {
-                    InputStream is = null;
-                    try {
-                        is = resources[i].getInputStream();
-                        zipFile(is, zOut, name,
-                                resources[i].getLastModified(),
-                                null, ArchiveFileSet.DEFAULT_FILE_MODE);
-                    } finally {
-                        FileUtils.close(is);
-                    }
+                    addResource(resources[i], name, "", zOut,
+                                ArchiveFileSet.DEFAULT_FILE_MODE,
+                                null, null);
                 }
             }
         }



Mime
View raw message