Repository: commons-compress Updated Branches: refs/heads/master d8d92941d -> 12d9048c6
provide pointers to SeekableInMemoryByteChannel in several places Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/12d9048c Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/12d9048c Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/12d9048c Branch: refs/heads/master Commit: 12d9048c6c09ab622f389c2550d0668dc7ad26dc Parents: d8d9294 Author: Stefan Bodewig <bode...@apache.org> Authored: Sat Oct 29 18:10:38 2016 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sat Oct 29 18:10:38 2016 +0200 ---------------------------------------------------------------------- .../compress/archivers/sevenz/SevenZFile.java | 23 +++++++++++++++++ .../archivers/sevenz/SevenZOutputFile.java | 4 +++ .../archivers/zip/ZipArchiveOutputStream.java | 8 +++++- .../commons/compress/archivers/zip/ZipFile.java | 26 +++++++++++++++++++- src/site/xdoc/examples.xml | 7 ++++++ src/site/xdoc/zip.xml | 11 ++++++++- .../archivers/sevenz/SevenZFileTest.java | 2 +- .../compress/archivers/zip/ZipFileTest.java | 3 +-- 8 files changed, 78 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java index 21e5676..c2f7999 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java @@ -104,6 +104,29 @@ public class SevenZFile implements Closeable { /** * Reads a SeekableByteChannel as 7z archive * + * <p>{@link + * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} + * allows you to read from an in-memory archive.</p> + * + * @param channel the channel to read + * @param password optional password if the archive is encrypted - + * the byte array is supposed to be the UTF16-LE encoded + * representation of the password. + * @throws IOException if reading the archive fails + * @since 1.13 + */ + public SevenZFile(final SeekableByteChannel channel, + final byte[] password) throws IOException { + this(channel, "unknown archive", password); + } + + /** + * Reads a SeekableByteChannel as 7z archive + * + * <p>{@link + * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} + * allows you to read from an in-memory archive.</p> + * * @param channel the channel to read * @param filename name of the archive - only used for error reporting * @param password optional password if the archive is encrypted - http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java ---------------------------------------------------------------------- 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 0ca9452..f0080d1 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 @@ -76,6 +76,10 @@ public class SevenZOutputFile implements Closeable { /** * Prepares channel to write a 7z archive to. * + * <p>{@link + * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} + * allows you to write to an in-memory archive.</p> + * * @param channel the channel to write to * @throws IOException if the channel cannot be positioned properly * @since 1.13 http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java index 71a9267..4655e3c 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java @@ -59,7 +59,7 @@ import static org.apache.commons.compress.archivers.zip.ZipShort.putShort; * data and central directory entries. * * <p>This class will try to use {@link - * java.nio.channels.SeekableByteChannel} when you know that the + * java.nio.channels.SeekableByteChannel} when it knows that the * output is going to go to a file.</p> * * <p>If SeekableByteChannel cannot be used, this implementation will use @@ -306,6 +306,12 @@ public class ZipArchiveOutputStream extends ArchiveOutputStream { /** * Creates a new ZIP OutputStream writing to a SeekableByteChannel. + * + * <p>{@link + * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} + * allows you to write to an in-memory archive using random + * access.</p> + * * @param channel the channel to zip to * @throws IOException on error * @since 1.13 http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java index e0d2cea..cff0f4b 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java @@ -220,9 +220,33 @@ public class ZipFile implements Closeable { } /** - * Opens the given file for reading, assuming the specified + * Opens the given channel for reading, assuming the specified + * encoding for file names. + * + * <p>{@link + * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} + * allows you to read from an in-memory archive.</p> + * + * @param channel the archive. + * @param encoding the encoding to use for file names, use null + * for the platform's default encoding + * + * @throws IOException if an error occurs while reading the file. + * @since 1.13 + */ + public ZipFile(final SeekableByteChannel channel, final String encoding) + throws IOException { + this(channel, "unknown archive", encoding, true); + } + + /** + * Opens the given channel for reading, assuming the specified * encoding for file names. * + * <p>{@link + * org.apache.commons.compress.utils.SeekableInMemoryByteChannel} + * allows you to read from an in-memory archive.</p> + * * @param channel the archive. * @param archiveName name of the archive, used for error messages only. * @param encoding the encoding to use for file names, use null http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/site/xdoc/examples.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/examples.xml b/src/site/xdoc/examples.xml index be23e6b..6b22a1a 100644 --- a/src/site/xdoc/examples.xml +++ b/src/site/xdoc/examples.xml @@ -579,6 +579,13 @@ defIn.close(); means compression ratio will likely be worse when using Commons Compress compared to the native 7z executable.</p> + <p>Reading or writing requires a + <code>SeekableByteChannel</code> that will be obtain + transparently when reading from or writing to a file. The + class + <code>org.apache.commons.compress.utils.SeekableInMemoryByteChannel</code> + allows you to read from or write to an in-memory archive.</p> + <p>Adding an entry to a 7z archive:</p> <source><![CDATA[ SevenZOutputFile sevenZOutput = new SevenZOutputFile(file); http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/site/xdoc/zip.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/zip.xml b/src/site/xdoc/zip.xml index 558e8ea..6f08ba6 100644 --- a/src/site/xdoc/zip.xml +++ b/src/site/xdoc/zip.xml @@ -104,6 +104,12 @@ <p>If possible, you should always prefer <code>ZipFile</code> over <code>ZipArchiveInputStream</code>.</p> + <p><code>ZipFile</code> requires a + <code>SeekableByteChannel</code> that will be obtain + transparently when reading from a file. The class + <code>org.apache.commons.compress.utils.SeekableInMemoryByteChannel</code> + allows you to read from an in-memory archive.</p> + </subsection> <subsection name="ZipArchiveOutputStream" id="ZipArchiveOutputStream"> @@ -126,7 +132,10 @@ <p>If you know you are writing to a file, you should always prefer the <code>File</code>- or - <code>SeekableByteChannel</code>-arg constructors.</p> + <code>SeekableByteChannel</code>-arg constructors. The class + <code>org.apache.commons.compress.utils.SeekableInMemoryByteChannel</code> + allows you to write to an in-memory archive.</p> + </subsection> <subsection name="Extra Fields"> http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java index d066bea..01efce7 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java @@ -249,7 +249,7 @@ public class SevenZFileTest extends AbstractTestCase { data = IOUtils.toByteArray(fis); } try (SevenZFile sevenZFile = new SevenZFile(new SeekableInMemoryByteChannel(data), - "in memory", null)) { + null)) { final Iterable<SevenZArchiveEntry> entries = sevenZFile.getEntries(); final Iterator<SevenZArchiveEntry> iter = entries.iterator(); SevenZArchiveEntry entry = iter.next(); http://git-wip-us.apache.org/repos/asf/commons-compress/blob/12d9048c/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java index c786549..ee7b26f 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java @@ -82,8 +82,7 @@ public class ZipFileTest { data = IOUtils.toByteArray(fis); } - zf = new ZipFile(new SeekableInMemoryByteChannel(data), "in memory", - ZipEncodingHelper.UTF8, true); + zf = new ZipFile(new SeekableInMemoryByteChannel(data), ZipEncodingHelper.UTF8); final ArrayList<ZipArchiveEntry> l = Collections.list(zf.getEntries()); assertEntryName(l, 0, "AbstractUnicodeExtraField"); assertEntryName(l, 1, "AsiExtraField");