You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2013/10/21 17:46:00 UTC

svn commit: r1534234 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java

Author: bodewig
Date: Mon Oct 21 15:46:00 2013
New Revision: 1534234

URL: http://svn.apache.org/r1534234
Log:
defer creation of output stream until data is written in order to keep empty streams empty - LZMA2 writes dictionary information, for example

Added:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java   (with props)
Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java?rev=1534234&r1=1534233&r2=1534234&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java Mon Oct 21 15:46:00 2013
@@ -114,7 +114,6 @@ public class SevenZOutputFile {
     public void putArchiveEntry(final ArchiveEntry archiveEntry) throws IOException {
         final SevenZArchiveEntry entry = (SevenZArchiveEntry) archiveEntry;
         files.add(entry);
-        currentOutputStream = setupFileOutputStream();
     }
     
     /**
@@ -122,8 +121,10 @@ public class SevenZOutputFile {
      * @throws IOException
      */
     public void closeArchiveEntry() throws IOException {
-        currentOutputStream.flush();
-        currentOutputStream.close();
+        if (currentOutputStream != null) {
+            currentOutputStream.flush();
+            currentOutputStream.close();
+        }
 
         final SevenZArchiveEntry entry = files.get(files.size() - 1);
         if (fileBytesWritten > 0) {
@@ -140,6 +141,7 @@ public class SevenZOutputFile {
             entry.setCompressedSize(0);
             entry.setHasCrc(false);
         }
+        currentOutputStream = null;
         crc32.reset();
         compressedCrc32.reset();
         fileBytesWritten = 0;
@@ -151,7 +153,7 @@ public class SevenZOutputFile {
      * @throws IOException on error
      */
     public void write(final int b) throws IOException {
-        currentOutputStream.write(b);
+        getCurrentOutputStream().write(b);
     }
     
     /**
@@ -160,7 +162,7 @@ public class SevenZOutputFile {
      * @throws IOException on error
      */
     public void write(final byte[] b) throws IOException {
-        currentOutputStream.write(b);
+        getCurrentOutputStream().write(b);
     }
     
     /**
@@ -171,7 +173,7 @@ public class SevenZOutputFile {
      * @throws IOException on error
      */
     public void write(final byte[] b, final int off, final int len) throws IOException {
-        currentOutputStream.write(b, off, len);
+        getCurrentOutputStream().write(b, off, len);
     }
     
     /**
@@ -220,6 +222,18 @@ public class SevenZOutputFile {
         file.write(startHeaderBytes);
     }
     
+    /*
+     * Creation of output stream is deferred until data is actually
+     * written as some codecs might write header information even for
+     * empty streams and directories otherwise.
+     */
+    private OutputStream getCurrentOutputStream() throws IOException {
+        if (currentOutputStream == null) {
+            currentOutputStream = setupFileOutputStream();
+        }
+        return currentOutputStream;
+    }
+
     private CountingOutputStream setupFileOutputStream() throws IOException {
         OutputStream out = new OutputStreamWrapper();
         return new CountingOutputStream(Coders

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java?rev=1534234&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java (added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java Mon Oct 21 15:46:00 2013
@@ -0,0 +1,124 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.sevenz;
+
+import java.io.File;
+import java.util.Calendar;
+import java.util.Date;
+import org.apache.commons.compress.AbstractTestCase;
+
+public class SevenZOutputFileTest extends AbstractTestCase {
+
+    private File output;
+
+    public void tearDown() throws Exception {
+        if (output != null && !output.delete()) {
+            output.deleteOnExit();
+        }
+    }
+
+    public void testDirectoriesAndEmptyFiles() throws Exception {
+        File output = new File(dir, "empties.7z");
+
+        Date accessDate = new Date();
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.HOUR, -1);
+        Date creationDate = cal.getTime();
+
+        SevenZOutputFile outArchive = new SevenZOutputFile(output);
+        try {
+            SevenZArchiveEntry entry = outArchive.createArchiveEntry(dir, "foo/");
+            outArchive.putArchiveEntry(entry);
+            outArchive.closeArchiveEntry();
+
+            entry = new SevenZArchiveEntry();
+            entry.setName("foo/bar");
+            entry.setCreationDate(creationDate);
+            entry.setAccessDate(accessDate);
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(new byte[0]);
+            outArchive.closeArchiveEntry();
+
+            entry = new SevenZArchiveEntry();
+            entry.setName("xyzzy");
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(0);
+            outArchive.closeArchiveEntry();
+
+            entry = outArchive.createArchiveEntry(dir, "baz/");
+            entry.setAntiItem(true);
+            outArchive.putArchiveEntry(entry);
+            outArchive.closeArchiveEntry();
+
+            entry = new SevenZArchiveEntry();
+            entry.setName("dada");
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(5);
+            outArchive.closeArchiveEntry();
+
+            outArchive.finish();
+        } finally {
+            outArchive.close();
+        }
+
+        final SevenZFile archive = new SevenZFile(output);
+        try {
+            SevenZArchiveEntry entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals("foo/", entry.getName());
+            assertTrue(entry.isDirectory());
+            assertFalse(entry.isAntiItem());
+
+            entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals("foo/bar", entry.getName());
+            assertFalse(entry.isDirectory());
+            assertFalse(entry.isAntiItem());
+            assertEquals(0, entry.getSize());
+            assertFalse(entry.getHasLastModifiedDate());
+            assertEquals(accessDate, entry.getAccessDate());
+            assertEquals(creationDate, entry.getCreationDate());
+
+            entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals("xyzzy", entry.getName());
+            assertEquals(1, entry.getSize());
+            assertFalse(entry.getHasAccessDate());
+            assertFalse(entry.getHasCreationDate());
+            assertEquals(0, archive.read());
+
+            entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals("baz/", entry.getName());
+            assertTrue(entry.isDirectory());
+            assertTrue(entry.isAntiItem());
+
+            entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals("dada", entry.getName());
+            assertEquals(1, entry.getSize());
+            assertEquals(5, archive.read());
+
+            assert(archive.getNextEntry() == null);
+        } finally {
+            archive.close();
+        }
+
+    }
+
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
------------------------------------------------------------------------------
    svn:eol-style = native