Repository: commons-compress Updated Branches: refs/heads/master 4da56433b -> 77addfc26
COMPRESS-452 example for reading an encrypted archive Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/77addfc2 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/77addfc2 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/77addfc2 Branch: refs/heads/master Commit: 77addfc268746a5b4b5abbbcef1cb3e6510690b1 Parents: 4da5643 Author: Stefan Bodewig <[email protected]> Authored: Mon May 7 08:36:52 2018 +0200 Committer: Stefan Bodewig <[email protected]> Committed: Mon May 7 08:36:52 2018 +0200 ---------------------------------------------------------------------- src/site/xdoc/examples.xml | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/77addfc2/src/site/xdoc/examples.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/examples.xml b/src/site/xdoc/examples.xml index 80b52d7..7c26f3a 100644 --- a/src/site/xdoc/examples.xml +++ b/src/site/xdoc/examples.xml @@ -191,7 +191,33 @@ SevenZFile sevenZFile = new SevenZFile(inMemoryByteChannel); SevenZArchiveEntry entry = sevenZFile.getNextEntry(); sevenZFile.read(); // read current entry's data ]]></source> - </subsection> + + <h4><a name="Encrypted 7z Archives"></a>Encrypted 7z Archives</h4> + + <p>Currently Compress supports reading but not writing of + encrypted archives. When reading an encrypted archive a + password has to be provided to one of + <code>SevenZFile</code>'s constructors. If you try to read + an encrypted archive without specifying a password a + <code>PasswordRequiredException</code> (a subclass of + <code>IOException</code>) will be thrown.</p> + + <p>When specifying the password as a <code>byte[]</code> one + common mistake is to use the wrong encoding when creating + the <code>byte[]</code> from a <code>String</code>. The + <code>SevenZFile</code> class expects the bytes to + correspond to the UTF16-LE encoding of the password. An + example of reading an encrypted archive is</p> + +<source><![CDATA[ +SevenZFile sevenZFile = new SevenZFile(new File("archive.7z"), "secret".getBytes(StandardCharsets.UTF_16LE)); +SevenZArchiveEntry entry = sevenZFile.getNextEntry(); +byte[] content = new byte[entry.getSize()]; +LOOP UNTIL entry.getSize() HAS BEEN READ { + sevenZFile.read(content, offset, content.length - offset); +} +]]></source> + </subsection> <subsection name="ar">
