Author: bodewig
Date: Fri Oct  4 16:07:23 2013
New Revision: 1529202

URL: http://svn.apache.org/r1529202
Log:
deflate support when reading 7Z archives

Added:
    commons/proper/compress/trunk/src/test/resources/bla.deflate.7z   (with 
props)
Modified:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
    
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java?rev=1529202&r1=1529201&r2=1529202&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
 Fri Oct  4 16:07:23 2013
@@ -17,12 +17,15 @@
  */
 package org.apache.commons.compress.archivers.sevenz;
 
+import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.GeneralSecurityException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
+import java.util.zip.Inflater;
+import java.util.zip.InflaterInputStream;
 
 import javax.crypto.Cipher;
 import javax.crypto.CipherInputStream;
@@ -50,8 +53,7 @@ class Coders {
         new CoderId(new byte[] { (byte)0x00 }, new CopyDecoder()),
         new CoderId(new byte[] { (byte)0x03, (byte)0x01, (byte)0x01 }, new 
LZMADecoder()),
         new CoderId(new byte[] { (byte)0x21 }, new LZMA2Decoder()),
-        // FIXME: gives corrupt output
-        //new CoderId(new byte[] { (byte)0x04, (byte)0x01, (byte)0x08 }, new 
DeflateDecoder()),
+        new CoderId(new byte[] { (byte)0x04, (byte)0x01, (byte)0x08 }, new 
DeflateDecoder()),
         new CoderId(new byte[] { (byte)0x04, (byte)0x02, (byte)0x02 }, new 
BZIP2Decoder()),
         new CoderId(new byte[] { (byte)0x06, (byte)0xf1, (byte)0x07, 
(byte)0x01 }, new AES256SHA256Decoder())
     };
@@ -116,15 +118,14 @@ class Coders {
         }
     }
     
-//    static class DeflateDecoder extends CoderBase {
-//        @Override
-//        InputStream decode(final InputStream in, final Coder coder, final 
String password)
-//                throws IOException {
-//            System.out.println("deflate prop count = " + (coder.properties 
== null ? -1 : coder.properties.length));
-//            return new DeflaterInputStream(in, new 
Deflater(Deflater.DEFAULT_COMPRESSION, true));
-//            //return new GZIPInputStream(in);
-//        }
-//    }
+    static class DeflateDecoder extends CoderBase {
+        @Override
+        InputStream decode(final InputStream in, final Coder coder, final 
String password)
+            throws IOException {
+            return new InflaterInputStream(new DummyByteAddingInputStream(in),
+                                           new Inflater(true));
+        }
+    }
 
     static class BZIP2Decoder extends CoderBase {
         @Override
@@ -232,4 +233,39 @@ class Coders {
             };
         }
     }
+
+    /**
+     * ZLIB requires an extra dummy byte.
+     *
+     * @see java.util.zip.Inflater#Inflater(boolean)
+     * @see 
org.apache.commons.compress.archivers.zip.ZipFile.BoundedInputStream
+     */
+    private static class DummyByteAddingInputStream extends FilterInputStream {
+        private boolean addDummyByte = true;
+
+        private DummyByteAddingInputStream(InputStream in) {
+            super(in);
+        }
+
+        @Override
+        public int read() throws IOException {
+            int result = super.read();
+            if (result == -1 && addDummyByte) {
+                addDummyByte = false;
+                result = 0;
+            }
+            return result;
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) throws IOException {
+            int result = super.read(b, off, len);
+            if (result == -1 && addDummyByte) {
+                addDummyByte = false;
+                b[off] = 0;
+                return 1;
+            }
+            return result;
+        }
+    }
 }

Modified: 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java?rev=1529202&r1=1529201&r2=1529202&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
 (original)
+++ 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
 Fri Oct  4 16:07:23 2013
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.compress.archivers.sevenz;
 
+import java.io.File;
 import org.apache.commons.compress.AbstractTestCase;
 
 public class SevenZFileTest extends AbstractTestCase {
@@ -41,7 +42,15 @@ public class SevenZFileTest extends Abst
     }
 
     public void test7zUnarchive() throws Exception {
-        SevenZFile sevenZFile = new SevenZFile(getFile("bla.7z"));
+        test7zUnarchive(getFile("bla.7z"));
+    }
+
+    public void test7zDeflateUnarchive() throws Exception {
+        test7zUnarchive(getFile("bla.deflate.7z"));
+    }
+
+    private void test7zUnarchive(File f) throws Exception {
+        SevenZFile sevenZFile = new SevenZFile(f);
         try {
             SevenZArchiveEntry entry = sevenZFile.getNextEntry();
             assertEquals("test1.xml", entry.getName());

Added: commons/proper/compress/trunk/src/test/resources/bla.deflate.7z
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/bla.deflate.7z?rev=1529202&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/compress/trunk/src/test/resources/bla.deflate.7z
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream


Reply via email to