commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r920286 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/zip/ZipFile.java test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java test/resources/ordertest.zip
Date Mon, 08 Mar 2010 12:00:25 GMT
Author: bodewig
Date: Mon Mar  8 12:00:25 2010
New Revision: 920286

URL: http://svn.apache.org/viewvc?rev=920286&view=rev
Log:
return Zipentries in predictable order.  COMPRESS-99

Added:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
  (with props)
    commons/proper/compress/trunk/src/test/resources/ordertest.zip   (with props)
Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=920286&r1=920285&r2=920286&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Mon Mar  8 12:00:25 2010
@@ -23,6 +23,13 @@
   </properties>
   <body>
     <release version="1.1" date="as in SVN" description="Release 1.1">
+      <action type="add" date="2010-03-08" issue="COMPRESS-99">
+        ZipFile#getEntries returns entries in a predictable order -
+        the order they appear inside the central directory.
+        A new method getEntriesInPhysicalOrder returns entries in
+        order of the entry data, i.e. the order ZipArchiveInputStream
+        would see.
+      </action>
       <action type="add" date="2010-02-19">
         The Archive*Stream and ZipFile classes now have
         can(Read|Write)EntryData methods that can be used to check

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=920286&r1=920285&r2=920286&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
Mon Mar  8 12:00:25 2010
@@ -21,9 +21,12 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.RandomAccessFile;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.zip.Inflater;
 import java.util.zip.InflaterInputStream;
@@ -72,7 +75,7 @@
      * Maps ZipArchiveEntrys to Longs, recording the offsets of the local
      * file headers.
      */
-    private final Map entries = new HashMap(HASH_SIZE);
+    private final Map entries = new LinkedHashMap(HASH_SIZE);
 
     /**
      * Maps String to ZipArchiveEntrys, name -> actual entry.
@@ -226,6 +229,10 @@
 
     /**
      * Returns all entries.
+     *
+     * <p>Entries will be returned in the same order they appear
+     * within the archive's central directory.</p>
+     *
      * @return all entries as {@link ZipArchiveEntry} instances
      */
     public Enumeration getEntries() {
@@ -233,6 +240,22 @@
     }
 
     /**
+     * Returns all entries in physical order.
+     *
+     * <p>Entries will be returned in the same order their contents
+     * appear within the archive.</p>
+     *
+     * @return all entries as {@link ZipArchiveEntry} instances
+     *
+     * @since Commons Compress 1.1
+     */
+    public Enumeration getEntriesInPhysicalOrder() {
+        Object[] allEntries = entries.keySet().toArray();
+        Arrays.sort(allEntries, OFFSET_COMPARATOR);
+        return Collections.enumeration(Arrays.asList(allEntries));
+    }
+
+    /**
      * Returns a named entry - or <code>null</code> if no entry by
      * that name exists.
      * @param name name of the entry.
@@ -655,4 +678,36 @@
             this.comment = comment;
         }
     }
+
+    /**
+     * Compares two ZupArchiveEntries based on their offset within the archive.
+     *
+     * <p>Won't return any meaningful results if one of the entries
+     * isn't part of the archive at all.</p>
+     *
+     * @since Commons Compress 1.1
+     */
+    private final Comparator OFFSET_COMPARATOR =
+        new Comparator() {
+            public int compare(Object o1, Object o2) {
+                if (o1 == o2)
+                    return 0;
+
+                ZipArchiveEntry e1 = (ZipArchiveEntry) o1;
+                ZipArchiveEntry e2 = (ZipArchiveEntry) o2;
+
+                OffsetEntry off1 = (OffsetEntry) entries.get(e1);
+                OffsetEntry off2 = (OffsetEntry) entries.get(e2);
+                if (off1 == null) {
+                    return 1;
+                }
+                if (off2 == null) {
+                    return -1;
+                }
+                return (int) Math.signum(off1.headerOffset - off2.headerOffset);
+            }
+            public boolean equals(Object o) {
+                return o == this;
+            }
+        };
 }

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java?rev=920286&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
(added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
Mon Mar  8 12:00:25 2010
@@ -0,0 +1,115 @@
+/*
+ *  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.commons.compress.archivers.zip;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import junit.framework.TestCase;
+
+public class ZipFileTest extends TestCase {
+    private ZipFile zf = null;
+
+    public void tearDown() {
+        ZipFile.closeQuietly(zf);
+    }
+
+    public void testCDOrder() throws Exception {
+        readOrderTest();
+        ArrayList l = Collections.list(zf.getEntries());
+        assertEntryName(l, 0, "AbstractUnicodeExtraField");
+        assertEntryName(l, 1, "AsiExtraField");
+        assertEntryName(l, 2, "ExtraFieldUtils");
+        assertEntryName(l, 3, "FallbackZipEncoding");
+        assertEntryName(l, 4, "GeneralPurposeBit");
+        assertEntryName(l, 5, "JarMarker");
+        assertEntryName(l, 6, "NioZipEncoding");
+        assertEntryName(l, 7, "Simple8BitZipEncoding");
+        assertEntryName(l, 8, "UnicodeCommentExtraField");
+        assertEntryName(l, 9, "UnicodePathExtraField");
+        assertEntryName(l, 10, "UnixStat");
+        assertEntryName(l, 11, "UnparseableExtraFieldData");
+        assertEntryName(l, 12, "UnrecognizedExtraField");
+        assertEntryName(l, 13, "ZipArchiveEntry");
+        assertEntryName(l, 14, "ZipArchiveInputStream");
+        assertEntryName(l, 15, "ZipArchiveOutputStream");
+        assertEntryName(l, 16, "ZipEncoding");
+        assertEntryName(l, 17, "ZipEncodingHelper");
+        assertEntryName(l, 18, "ZipExtraField");
+        assertEntryName(l, 19, "ZipUtil");
+        assertEntryName(l, 20, "ZipLong");
+        assertEntryName(l, 21, "ZipShort");
+        assertEntryName(l, 22, "ZipFile");
+    }
+
+    public void testPhysicalOrder() throws Exception {
+        readOrderTest();
+        ArrayList l = Collections.list(zf.getEntriesInPhysicalOrder());
+        assertEntryName(l, 0, "AbstractUnicodeExtraField");
+        assertEntryName(l, 1, "AsiExtraField");
+        assertEntryName(l, 2, "ExtraFieldUtils");
+        assertEntryName(l, 3, "FallbackZipEncoding");
+        assertEntryName(l, 4, "GeneralPurposeBit");
+        assertEntryName(l, 5, "JarMarker");
+        assertEntryName(l, 6, "NioZipEncoding");
+        assertEntryName(l, 7, "Simple8BitZipEncoding");
+        assertEntryName(l, 8, "UnicodeCommentExtraField");
+        assertEntryName(l, 9, "UnicodePathExtraField");
+        assertEntryName(l, 10, "UnixStat");
+        assertEntryName(l, 11, "UnparseableExtraFieldData");
+        assertEntryName(l, 12, "UnrecognizedExtraField");
+        assertEntryName(l, 13, "ZipArchiveEntry");
+        assertEntryName(l, 14, "ZipArchiveInputStream");
+        assertEntryName(l, 15, "ZipArchiveOutputStream");
+        assertEntryName(l, 16, "ZipEncoding");
+        assertEntryName(l, 17, "ZipEncodingHelper");
+        assertEntryName(l, 18, "ZipExtraField");
+        assertEntryName(l, 19, "ZipFile");
+        assertEntryName(l, 20, "ZipLong");
+        assertEntryName(l, 21, "ZipShort");
+        assertEntryName(l, 22, "ZipUtil");
+    }
+
+    /*
+     * ordertest.zip has been handcrafted.
+     *
+     * It contains enough files so any random coincidence of
+     * entries.keySet() and central directory order would be unlikely
+     * - in fact testCDOrder fails in svn revision 920284.
+     *
+     * The central directory has ZipFile and ZipUtil swapped so
+     * central directory order is different from entry data order.
+     */
+    private void readOrderTest() throws Exception {
+        URL zip = getClass().getResource("/ordertest.zip");
+        File archive = new File(new URI(zip.toString()));
+        zf = new ZipFile(archive);
+    }
+
+    private static void assertEntryName(ArrayList entries, int index,
+                                        String expectedName) {
+        ZipArchiveEntry ze = (ZipArchiveEntry) entries.get(index);
+        assertEquals("src/main/java/org/apache/commons/compress/archivers/zip/"
+                     + expectedName + ".java",
+                     ze.getName());
+    }
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/trunk/src/test/resources/ordertest.zip
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/ordertest.zip?rev=920286&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/compress/trunk/src/test/resources/ordertest.zip
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message