commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1149796 - in /commons/proper/compress/trunk/src: changes/ main/java/org/apache/commons/compress/archivers/zip/ test/java/org/apache/commons/compress/archivers/zip/
Date Sat, 23 Jul 2011 04:33:46 GMT
Author: bodewig
Date: Sat Jul 23 04:33:44 2011
New Revision: 1149796

URL: http://svn.apache.org/viewvc?rev=1149796&view=rev
Log:
Provide access to the raw bytes that made up the ZIP entry's name.  COMPRESS-123

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.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=1149796&r1=1149795&r2=1149796&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sat Jul 23 04:33:44 2011
@@ -45,6 +45,11 @@ The <action> type attribute can be add,u
   </properties>
   <body>
     <release version="1.2" date="as in SVN" description="Release 1.2">
+      <action issue="COMPRESS-123" type="add" date="2011-07.23">
+        ZipArchiveEntry has a new method getRawName that provides the
+        original bytes that made up the name.  This may allow user
+        code to detect the encoding.
+      </action>
       <action issue="COMPRESS-130" type="fix" date="2011-07-20">
         The Javadoc for ZipArchiveInputStream#skip now matches the
         implementation, the code has been made more defensive.

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java?rev=1149796&r1=1149795&r2=1149796&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
Sat Jul 23 04:33:44 2011
@@ -74,6 +74,7 @@ public class ZipArchiveEntry extends jav
     private LinkedHashMap/*<ZipShort, ZipExtraField>*/ extraFields = null;
     private UnparseableExtraFieldData unparseableExtra = null;
     private String name = null;
+    private byte[] rawName = null;
     private GeneralPurposeBit gpb = new GeneralPurposeBit();
 
     /**
@@ -496,6 +497,34 @@ public class ZipArchiveEntry extends jav
     }
 
     /**
+     * Package private setter that sets the name using the raw bytes
+     * and the string created from it by guessing or suing the
+     * configured encoding.
+     */
+    void setName(String name, byte[] rawName) {
+        setName(name);
+        this.rawName = rawName;
+    }
+
+    /**
+     * Returns the raw bytes that made up the name before it has been
+     * converted using the configured or guessed encoding.
+     *
+     * <p>This method will return null if this instance has not been
+     * read from an archive.</p>
+     *
+     * @since Apache Commons Compress 1.2
+     */
+    public byte[] getRawName() {
+        if (rawName != null) {
+            byte[] b = new byte[rawName.length];
+            System.arraycopy(rawName, 0, b, 0, rawName.length);
+            return b;
+        }
+        return null;
+    }
+
+    /**
      * Get the hashCode of the entry.
      * This uses the name as the hashcode.
      * @return a hashcode.

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=1149796&r1=1149795&r2=1149796&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
Sat Jul 23 04:33:44 2011
@@ -195,7 +195,7 @@ public class ZipArchiveInputStream exten
 
         byte[] fileName = new byte[fileNameLen];
         readFully(fileName);
-        current.setName(entryEncoding.decode(fileName));
+        current.setName(entryEncoding.decode(fileName), fileName);
 
         byte[] extraData = new byte[extraLen];
         readFully(extraData);

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=1149796&r1=1149795&r2=1149796&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
Sat Jul 23 04:33:44 2011
@@ -450,7 +450,7 @@ public class ZipFile {
 
             byte[] fileName = new byte[fileNameLen];
             archive.readFully(fileName);
-            ze.setName(entryEncoding.decode(fileName));
+            ze.setName(entryEncoding.decode(fileName), fileName);
 
             // LFH offset,
             OffsetEntry offset = new OffsetEntry();

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java?rev=1149796&r1=1149795&r2=1149796&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
Sat Jul 23 04:33:44 2011
@@ -192,6 +192,35 @@ public class UTF8ZipFilesTest extends Ab
         }
     }
 
+    public void testRawNameReadFromZipFile()
+        throws IOException, URISyntaxException {
+        URL zip = getClass().getResource("/utf8-7zip-test.zip");
+        File archive = new File(new URI(zip.toString()));
+        ZipFile zf = null;
+        try {
+            zf = new ZipFile(archive, CP437, false);
+            assertRawNameOfAcsiiTxt(zf.getEntry(ASCII_TXT));
+        } finally {
+            ZipFile.closeQuietly(zf);
+        }
+    }
+
+    public void testRawNameReadFromStream()
+        throws IOException, URISyntaxException {
+        URL zip = getClass().getResource("/utf8-7zip-test.zip");
+        FileInputStream archive =
+            new FileInputStream(new File(new URI(zip.toString())));
+        ZipArchiveInputStream zi = null;
+        try {
+            zi = new ZipArchiveInputStream(archive, CP437, false);
+            assertRawNameOfAcsiiTxt((ZipArchiveEntry) zi.getNextEntry());
+        } finally {
+            if (zi != null) {
+                zi.close();
+            }
+        }
+    }
+
     private static void testFileRoundtrip(String encoding, boolean withEFS,
                                           boolean withExplicitUnicodeExtra)
         throws IOException {
@@ -343,5 +372,15 @@ public class UTF8ZipFilesTest extends Ab
 
     }
 
+    private static void assertRawNameOfAcsiiTxt(ZipArchiveEntry ze) {
+        byte[] b = ze.getRawName();
+        assertNotNull(b);
+        final int len = ASCII_TXT.length();
+        assertEquals(len, b.length);
+        for (int i = 0; i < len; i++) {
+            assertEquals("Byte " + i, (byte) ASCII_TXT.charAt(i), b[i]);
+        }
+        assertNotSame(b, ze.getRawName());
+    }
 }
 



Mime
View raw message