Repository: commons-compress Updated Branches: refs/heads/master 9c3c448cf -> a29131675
CMPRESS-118 make 7z creator API public Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/0bb584bb Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/0bb584bb Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/0bb584bb Branch: refs/heads/master Commit: 0bb584bb8cae3ca8fc221ee0411a89100e24ae39 Parents: 9c3c448 Author: Stefan Bodewig <[email protected]> Authored: Mon Apr 23 18:05:40 2018 +0200 Committer: Stefan Bodewig <[email protected]> Committed: Mon Apr 23 18:05:40 2018 +0200 ---------------------------------------------------------------------- .../commons/compress/archivers/Archiver.java | 36 +++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0bb584bb/src/main/java/org/apache/commons/compress/archivers/Archiver.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/Archiver.java b/src/main/java/org/apache/commons/compress/archivers/Archiver.java index c576fc0..d1f24a8 100644 --- a/src/main/java/org/apache/commons/compress/archivers/Archiver.java +++ b/src/main/java/org/apache/commons/compress/archivers/Archiver.java @@ -162,7 +162,7 @@ public class Archiver { } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) { create(format, new ZipArchiveOutputStream(target), directory, filter); } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) { - create7z(target, directory, filter); + create(new SevenZOutputFile(target), directory, filter); } else { throw new ArchiveException("don't know how to handle format " + format); } @@ -211,32 +211,50 @@ public class Archiver { }); } - private void create7z(SeekableByteChannel target, File directory, FileFilter filter) - throws IOException, ArchiveException { - final SevenZOutputFile out = new SevenZOutputFile(target); + /** + * Creates an archive {@code target} by recursively including all + * files and directories in {@code directory}. + * + * @param target the file to write the new archive to. + * @param the directory that contains the files to archive. + */ + public void create(final SevenZOutputFile target, File directory) throws IOException { + create(target, directory, ACCEPT_ALL); + } + + /** + * Creates an archive {@code target} by recursively including all + * files and directories in {@code directory} that are accepted by + * {@code filter}. + * + * @param target the file to write the new archive to. + * @param the directory that contains the files to archive. + * @param filter selects the files and directories to include inside the archive. + */ + public void create(final SevenZOutputFile target, File directory, FileFilter filter) throws IOException { create(directory, filter, new ArchiveEntryCreator() { public ArchiveEntry create(File f, String entryName) throws IOException { - return out.createArchiveEntry(f, entryName); + return target.createArchiveEntry(f, entryName); } }, new ArchiveEntryConsumer() { public void accept(File source, ArchiveEntry e) throws IOException { - out.putArchiveEntry(e); + target.putArchiveEntry(e); if (!e.isDirectory()) { final byte[] buffer = new byte[8024]; int n = 0; long count = 0; try (InputStream in = new BufferedInputStream(new FileInputStream(source))) { while (-1 != (n = in.read(buffer))) { - out.write(buffer, 0, n); + target.write(buffer, 0, n); count += n; } } } - out.closeArchiveEntry(); + target.closeArchiveEntry(); } }, new Finisher() { public void finish() throws IOException { - out.finish(); + target.finish(); } }); }
