Author: bodewig
Date: Wed Feb 26 20:33:05 2014
New Revision: 1572240

URL: http://svn.apache.org/r1572240
Log:
COMPRESS-258 read back some of the method's options as well

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
    commons/proper/compress/trunk/src/site/xdoc/examples.xml
    
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.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=1572240&r1=1572239&r2=1572240&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Wed Feb 26 20:33:05 
2014
@@ -93,6 +93,10 @@ The <action> type attribute can be add,u
         The (compression) method(s) can now be specified per entry in
         SevenZOutputFile.
       </action>
+      <action issue="COMPRESS-258" type="add" date="2014-02-26">
+        SevenZArchiveEntry "knows" which method(s) have been used to
+        write it to the archive.
+      </action>
     </release>
     <release version="1.7" date="2014-01-20"
              description="Release 1.7">

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java?rev=1572240&r1=1572239&r2=1572240&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
 Wed Feb 26 20:33:05 2014
@@ -55,6 +55,13 @@ abstract class CoderBase {
     }
 
     /**
+     * @return configuration options that have been used to create the given 
InputStream from the given Coder
+     */
+    Object getOptionsFromCoder(Coder coder, InputStream in) {
+        return null;
+    }
+
+    /**
      * @return a stream that reads from in using the configured coder and 
password.
      */
     abstract InputStream decode(final InputStream in, final Coder coder, 
byte[] password) throws IOException;

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java?rev=1572240&r1=1572239&r2=1572240&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
 Wed Feb 26 20:33:05 2014
@@ -31,22 +31,13 @@ class LZMA2Decoder extends CoderBase {
     }
 
     @Override
-    InputStream decode(final InputStream in, final Coder coder, byte[] 
password)
-        throws IOException {
-        final int dictionarySizeBits = 0xff & coder.properties[0];
-        if ((dictionarySizeBits & (~0x3f)) != 0) {
-            throw new IOException("Unsupported LZMA2 property bits");
-        }
-        if (dictionarySizeBits > 40) {
-            throw new IOException("Dictionary larger than 4GiB maximum size");
+    InputStream decode(final InputStream in, final Coder coder, byte[] 
password) throws IOException {
+        try {
+            int dictionarySize = getDictionarySize(coder);
+            return new LZMA2InputStream(in, dictionarySize);
+        } catch (IllegalArgumentException ex) {
+            throw new IOException(ex.getMessage());
         }
-        final int dictionarySize;
-        if (dictionarySizeBits == 40) {
-            dictionarySize = 0xFFFFffff;
-        } else {
-            dictionarySize = (2 | (dictionarySizeBits & 0x1)) << 
(dictionarySizeBits / 2 + 11);
-        }
-        return new LZMA2InputStream(in, dictionarySize);
     }
 
     @Override
@@ -67,6 +58,11 @@ class LZMA2Decoder extends CoderBase {
         };
     }
 
+    @Override
+    Object getOptionsFromCoder(Coder coder, InputStream in) {
+        return getDictionarySize(coder);
+    }
+
     private int getDictSize(Object opts) {
         if (opts instanceof LZMA2Options) {
             return ((LZMA2Options) opts).getDictSize();
@@ -74,6 +70,20 @@ class LZMA2Decoder extends CoderBase {
         return numberOptionOrDefault(opts);
     }
 
+    private int getDictionarySize(Coder coder) throws IllegalArgumentException 
{
+        final int dictionarySizeBits = 0xff & coder.properties[0];
+        if ((dictionarySizeBits & (~0x3f)) != 0) {
+            throw new IllegalArgumentException("Unsupported LZMA2 property 
bits");
+        }
+        if (dictionarySizeBits > 40) {
+            throw new IllegalArgumentException("Dictionary larger than 4GiB 
maximum size");
+        }
+        if (dictionarySizeBits == 40) {
+            return 0xFFFFffff;
+        }
+        return (2 | (dictionarySizeBits & 0x1)) << (dictionarySizeBits / 2 + 
11);
+    }
+
     private LZMA2Options getOptions(Object opts) throws IOException {
         if (opts instanceof LZMA2Options) {
             return (LZMA2Options) opts;

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java?rev=1572240&r1=1572239&r2=1572240&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
 Wed Feb 26 20:33:05 2014
@@ -850,8 +850,9 @@ public class SevenZFile implements Close
                 throw new IOException("Multi input/output stream coders are 
not yet supported");
             }
             SevenZMethod method = 
SevenZMethod.byId(coder.decompressionMethodId);
-            methods.addFirst(new SevenZMethodConfiguration(method));
             inputStreamStack = Coders.addDecoder(inputStreamStack, coder, 
password);
+            methods.addFirst(new SevenZMethodConfiguration(method,
+                     Coders.findByMethod(method).getOptionsFromCoder(coder, 
inputStreamStack)));
         }
         entry.setContentMethods(methods);
         if (folder.hasCrc) {

Modified: commons/proper/compress/trunk/src/site/xdoc/examples.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/site/xdoc/examples.xml?rev=1572240&r1=1572239&r2=1572240&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/site/xdoc/examples.xml (original)
+++ commons/proper/compress/trunk/src/site/xdoc/examples.xml Wed Feb 26 
20:33:05 2014
@@ -502,6 +502,13 @@ lzmaIn.close();
         the methods - see the Javadocs of
         <code>SevenZMethodConfiguration</code> for details.</p>
 
+        <p>When reading entries from an archive the
+        <code>getContentMethods</code> method of
+        <code>SevenZArchiveEntry</code> will properly represent the
+        compression/encryption/filter methods but may fail to
+        determine the configuration options used.  As of Compress 1.8
+        only the dictionary size used for LZMA2 can be read.</p>
+
         <p>Currently solid compression - compressing multiple files
         as a single block to benefit from patterns repeating accross
         files - is only supported when reading archives.  This also

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=1572240&r1=1572239&r2=1572240&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
 Wed Feb 26 20:33:05 2014
@@ -20,6 +20,7 @@ package org.apache.commons.compress.arch
 import java.io.File;
 import java.io.IOException;
 import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
 import javax.crypto.Cipher;
 import org.apache.commons.compress.AbstractTestCase;
 
@@ -103,6 +104,31 @@ public class SevenZFileTest extends Abst
                                                     (byte) 0xAF, 0x27, 0x1D}, 
6));
     }
 
+    public void testReadingBackLZMA2DictSize() throws Exception {
+        File output = new File(dir, "lzma2-dictsize.7z");
+        SevenZOutputFile outArchive = new SevenZOutputFile(output);
+        try {
+            outArchive.setContentMethods(Arrays.asList(new 
SevenZMethodConfiguration(SevenZMethod.LZMA2, 1 << 20)));
+            SevenZArchiveEntry entry = new SevenZArchiveEntry();
+            entry.setName("foo.txt");
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(new byte[] { 'A' });
+            outArchive.closeArchiveEntry();
+        } finally {
+            outArchive.close();
+        }
+
+        SevenZFile archive = new SevenZFile(output);
+        try {
+            SevenZArchiveEntry entry = archive.getNextEntry();
+            SevenZMethodConfiguration m = 
entry.getContentMethods().iterator().next();
+            assertEquals(SevenZMethod.LZMA2, m.getMethod());
+            assertEquals(1 << 20, m.getOptions());
+        } finally {
+            archive.close();
+        }
+    }
+
     private void test7zUnarchive(File f, SevenZMethod m, byte[] password) 
throws Exception {
         SevenZFile sevenZFile = new SevenZFile(f, password);
         try {


Reply via email to