commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1586869 - in /commons/proper/compress/trunk/src: changes/ main/java/org/apache/commons/compress/compressors/ main/java/org/apache/commons/compress/compressors/z/ test/java/org/apache/commons/compress/compressors/
Date Sat, 12 Apr 2014 16:16:53 GMT
Author: bodewig
Date: Sat Apr 12 16:16:53 2014
New Revision: 1586869

URL: http://svn.apache.org/r1586869
Log:
COMPRESS-272 add auto-detect for .Z

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.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=1586869&r1=1586868&r2=1586869&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sat Apr 12 16:16:53 2014
@@ -51,6 +51,10 @@ The <action> type attribute can be add,u
         The snappy, ar and tar inputstreams might fail to read from a
         non-buffered stream in certain cases.
       </action>
+      <action type="add" date="2014-04-12" issue="COMPRESS-272">
+        CompressorStreamFactory can now auto-detect Unix compress
+        (".Z") streams.
+      </action>
     </release>
     <release version="1.8" date="2014-03-12"
              description="Release 1.8">

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java?rev=1586869&r1=1586868&r2=1586869&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
Sat Apr 12 16:16:53 2014
@@ -179,6 +179,10 @@ public class CompressorStreamFactory {
                 return new FramedSnappyCompressorInputStream(in);
             }
 
+            if (ZCompressorInputStream.matches(signature, signatureLength)) {
+                return new ZCompressorInputStream(in);
+            }
+
         } catch (IOException e) {
             throw new CompressorException("Failed to detect Compressor from InputStream.",
e);
         }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java?rev=1586869&r1=1586868&r2=1586869&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
Sat Apr 12 16:16:53 2014
@@ -149,4 +149,20 @@ public class ZCompressorInputStream exte
         }
     }
     
+    /**
+     * Checks if the signature matches what is expected for a Unix compress file.
+     * 
+     * @param signature
+     *            the bytes to check
+     * @param length
+     *            the number of bytes to check
+     * @return true, if this stream is a Unix compress compressed
+     * stream, false otherwise
+     * 
+     * @since 1.9
+     */
+    public static boolean matches(byte[] signature, int length) {
+        return length > 3 && signature[0] == MAGIC_1 && signature[1] ==
(byte) MAGIC_2;
+    }
+
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java?rev=1586869&r1=1586868&r2=1586869&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
Sat Apr 12 16:16:53 2014
@@ -18,6 +18,7 @@
  */
 package org.apache.commons.compress.compressors;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -47,6 +48,26 @@ public final class ZTestCase extends Abs
         });
     }
 
+    public void testZUnarchiveViaAutoDetection() throws Exception {
+        testUnarchive(new StreamWrapper<CompressorInputStream>() {
+            public CompressorInputStream wrap(InputStream is) throws Exception {
+                return new CompressorStreamFactory()
+                    .createCompressorInputStream(new BufferedInputStream(is));
+            }
+        });
+    }
+
+    public void testMatches() throws Exception {
+        assertFalse(ZCompressorInputStream.matches(new byte[] { 1, 2, 3, 4 }, 4));
+        assertFalse(ZCompressorInputStream.matches(new byte[] { 0x1f, 2, 3, 4 }, 4));
+        assertFalse(ZCompressorInputStream.matches(new byte[] { 1, (byte)0x9d, 3, 4 },
+                                                   4));
+        assertFalse(ZCompressorInputStream.matches(new byte[] { 0x1f, (byte) 0x9d, 3, 4 },
+                                                   3));
+        assertTrue(ZCompressorInputStream.matches(new byte[] { 0x1f, (byte) 0x9d, 3, 4 },
+                                                  4));
+    }
+
     private void testUnarchive(StreamWrapper<CompressorInputStream> wrapper) throws
Exception {
         final File input = getFile("bla.tar.Z");
         final File output = new File(dir, "bla.tar");



Mime
View raw message