commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1570248 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java test/java/org/apache/commons/compress/compressors/GZipTestCase.java
Date Thu, 20 Feb 2014 16:03:23 GMT
Author: bodewig
Date: Thu Feb 20 16:03:23 2014
New Revision: 1570248

URL: http://svn.apache.org/r1570248
Log:
COMPRESS-260 provide read access to GZIP metadata

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.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=1570248&r1=1570247&r2=1570248&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Thu Feb 20 16:03:23 2014
@@ -57,6 +57,11 @@ The <action> type attribute can be add,u
         CompressorStreamFactory.createCompressorInputStream with
         explicit compression did not honor decompressConcatenated
       </action>
+      <action issue="COMPRESS-260" type="add" date="2014-02-20">
+        GzipCompressorInputStream now provides access to the same
+        metadata that can be provided via GzipParameters when writing
+        a gzip stream.
+      </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/compressors/gzip/GzipCompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java?rev=1570248&r1=1570247&r2=1570248&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java
Thu Feb 20 16:03:23 2014
@@ -18,12 +18,14 @@
  */
 package org.apache.commons.compress.compressors.gzip;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.EOFException;
 import java.io.InputStream;
 import java.io.DataInputStream;
 import java.io.BufferedInputStream;
 import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
 import java.util.zip.Inflater;
 import java.util.zip.CRC32;
 
@@ -79,6 +81,8 @@ public class GzipCompressorInputStream e
     // used in no-arg read method
     private final byte[] oneByte = new byte[1];
 
+    private final GzipParameters parameters = new GzipParameters();
+
     /**
      * Constructs a new input stream that decompresses gzip-compressed data
      * from the specified input stream.
@@ -132,6 +136,16 @@ public class GzipCompressorInputStream e
         init(true);
     }
 
+    /**
+     * Provides the stream's meta data - may change with each stream
+     * when decompressing concatenated streams.
+     * @return the stream's meta data
+     * @since 1.8
+     */
+    public GzipParameters getMetaData() {
+        return parameters;
+    }
+
     private boolean init(boolean isFirstMember) throws IOException {
         assert isFirstMember || decompressConcatenated;
 
@@ -154,7 +168,7 @@ public class GzipCompressorInputStream e
         // Parsing the rest of the header may throw EOFException.
         DataInputStream inData = new DataInputStream(in);
         int method = inData.readUnsignedByte();
-        if (method != 8) {
+        if (method != Deflater.DEFLATED) {
             throw new IOException("Unsupported compression method "
                                   + method + " in the .gz header");
         }
@@ -165,9 +179,19 @@ public class GzipCompressorInputStream e
                     "Reserved flags are set in the .gz header");
         }
 
-        inData.readInt(); // mtime, ignored
-        inData.readUnsignedByte(); // extra flags, ignored
-        inData.readUnsignedByte(); // operating system, ignored
+        parameters.setModificationTime(readLittleEndianInt(inData) * 1000);
+        switch (inData.readUnsignedByte()) { // extra flags
+        case 2:
+            parameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
+            break;
+        case 4:
+            parameters.setCompressionLevel(Deflater.BEST_SPEED);
+            break;
+        default:
+            // ignored for now
+            break;
+        }
+        parameters.setOperatingSystem(inData.readUnsignedByte());
 
         // Extra field, ignored
         if ((flg & FEXTRA) != 0) {
@@ -182,14 +206,14 @@ public class GzipCompressorInputStream e
             }
         }
 
-        // Original file name, ignored
+        // Original file name
         if ((flg & FNAME) != 0) {
-            readToNull(inData);
+            parameters.setFilename(new String(readToNull(inData), "ISO-8859-1"));
         }
 
-        // Comment, ignored
+        // Comment
         if ((flg & FCOMMENT) != 0) {
-            readToNull(inData);
+            parameters.setComment(new String(readToNull(inData), "ISO-8859-1"));
         }
 
         // Header "CRC16" which is actually a truncated CRC32 (which isn't
@@ -209,9 +233,20 @@ public class GzipCompressorInputStream e
         return true;
     }
 
-    private void readToNull(DataInputStream inData) throws IOException {
-        while (inData.readUnsignedByte() != 0x00) { // NOPMD
+    private byte[] readToNull(DataInputStream inData) throws IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        int b = 0;
+        while ((b = inData.readUnsignedByte()) != 0x00) { // NOPMD
+            bos.write(b);
         }
+        return bos.toByteArray();
+    }
+
+    private int readLittleEndianInt(DataInputStream inData) throws IOException {
+        return inData.readUnsignedByte()
+            | (inData.readUnsignedByte() << 8)
+            | (inData.readUnsignedByte() << 16)
+            | (inData.readUnsignedByte() << 24);
     }
 
     @Override

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java?rev=1570248&r1=1570247&r2=1570248&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java
Thu Feb 20 16:03:23 2014
@@ -269,4 +269,33 @@ public final class GZipTestCase extends 
             // expected
         }
     }
+
+    public void testMetadataRoundTrip() throws Exception {
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+                
+        GzipParameters parameters = new GzipParameters();
+        parameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
+        parameters.setModificationTime(123456000);
+        parameters.setOperatingSystem(13);
+        parameters.setFilename("test3.xml");
+        parameters.setComment("Umlaute möglich?");
+        GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);
+        FileInputStream fis = new FileInputStream(getFile("test3.xml"));
+        try {
+            IOUtils.copy(fis, out);
+        } finally {
+            fis.close();
+            out.close();
+        }
+        
+        GzipCompressorInputStream input =
+            new GzipCompressorInputStream(new ByteArrayInputStream(bout.toByteArray()));
+        input.close();
+        GzipParameters readParams = input.getMetaData();
+        assertEquals(Deflater.BEST_COMPRESSION, readParams.getCompressionLevel());
+        assertEquals(123456000, readParams.getModificationTime());
+        assertEquals(13, readParams.getOperatingSystem());
+        assertEquals("test3.xml", readParams.getFilename());
+        assertEquals("Umlaute möglich?", readParams.getComment());
+    }
 }



Mime
View raw message