You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/08/08 20:02:36 UTC

[commons-compress] 01/03: Add SevenZOutputFile.write(InputStream).

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 379f75ace59e52853aae91c591aeaee20e3f3c79
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Aug 8 15:45:56 2020 -0400

    Add SevenZOutputFile.write(InputStream).
---
 src/changes/changes.xml                            |  3 ++
 .../archivers/sevenz/SevenZOutputFile.java         | 17 +++++++
 .../archivers/sevenz/SevenZOutputFileTest.java     | 55 ++++++++++++++++++++++
 3 files changed, 75 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f1f303d..58d23c5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -130,6 +130,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="COMPRESS-539" type="update" date="2020-07-04" due-to="Robin Schimpf">
         Reuse the record buffer in TarArchiveInputStream.
       </action>
+      <action type="update" date="2020-08-08" due-to="Gary Gregory" dev="ggregory">
+        Add SevenZOutputFile.write(InputStream).
+      </action>
       <action type="update" date="2020-07-23" due-to="Dependabot" dev="ggregory">
         Update GitHub actions/checkout from v1 to v2.3.1 #114.
       </action>
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
index 0e43769..28a7e6c 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
@@ -17,12 +17,14 @@
  */
 package org.apache.commons.compress.archivers.sevenz;
 
+import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.DataOutput;
 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
@@ -243,6 +245,20 @@ public class SevenZOutputFile implements Closeable {
     }
 
     /**
+     * Writes all of the given input stream to the current archive entry.
+     * @param inputStream the data source.
+     * @throws IOException if an I/O error occurs.
+     * @since 1.21
+     */
+    public void write(final InputStream inputStream) throws IOException {
+        final byte[] buffer = new byte[8024];
+        int n = 0;
+        while (-1 != (n = inputStream.read(buffer))) {
+            write(buffer, 0, n);
+        }
+    }
+
+    /**
      * Finishes the addition of entries to this archive, without closing it.
      *
      * @throws IOException if archive is already closed.
@@ -810,4 +826,5 @@ public class SevenZOutputFile implements Closeable {
             // the file will be closed by the containing class's close method
         }
     }
+
 }
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
index 63836ae..638d9d2 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
@@ -20,6 +20,7 @@ package org.apache.commons.compress.archivers.sevenz;
 import static org.junit.Assert.*;
 import org.junit.Test;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
@@ -77,6 +78,30 @@ public class SevenZOutputFileTest extends AbstractTestCase {
             outArchive.closeArchiveEntry();
 
             entry = new SevenZArchiveEntry();
+            entry.setName("foo/bar/boo0");
+            entry.setCreationDate(creationDate);
+            entry.setAccessDate(accessDate);
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(new ByteArrayInputStream(new byte[0]));
+            outArchive.closeArchiveEntry();
+
+            entry = new SevenZArchiveEntry();
+            entry.setName("foo/bar/boo1");
+            entry.setCreationDate(creationDate);
+            entry.setAccessDate(accessDate);
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(new ByteArrayInputStream(new byte[] {'a'}));
+            outArchive.closeArchiveEntry();
+
+            entry = new SevenZArchiveEntry();
+            entry.setName("foo/bar/boo10000");
+            entry.setCreationDate(creationDate);
+            entry.setAccessDate(accessDate);
+            outArchive.putArchiveEntry(entry);
+            outArchive.write(new ByteArrayInputStream(new byte[10000]));
+            outArchive.closeArchiveEntry();
+
+            entry = new SevenZArchiveEntry();
             entry.setName("xyzzy");
             outArchive.putArchiveEntry(entry);
             outArchive.write(0);
@@ -118,6 +143,36 @@ public class SevenZOutputFileTest extends AbstractTestCase {
 
             entry = archive.getNextEntry();
             assert (entry != null);
+            assertEquals("foo/bar/boo0", 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("foo/bar/boo1", entry.getName());
+            assertFalse(entry.isDirectory());
+            assertFalse(entry.isAntiItem());
+            assertEquals(1, entry.getSize());
+            assertFalse(entry.getHasLastModifiedDate());
+            assertEquals(accessDate, entry.getAccessDate());
+            assertEquals(creationDate, entry.getCreationDate());
+
+            entry = archive.getNextEntry();
+            assert (entry != null);
+            assertEquals("foo/bar/boo10000", entry.getName());
+            assertFalse(entry.isDirectory());
+            assertFalse(entry.isAntiItem());
+            assertEquals(10000, 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());