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 2018/05/08 03:53:50 UTC
[1/3] commons-compress git commit: reduce API surface by removing
filter from signatures
Repository: commons-compress
Updated Branches:
refs/heads/master 2b8171bd3 -> 7a10230e1
reduce API surface by removing filter from signatures
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/848be9d9
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/848be9d9
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/848be9d9
Branch: refs/heads/master
Commit: 848be9d922bd9d803e6a1ebf58fedc1d357ef930
Parents: 2b8171b
Author: Stefan Bodewig <bo...@apache.org>
Authored: Mon May 7 21:42:31 2018 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Mon May 7 21:46:13 2018 +0200
----------------------------------------------------------------------
.../commons/compress/archivers/Archiver.java | 119 ++----------
.../commons/compress/archivers/Expander.java | 184 ++-----------------
2 files changed, 31 insertions(+), 272 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/848be9d9/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 798591e..291d301 100644
--- a/src/main/java/org/apache/commons/compress/archivers/Archiver.java
+++ b/src/main/java/org/apache/commons/compress/archivers/Archiver.java
@@ -20,7 +20,6 @@ package org.apache.commons.compress.archivers;
import java.io.BufferedInputStream;
import java.io.File;
-import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -42,13 +41,6 @@ import org.apache.commons.compress.utils.IOUtils;
*/
public class Archiver {
- private static final FileFilter ACCEPT_ALL = new FileFilter() {
- @Override
- public boolean accept(File f) {
- return true;
- }
- };
-
private interface ArchiveEntryCreator {
ArchiveEntry create(File f, String entryName) throws IOException;
}
@@ -74,33 +66,15 @@ public class Archiver {
* @throws ArchiveException if the archive cannot be created for other reasons
*/
public void create(String format, File target, File directory) throws IOException, ArchiveException {
- create(format, target, directory, ACCEPT_ALL);
- }
-
- /**
- * Creates an archive {@code target} using the format {@code
- * format} by recursively including all files and directories in
- * {@code directory} that are accepted by {@code filter}.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the file to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @param filter selects the files and directories to include inside the archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(String format, File target, File directory, FileFilter filter)
- throws IOException, ArchiveException {
if (prefersSeekableByteChannel(format)) {
try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
- create(format, c, directory, filter);
+ create(format, c, directory);
}
return;
}
try (OutputStream o = Files.newOutputStream(target.toPath())) {
- create(format, o, directory, filter);
+ create(format, o, directory);
}
}
@@ -117,25 +91,7 @@ public class Archiver {
* @throws ArchiveException if the archive cannot be created for other reasons
*/
public void create(String format, OutputStream target, File directory) throws IOException, ArchiveException {
- create(format, target, directory, ACCEPT_ALL);
- }
-
- /**
- * Creates an archive {@code target} using the format {@code
- * format} by recursively including all files and directories in
- * {@code directory} that are accepted by {@code filter}.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the stream to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @param filter selects the files and directories to include inside the archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(String format, OutputStream target, File directory, FileFilter filter)
- throws IOException, ArchiveException {
- create(new ArchiveStreamFactory().createArchiveOutputStream(format, target), directory, filter);
+ create(new ArchiveStreamFactory().createArchiveOutputStream(format, target), directory);
}
/**
@@ -152,30 +108,12 @@ public class Archiver {
*/
public void create(String format, SeekableByteChannel target, File directory)
throws IOException, ArchiveException {
- create(format, target, directory, ACCEPT_ALL);
- }
-
- /**
- * Creates an archive {@code target} using the format {@code
- * format} by recursively including all files and directories in
- * {@code directory} that are accepted by {@code filter}.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the channel to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @param filter selects the files and directories to include inside the archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(String format, SeekableByteChannel target, File directory, FileFilter filter)
- throws IOException, ArchiveException {
if (!prefersSeekableByteChannel(format)) {
- create(format, Channels.newOutputStream(target), directory, filter);
+ create(format, Channels.newOutputStream(target), directory);
} else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
- create(new ZipArchiveOutputStream(target), directory, filter);
+ create(new ZipArchiveOutputStream(target), directory);
} else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
- create(new SevenZOutputFile(target), directory, filter);
+ create(new SevenZOutputFile(target), directory);
} else {
throw new ArchiveException("don't know how to handle format " + format);
}
@@ -190,24 +128,9 @@ public class Archiver {
* @throws IOException if an I/O error occurs
* @throws ArchiveException if the archive cannot be created for other reasons
*/
- public void create(ArchiveOutputStream target, File directory) throws IOException, ArchiveException {
- 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 stream to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @param filter selects the files and directories to include inside the archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(final ArchiveOutputStream target, File directory, FileFilter filter)
+ public void create(final ArchiveOutputStream target, File directory)
throws IOException, ArchiveException {
- create(directory, filter, new ArchiveEntryCreator() {
+ create(directory, new ArchiveEntryCreator() {
public ArchiveEntry create(File f, String entryName) throws IOException {
return target.createArchiveEntry(f, entryName);
}
@@ -237,21 +160,7 @@ public class Archiver {
* @throws IOException if an I/O error occurs
*/
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 directory the directory that contains the files to archive.
- * @param filter selects the files and directories to include inside the archive.
- * @throws IOException if an I/O error occurs
- */
- public void create(final SevenZOutputFile target, File directory, FileFilter filter) throws IOException {
- create(directory, filter, new ArchiveEntryCreator() {
+ create(directory, new ArchiveEntryCreator() {
public ArchiveEntry create(File f, String entryName) throws IOException {
return target.createArchiveEntry(f, entryName);
}
@@ -282,15 +191,15 @@ public class Archiver {
return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
}
- private void create(File directory, FileFilter filter, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer,
+ private void create(File directory, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer,
Finisher finisher) throws IOException {
- create("", directory, filter, creator, consumer);
+ create("", directory, creator, consumer);
finisher.finish();
}
- private void create(String prefix, File directory, FileFilter filter, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer)
+ private void create(String prefix, File directory, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer)
throws IOException {
- File[] children = directory.listFiles(filter);
+ File[] children = directory.listFiles();
if (children == null) {
return;
}
@@ -298,7 +207,7 @@ public class Archiver {
String entryName = prefix + f.getName() + (f.isDirectory() ? "/" : "");
consumer.accept(f, creator.create(f, entryName));
if (f.isDirectory()) {
- create(entryName, f, filter, creator, consumer);
+ create(entryName, f, creator, consumer);
}
}
}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/848be9d9/src/main/java/org/apache/commons/compress/archivers/Expander.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/Expander.java b/src/main/java/org/apache/commons/compress/archivers/Expander.java
index f726de7..1575e10 100644
--- a/src/main/java/org/apache/commons/compress/archivers/Expander.java
+++ b/src/main/java/org/apache/commons/compress/archivers/Expander.java
@@ -42,23 +42,6 @@ import org.apache.commons.compress.utils.IOUtils;
* @since 1.17
*/
public class Expander {
- /**
- * Used to filter the entries to be extracted.
- */
- public interface ArchiveEntryFilter {
- /**
- * @return true if the entry shall be expanded
- * @param entry the entry to test
- */
- boolean accept(ArchiveEntry entry);
- }
-
- private static final ArchiveEntryFilter ACCEPT_ALL = new ArchiveEntryFilter() {
- @Override
- public boolean accept(ArchiveEntry e) {
- return true;
- }
- };
private interface ArchiveEntrySupplier {
ArchiveEntry getNextReadableEntry() throws IOException;
@@ -79,66 +62,32 @@ public class Expander {
* @throws ArchiveException if the archive cannot be read for other reasons
*/
public void expand(File archive, File targetDirectory) throws IOException, ArchiveException {
- expand(archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(String format, File archive, File targetDirectory) throws IOException, ArchiveException {
- expand(format, archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * <p>Tries to auto-detect the archive's format.</p>
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(File archive, File targetDirectory, ArchiveEntryFilter filter)
- throws IOException, ArchiveException {
String format = null;
try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
format = new ArchiveStreamFactory().detect(i);
}
- expand(format, archive, targetDirectory, filter);
+ expand(format, archive, targetDirectory);
}
/**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
+ * Expands {@code archive} into {@code targetDirectory}.
*
* @param archive the file to expand
* @param targetDirectory the directory to write to
* @param format the archive format. This uses the same format as
* accepted by {@link ArchiveStreamFactory}.
- * @param filter selects the entries to expand
* @throws IOException if an I/O error occurs
* @throws ArchiveException if the archive cannot be read for other reasons
*/
- public void expand(String format, File archive, File targetDirectory, ArchiveEntryFilter filter)
- throws IOException, ArchiveException {
+ public void expand(String format, File archive, File targetDirectory) throws IOException, ArchiveException {
if (prefersSeekableByteChannel(format)) {
try (SeekableByteChannel c = FileChannel.open(archive.toPath(), StandardOpenOption.READ)) {
- expand(format, c, targetDirectory, filter);
+ expand(format, c, targetDirectory);
}
return;
}
try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
- expand(format, i, targetDirectory, filter);
+ expand(format, i, targetDirectory);
}
}
@@ -153,7 +102,7 @@ public class Expander {
* @throws ArchiveException if the archive cannot be read for other reasons
*/
public void expand(InputStream archive, File targetDirectory) throws IOException, ArchiveException {
- expand(archive, targetDirectory, ACCEPT_ALL);
+ expand(new ArchiveStreamFactory().createArchiveInputStream(archive), targetDirectory);
}
/**
@@ -168,41 +117,7 @@ public class Expander {
*/
public void expand(String format, InputStream archive, File targetDirectory)
throws IOException, ArchiveException {
- expand(format, archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * <p>Tries to auto-detect the archive's format.</p>
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(InputStream archive, File targetDirectory, ArchiveEntryFilter filter)
- throws IOException, ArchiveException {
- expand(new ArchiveStreamFactory().createArchiveInputStream(archive), targetDirectory, filter);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(String format, InputStream archive, File targetDirectory, ArchiveEntryFilter filter)
- throws IOException, ArchiveException {
- expand(new ArchiveStreamFactory().createArchiveInputStream(format, archive), targetDirectory, filter);
+ expand(new ArchiveStreamFactory().createArchiveInputStream(format, archive), targetDirectory);
}
/**
@@ -217,29 +132,12 @@ public class Expander {
*/
public void expand(String format, SeekableByteChannel archive, File targetDirectory)
throws IOException, ArchiveException {
- expand(format, archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(String format, SeekableByteChannel archive, File targetDirectory, ArchiveEntryFilter filter)
- throws IOException, ArchiveException {
if (!prefersSeekableByteChannel(format)) {
- expand(format, Channels.newInputStream(archive), targetDirectory, filter);
+ expand(format, Channels.newInputStream(archive), targetDirectory);
} else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
- expand(new ZipFile(archive), targetDirectory, filter);
+ expand(new ZipFile(archive), targetDirectory);
} else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
- expand(new SevenZFile(archive), targetDirectory, filter);
+ expand(new SevenZFile(archive), targetDirectory);
} else {
throw new ArchiveException("don't know how to handle format " + format);
}
@@ -253,22 +151,7 @@ public class Expander {
* @throws IOException if an I/O error occurs
* @throws ArchiveException if the archive cannot be read for other reasons
*/
- public void expand(ArchiveInputStream archive, File targetDirectory)
- throws IOException, ArchiveException {
- expand(archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(final ArchiveInputStream archive, File targetDirectory, ArchiveEntryFilter filter)
+ public void expand(final ArchiveInputStream archive, File targetDirectory)
throws IOException, ArchiveException {
expand(new ArchiveEntrySupplier() {
@Override
@@ -284,7 +167,7 @@ public class Expander {
public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
IOUtils.copy(archive, out);
}
- }, targetDirectory, filter);
+ }, targetDirectory);
}
/**
@@ -295,22 +178,7 @@ public class Expander {
* @throws IOException if an I/O error occurs
* @throws ArchiveException if the archive cannot be read for other reasons
*/
- public void expand(ZipFile archive, File targetDirectory)
- throws IOException, ArchiveException {
- expand(archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(final ZipFile archive, File targetDirectory, ArchiveEntryFilter filter)
+ public void expand(final ZipFile archive, File targetDirectory)
throws IOException, ArchiveException {
final Enumeration<ZipArchiveEntry> entries = archive.getEntries();
expand(new ArchiveEntrySupplier() {
@@ -329,7 +197,7 @@ public class Expander {
IOUtils.copy(in, out);
}
}
- }, targetDirectory, filter);
+ }, targetDirectory);
}
/**
@@ -340,22 +208,7 @@ public class Expander {
* @throws IOException if an I/O error occurs
* @throws ArchiveException if the archive cannot be read for other reasons
*/
- public void expand(SevenZFile archive, File targetDirectory)
- throws IOException, ArchiveException {
- expand(archive, targetDirectory, ACCEPT_ALL);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}, using
- * only the entries accepted by the {@code filter}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param filter selects the entries to expand
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(final SevenZFile archive, File targetDirectory, ArchiveEntryFilter filter)
+ public void expand(final SevenZFile archive, File targetDirectory)
throws IOException, ArchiveException {
expand(new ArchiveEntrySupplier() {
@Override
@@ -373,21 +226,18 @@ public class Expander {
count += n;
}
}
- }, targetDirectory, filter);
+ }, targetDirectory);
}
private boolean prefersSeekableByteChannel(String format) {
return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
}
- private void expand(ArchiveEntrySupplier supplier, EntryWriter writer, File targetDirectory, ArchiveEntryFilter filter)
+ private void expand(ArchiveEntrySupplier supplier, EntryWriter writer, File targetDirectory)
throws IOException {
String targetDirPath = targetDirectory.getCanonicalPath();
ArchiveEntry nextEntry = supplier.getNextReadableEntry();
while (nextEntry != null) {
- if (!filter.accept(nextEntry)) {
- continue;
- }
File f = new File(targetDirectory, nextEntry.getName());
if (!f.getCanonicalPath().startsWith(targetDirPath)) {
throw new IOException("expanding " + nextEntry.getName()
[3/3] commons-compress git commit: remove more complex examples
package, move remaining example code
Posted by bo...@apache.org.
remove more complex examples package, move remaining example code
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/7a10230e
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/7a10230e
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/7a10230e
Branch: refs/heads/master
Commit: 7a10230e1ecbb0f642eb2352a7f85f00961a6d54
Parents: 848be9d
Author: Stefan Bodewig <bo...@apache.org>
Authored: Tue May 8 05:53:08 2018 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Tue May 8 05:53:08 2018 +0200
----------------------------------------------------------------------
pom.xml | 2 +-
.../commons/compress/archivers/Archiver.java | 214 ---------------
.../commons/compress/archivers/Expander.java | 258 ------------------
.../compress/archivers/examples/Archive.java | 148 -----------
.../compress/archivers/examples/ArchiveCli.java | 44 ----
.../archivers/examples/ArchiveEntrySource.java | 36 ---
.../archivers/examples/ArchiveSinks.java | 100 -------
.../archivers/examples/ArchiveSources.java | 139 ----------
.../compress/archivers/examples/Archiver.java | 216 +++++++++++++++
.../compress/archivers/examples/Chain.java | 54 ----
.../archivers/examples/ChainDefinition.java | 70 -----
.../archivers/examples/ChainPayload.java | 74 ------
.../archivers/examples/ChainRunner.java | 46 ----
.../compress/archivers/examples/ChainStep.java | 42 ---
.../examples/DirectoryBasedSource.java | 104 --------
.../archivers/examples/DirectorySink.java | 73 ------
.../compress/archivers/examples/Expand.java | 123 ---------
.../compress/archivers/examples/ExpandCli.java | 48 ----
.../compress/archivers/examples/Expander.java | 260 +++++++++++++++++++
.../archivers/examples/FileFilterAdapter.java | 37 ---
.../archivers/examples/FileToArchiveSink.java | 68 -----
.../compress/archivers/examples/Filter.java | 44 ----
.../compress/archivers/examples/ListerCli.java | 68 -----
.../examples/SevenZArchiveEntrySource.java | 124 ---------
.../examples/SevenZOutputFileSink.java | 78 ------
.../compress/archivers/examples/Sink.java | 54 ----
.../compress/archivers/examples/Source.java | 28 --
.../examples/StreamBasedArchiveEntrySource.java | 97 -------
.../compress/archivers/examples/Supplier.java | 37 ---
.../archivers/examples/ThrowingIterator.java | 30 ---
.../archivers/examples/Transformer.java | 41 ---
.../examples/ZipArchiveEntrySource.java | 96 -------
.../compress/archivers/examples/package.html | 69 +----
.../compress/utils/NoCloseInputStream.java | 43 ---
34 files changed, 478 insertions(+), 2487 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 85ee514..c3eff8e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -329,7 +329,7 @@ Brotli, Zstandard and ar, cpio, jar, tar, zip, dump, 7z, arj.
<configuration>
<archive>
<manifestEntries>
- <Main-Class>org.apache.commons.compress.archivers.examples.ListerCli</Main-Class>
+ <Main-Class>org.apache.commons.compress.archivers.Lister</Main-Class>
<Extension-Name>org.apache.commons.compress</Extension-Name>
<Automatic-Module-Name>${commons.module.name}</Automatic-Module-Name>
</manifestEntries>
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/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
deleted file mode 100644
index 291d301..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/Archiver.java
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
- * 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;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.SeekableByteChannel;
-import java.nio.file.Files;
-import java.nio.file.StandardOpenOption;
-
-import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
-import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
-import org.apache.commons.compress.utils.IOUtils;
-
-/**
- * Provides a high level API for creating archives.
- * @since 1.17
- */
-public class Archiver {
-
- private interface ArchiveEntryCreator {
- ArchiveEntry create(File f, String entryName) throws IOException;
- }
-
- private interface ArchiveEntryConsumer {
- void accept(File source, ArchiveEntry entry) throws IOException;
- }
-
- private interface Finisher {
- void finish() throws IOException;
- }
-
- /**
- * Creates an archive {@code target} using the format {@code
- * format} by recursively including all files and directories in
- * {@code directory}.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the file to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(String format, File target, File directory) throws IOException, ArchiveException {
- if (prefersSeekableByteChannel(format)) {
- try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
- StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
- create(format, c, directory);
- }
- return;
- }
- try (OutputStream o = Files.newOutputStream(target.toPath())) {
- create(format, o, directory);
- }
- }
-
- /**
- * Creates an archive {@code target} using the format {@code
- * format} by recursively including all files and directories in
- * {@code directory}.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the stream to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(String format, OutputStream target, File directory) throws IOException, ArchiveException {
- create(new ArchiveStreamFactory().createArchiveOutputStream(format, target), directory);
- }
-
- /**
- * Creates an archive {@code target} using the format {@code
- * format} by recursively including all files and directories in
- * {@code directory}.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the channel to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(String format, SeekableByteChannel target, File directory)
- throws IOException, ArchiveException {
- if (!prefersSeekableByteChannel(format)) {
- create(format, Channels.newOutputStream(target), directory);
- } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
- create(new ZipArchiveOutputStream(target), directory);
- } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
- create(new SevenZOutputFile(target), directory);
- } else {
- throw new ArchiveException("don't know how to handle format " + format);
- }
- }
-
- /**
- * Creates an archive {@code target} by recursively including all
- * files and directories in {@code directory}.
- *
- * @param target the stream to write the new archive to.
- * @param directory the directory that contains the files to archive.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public void create(final ArchiveOutputStream target, File directory)
- throws IOException, ArchiveException {
- create(directory, new ArchiveEntryCreator() {
- public ArchiveEntry create(File f, String entryName) throws IOException {
- return target.createArchiveEntry(f, entryName);
- }
- }, new ArchiveEntryConsumer() {
- public void accept(File source, ArchiveEntry e) throws IOException {
- target.putArchiveEntry(e);
- if (!e.isDirectory()) {
- try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
- IOUtils.copy(in, target);
- }
- }
- target.closeArchiveEntry();
- }
- }, new Finisher() {
- public void finish() throws IOException {
- target.finish();
- }
- });
- }
-
- /**
- * 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 directory the directory that contains the files to archive.
- * @throws IOException if an I/O error occurs
- */
- public void create(final SevenZOutputFile target, File directory) throws IOException {
- create(directory, new ArchiveEntryCreator() {
- public ArchiveEntry create(File f, String entryName) throws IOException {
- return target.createArchiveEntry(f, entryName);
- }
- }, new ArchiveEntryConsumer() {
- public void accept(File source, ArchiveEntry e) throws IOException {
- target.putArchiveEntry(e);
- if (!e.isDirectory()) {
- final byte[] buffer = new byte[8024];
- int n = 0;
- long count = 0;
- try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
- while (-1 != (n = in.read(buffer))) {
- target.write(buffer, 0, n);
- count += n;
- }
- }
- }
- target.closeArchiveEntry();
- }
- }, new Finisher() {
- public void finish() throws IOException {
- target.finish();
- }
- });
- }
-
- private boolean prefersSeekableByteChannel(String format) {
- return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
- }
-
- private void create(File directory, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer,
- Finisher finisher) throws IOException {
- create("", directory, creator, consumer);
- finisher.finish();
- }
-
- private void create(String prefix, File directory, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer)
- throws IOException {
- File[] children = directory.listFiles();
- if (children == null) {
- return;
- }
- for (File f : children) {
- String entryName = prefix + f.getName() + (f.isDirectory() ? "/" : "");
- consumer.accept(f, creator.create(f, entryName));
- if (f.isDirectory()) {
- create(entryName, f, creator, consumer);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/Expander.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/Expander.java b/src/main/java/org/apache/commons/compress/archivers/Expander.java
deleted file mode 100644
index 1575e10..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/Expander.java
+++ /dev/null
@@ -1,258 +0,0 @@
-/*
- * 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;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.SeekableByteChannel;
-import java.nio.file.Files;
-import java.nio.file.StandardOpenOption;
-import java.util.Enumeration;
-
-import org.apache.commons.compress.archivers.sevenz.SevenZFile;
-import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
-import org.apache.commons.compress.archivers.zip.ZipFile;
-import org.apache.commons.compress.utils.IOUtils;
-
-/**
- * Provides a high level API for expanding archives.
- * @since 1.17
- */
-public class Expander {
-
- private interface ArchiveEntrySupplier {
- ArchiveEntry getNextReadableEntry() throws IOException;
- }
-
- private interface EntryWriter {
- void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException;
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * <p>Tries to auto-detect the archive's format.</p>
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(File archive, File targetDirectory) throws IOException, ArchiveException {
- String format = null;
- try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
- format = new ArchiveStreamFactory().detect(i);
- }
- expand(format, archive, targetDirectory);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(String format, File archive, File targetDirectory) throws IOException, ArchiveException {
- if (prefersSeekableByteChannel(format)) {
- try (SeekableByteChannel c = FileChannel.open(archive.toPath(), StandardOpenOption.READ)) {
- expand(format, c, targetDirectory);
- }
- return;
- }
- try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
- expand(format, i, targetDirectory);
- }
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * <p>Tries to auto-detect the archive's format.</p>
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(InputStream archive, File targetDirectory) throws IOException, ArchiveException {
- expand(new ArchiveStreamFactory().createArchiveInputStream(archive), targetDirectory);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(String format, InputStream archive, File targetDirectory)
- throws IOException, ArchiveException {
- expand(new ArchiveStreamFactory().createArchiveInputStream(format, archive), targetDirectory);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(String format, SeekableByteChannel archive, File targetDirectory)
- throws IOException, ArchiveException {
- if (!prefersSeekableByteChannel(format)) {
- expand(format, Channels.newInputStream(archive), targetDirectory);
- } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
- expand(new ZipFile(archive), targetDirectory);
- } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
- expand(new SevenZFile(archive), targetDirectory);
- } else {
- throw new ArchiveException("don't know how to handle format " + format);
- }
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(final ArchiveInputStream archive, File targetDirectory)
- throws IOException, ArchiveException {
- expand(new ArchiveEntrySupplier() {
- @Override
- public ArchiveEntry getNextReadableEntry() throws IOException {
- ArchiveEntry next = archive.getNextEntry();
- while (next != null && !archive.canReadEntryData(next)) {
- next = archive.getNextEntry();
- }
- return next;
- }
- }, new EntryWriter() {
- @Override
- public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
- IOUtils.copy(archive, out);
- }
- }, targetDirectory);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(final ZipFile archive, File targetDirectory)
- throws IOException, ArchiveException {
- final Enumeration<ZipArchiveEntry> entries = archive.getEntries();
- expand(new ArchiveEntrySupplier() {
- @Override
- public ArchiveEntry getNextReadableEntry() throws IOException {
- ZipArchiveEntry next = entries.hasMoreElements() ? entries.nextElement() : null;
- while (next != null && !archive.canReadEntryData(next)) {
- next = entries.hasMoreElements() ? entries.nextElement() : null;
- }
- return next;
- }
- }, new EntryWriter() {
- @Override
- public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
- try (InputStream in = archive.getInputStream((ZipArchiveEntry) entry)) {
- IOUtils.copy(in, out);
- }
- }
- }, targetDirectory);
- }
-
- /**
- * Expands {@code archive} into {@code targetDirectory}.
- *
- * @param archive the file to expand
- * @param targetDirectory the directory to write to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public void expand(final SevenZFile archive, File targetDirectory)
- throws IOException, ArchiveException {
- expand(new ArchiveEntrySupplier() {
- @Override
- public ArchiveEntry getNextReadableEntry() throws IOException {
- return archive.getNextEntry();
- }
- }, new EntryWriter() {
- @Override
- public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
- final byte[] buffer = new byte[8024];
- int n = 0;
- long count = 0;
- while (-1 != (n = archive.read(buffer))) {
- out.write(buffer, 0, n);
- count += n;
- }
- }
- }, targetDirectory);
- }
-
- private boolean prefersSeekableByteChannel(String format) {
- return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
- }
-
- private void expand(ArchiveEntrySupplier supplier, EntryWriter writer, File targetDirectory)
- throws IOException {
- String targetDirPath = targetDirectory.getCanonicalPath();
- ArchiveEntry nextEntry = supplier.getNextReadableEntry();
- while (nextEntry != null) {
- File f = new File(targetDirectory, nextEntry.getName());
- if (!f.getCanonicalPath().startsWith(targetDirPath)) {
- throw new IOException("expanding " + nextEntry.getName()
- + " would craete file outside of " + targetDirectory);
- }
- if (nextEntry.isDirectory()) {
- f.mkdirs();
- } else {
- f.getParentFile().mkdirs();
- try (OutputStream o = Files.newOutputStream(f.toPath())) {
- writer.writeEntryDataTo(nextEntry, o);
- }
- }
- nextEntry = supplier.getNextReadableEntry();
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java b/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java
deleted file mode 100644
index 1895a7d..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Consumes files and passes them to a sink, usually used to create an archive of them.
- * @since 1.17
- */
-public class Archive {
- /**
- * Sets up a chain of operations and consumes the files from a source of files.
- * @since 1.17
- */
- public interface ChainBuilder {
- /**
- * Adds a filter to the chain.
- * @param filter the filter to apply
- * @return an updated builder
- */
- ChainBuilder filter(Filter<File> filter);
- /**
- * Adds a filter to the chain.
- * @param filter the filter to apply
- * @return an updated builder
- */
- ChainBuilder filter(FileFilter filter);
- /**
- * Adds a filter to the chain that filters out entries that cannot be read.
- * @return an updated builder
- */
- ChainBuilder skipUnreadable();
- /**
- * Adds a filter to the chain that filters out everything that is not a file.
- * @return an updated builder
- */
- ChainBuilder skipNonFiles();
- /**
- * Adds a transformer to the chain.
- * @param transformer transformer to apply
- * @return an updated builder
- */
- ChainBuilder map(Transformer<File> transformer);
- /**
- * Adds a generic step to the chain.
- * @param step step to perform
- * @return an updated builder
- */
- ChainBuilder withStep(ChainStep<File> step);
- /**
- * Actually consumes all the files supplied.
- * @param sink sink that the entries will be sent to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be written for other reasons
- */
- void to(Sink<File> sink) throws IOException, ArchiveException;
- }
-
- /**
- * Sets the source of files to be a directory.
- * @param f the source directory
- * @return a builder for the chain to be created and run
- */
- public static ChainBuilder directory(File f) {
- return source(new DirectoryBasedSource(f));
- }
-
- /**
- * Sets the source of files to process.
- * @param source the source directory
- * @return a builder for the chain to be created and run
- */
- public static ChainBuilder source(Source<File> source) {
- return new Builder(source);
- }
-
- private static class Builder implements ChainBuilder {
- private final Source<File> source;
- private ChainDefinition<File> chainDef = new ChainDefinition<>();
-
- Builder(Source<File> source) {
- this.source = source;
- }
-
- @Override
- public ChainBuilder filter(Filter<File> filter) {
- return withStep(filter);
- }
- @Override
- public ChainBuilder filter(FileFilter filter) {
- return filter(new FileFilterAdapter(filter));
- }
- @Override
- public ChainBuilder skipUnreadable() {
- return filter(new FileFilter() {
- @Override
- public boolean accept(File f) {
- return f.canRead();
- }
- });
- }
- @Override
- public ChainBuilder skipNonFiles() {
- return filter(new FileFilter() {
- @Override
- public boolean accept(File f) {
- return f.isFile();
- }
- });
- }
- @Override
- public ChainBuilder map(Transformer<File> transformer) {
- return withStep(transformer);
- }
- @Override
- public ChainBuilder withStep(ChainStep<File> step) {
- chainDef.add(step);
- return this;
- }
- @Override
- public void to(Sink<File> sink) throws IOException, ArchiveException {
- chainDef.add(sink);
- chainDef.freeze();
- new ChainRunner<File>(source, chainDef, sink).run();
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
deleted file mode 100644
index cb20460..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveCli.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Simple command line tool that creates an archive from the contents of a directory.
- *
- * <p>Usage: <code>ArchiveCli dir format archive</code></p>
- * @since 1.17
- */
-public class ArchiveCli {
-
- public static void main(String[] args) throws IOException, ArchiveException {
- if (args.length != 3) {
- System.err.println("Usage: ArchiveCli dir format target");
- System.exit(1);
- }
- try (Sink<File> sink = ArchiveSinks.forFile(args[1], new File(args[2]))) {
- Archive.directory(new File(args[0]))
- .to(sink);
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveEntrySource.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveEntrySource.java b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveEntrySource.java
deleted file mode 100644
index 402134f..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveEntrySource.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.examples;
-
-import org.apache.commons.compress.archivers.ArchiveEntry;
-
-/**
- * Combines Source and a factory for filter that skips unreadable entries.
- * @since 1.17
- */
-public interface ArchiveEntrySource extends Source<ArchiveEntry> {
-
- /**
- * Provides a filter that can be used to skip entries the
- * underlying source is unable to read the content of.
- * @return filter that can be used to skip entries the underlying
- * source is unable to read the content of
- */
- Filter<ArchiveEntry> skipUnreadable();
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java
deleted file mode 100644
index f00fc4f..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSinks.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.FileOutputStream;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.SeekableByteChannel;
-import java.nio.file.StandardOpenOption;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.archivers.ArchiveStreamFactory;
-import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
-
-/**
- * Supplies factory methods for file sinks that write to archives,
- * @since 1.17
- */
-public class ArchiveSinks {
- /**
- * Uses {@link ArchiveStreamFactory#createArchiveOutputStream}.
- *
- * <p>Will not support 7z.</p>
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param os the stream to write to.
- * @return a sink that consumes the files
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public static Sink<File> forStream(String format, OutputStream os) throws IOException, ArchiveException {
- return new FileToArchiveSink(new ArchiveStreamFactory().createArchiveOutputStream(format, os));
- }
-
- /**
- * Uses {@link ArchiveStreamFactory#createArchiveOutputStream} unless
- * special handling for ZIP or 7z is required.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param target the file to write to.
- * @return a sink that consumes the files
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public static Sink<File> forFile(String format, File target) throws IOException, ArchiveException {
- if (prefersSeekableByteChannel(format)) {
- return forChannel(format, FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
- StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
- }
- return new FileToArchiveSink(new ArchiveStreamFactory()
- .createArchiveOutputStream(format, new FileOutputStream(target)));
- }
-
- /**
- * Uses {@link ArchiveStreamFactory#createArchiveOutputStream} unless
- * special handling for ZIP or 7z is required.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param c the channel to write to.
- * @return a sink that consumes the files
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be created for other reasons
- */
- public static Sink<File> forChannel(String format, SeekableByteChannel c) throws IOException, ArchiveException {
- if (!prefersSeekableByteChannel(format)) {
- return forStream(format, Channels.newOutputStream(c));
- } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
- return new FileToArchiveSink(new ZipArchiveOutputStream(c));
- } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
- return new SevenZOutputFileSink(c);
- } else {
- throw new ArchiveException("don't know how to handle format " + format);
- }
- }
-
- private static boolean prefersSeekableByteChannel(String format) {
- return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSources.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSources.java b/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSources.java
deleted file mode 100644
index 670eb2f..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ArchiveSources.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.FileInputStream;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.SeekableByteChannel;
-import java.nio.file.StandardOpenOption;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.archivers.ArchiveInputStream;
-import org.apache.commons.compress.archivers.ArchiveStreamFactory;
-import org.apache.commons.compress.archivers.sevenz.SevenZFile;
-import org.apache.commons.compress.archivers.zip.ZipFile;
-
-/**
- * Supplies factory methods for ArchiveEntry sources that read from archives,
- * @since 1.17
- */
-public class ArchiveSources {
- /**
- * Builder for {@link ArchiveEntrySource} that needs to know its format.
- * @since 1.17
- */
- public interface PendingFormat {
- /**
- * Signals the format shall be detcted automatically.
- * @return the configured source
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- ArchiveEntrySource detectFormat() throws IOException, ArchiveException;
- /**
- * Explicitly provides the expected format of the archive.
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @return the configured source
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- ArchiveEntrySource withFormat(String format) throws IOException, ArchiveException;
- }
-
- /**
- * Uses {@link ArchiveStreamFactory#createArchiveInputStream} unless special handling for ZIP or /z is required.
- *
- * @param f the file to read from
- * @return a builder that needs to know the format
- */
- public static PendingFormat forFile(final File f) {
- return new PendingFormat() {
- @Override
- public ArchiveEntrySource detectFormat() throws IOException, ArchiveException {
- String format = null;
- try (InputStream i = new BufferedInputStream(new FileInputStream(f))) {
- format = new ArchiveStreamFactory().detect(i);
- }
- return withFormat(format);
- }
- @Override
- public ArchiveEntrySource withFormat(String format) throws IOException, ArchiveException {
- if (prefersSeekableByteChannel(format)) {
- return forChannel(format, FileChannel.open(f.toPath(), StandardOpenOption.READ));
- }
- return new StreamBasedArchiveEntrySource(new ArchiveStreamFactory()
- .createArchiveInputStream(format, new BufferedInputStream(new FileInputStream(f))));
- }
- };
- }
-
- /**
- * Uses {@link ArchiveStreamFactory#createArchiveInputStream} unless special handling for ZIP or /z is required.
- *
- * @param format the archive format. This uses the same format as
- * accepted by {@link ArchiveStreamFactory}.
- * @param c the channel to read from
- * @return the configured source
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the archive cannot be read for other reasons
- */
- public static ArchiveEntrySource forChannel(String format, SeekableByteChannel c)
- throws IOException, ArchiveException {
- if (!prefersSeekableByteChannel(format)) {
- return new StreamBasedArchiveEntrySource(new ArchiveStreamFactory()
- .createArchiveInputStream(format, Channels.newInputStream(c)));
- } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
- return new ZipArchiveEntrySource(c);
- } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
- return new SevenZArchiveEntrySource(c);
- }
- throw new ArchiveException("don't know how to handle format " + format);
- }
-
- /**
- * Uses {@link ArchiveStreamFactory#createArchiveInputStream}.
- *
- * <p>Will not support 7z.</p>
- *
- * @param in the stream to read from
- * @return a builder that needs to know the format
- */
- public static PendingFormat forStream(final InputStream in) {
- return new PendingFormat() {
- @Override
- public ArchiveEntrySource detectFormat() throws IOException, ArchiveException {
- return new StreamBasedArchiveEntrySource(new ArchiveStreamFactory().createArchiveInputStream(in));
- }
- @Override
- public ArchiveEntrySource withFormat(String format) throws IOException, ArchiveException {
- return new StreamBasedArchiveEntrySource(new ArchiveStreamFactory()
- .createArchiveInputStream(format, in));
- }
- };
- }
-
- private static boolean prefersSeekableByteChannel(String format) {
- return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java b/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
new file mode 100644
index 0000000..4c43bb9
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
@@ -0,0 +1,216 @@
+/*
+ * 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.examples;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+import org.apache.commons.compress.utils.IOUtils;
+
+/**
+ * Provides a high level API for creating archives.
+ * @since 1.17
+ */
+public class Archiver {
+
+ private interface ArchiveEntryCreator {
+ ArchiveEntry create(File f, String entryName) throws IOException;
+ }
+
+ private interface ArchiveEntryConsumer {
+ void accept(File source, ArchiveEntry entry) throws IOException;
+ }
+
+ private interface Finisher {
+ void finish() throws IOException;
+ }
+
+ /**
+ * Creates an archive {@code target} using the format {@code
+ * format} by recursively including all files and directories in
+ * {@code directory}.
+ *
+ * @param format the archive format. This uses the same format as
+ * accepted by {@link ArchiveStreamFactory}.
+ * @param target the file to write the new archive to.
+ * @param directory the directory that contains the files to archive.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be created for other reasons
+ */
+ public void create(String format, File target, File directory) throws IOException, ArchiveException {
+ if (prefersSeekableByteChannel(format)) {
+ try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
+ StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
+ create(format, c, directory);
+ }
+ return;
+ }
+ try (OutputStream o = Files.newOutputStream(target.toPath())) {
+ create(format, o, directory);
+ }
+ }
+
+ /**
+ * Creates an archive {@code target} using the format {@code
+ * format} by recursively including all files and directories in
+ * {@code directory}.
+ *
+ * @param format the archive format. This uses the same format as
+ * accepted by {@link ArchiveStreamFactory}.
+ * @param target the stream to write the new archive to.
+ * @param directory the directory that contains the files to archive.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be created for other reasons
+ */
+ public void create(String format, OutputStream target, File directory) throws IOException, ArchiveException {
+ create(new ArchiveStreamFactory().createArchiveOutputStream(format, target), directory);
+ }
+
+ /**
+ * Creates an archive {@code target} using the format {@code
+ * format} by recursively including all files and directories in
+ * {@code directory}.
+ *
+ * @param format the archive format. This uses the same format as
+ * accepted by {@link ArchiveStreamFactory}.
+ * @param target the channel to write the new archive to.
+ * @param directory the directory that contains the files to archive.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be created for other reasons
+ */
+ public void create(String format, SeekableByteChannel target, File directory)
+ throws IOException, ArchiveException {
+ if (!prefersSeekableByteChannel(format)) {
+ create(format, Channels.newOutputStream(target), directory);
+ } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
+ create(new ZipArchiveOutputStream(target), directory);
+ } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
+ create(new SevenZOutputFile(target), directory);
+ } else {
+ throw new ArchiveException("don't know how to handle format " + format);
+ }
+ }
+
+ /**
+ * Creates an archive {@code target} by recursively including all
+ * files and directories in {@code directory}.
+ *
+ * @param target the stream to write the new archive to.
+ * @param directory the directory that contains the files to archive.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be created for other reasons
+ */
+ public void create(final ArchiveOutputStream target, File directory)
+ throws IOException, ArchiveException {
+ create(directory, new ArchiveEntryCreator() {
+ public ArchiveEntry create(File f, String entryName) throws IOException {
+ return target.createArchiveEntry(f, entryName);
+ }
+ }, new ArchiveEntryConsumer() {
+ public void accept(File source, ArchiveEntry e) throws IOException {
+ target.putArchiveEntry(e);
+ if (!e.isDirectory()) {
+ try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
+ IOUtils.copy(in, target);
+ }
+ }
+ target.closeArchiveEntry();
+ }
+ }, new Finisher() {
+ public void finish() throws IOException {
+ target.finish();
+ }
+ });
+ }
+
+ /**
+ * 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 directory the directory that contains the files to archive.
+ * @throws IOException if an I/O error occurs
+ */
+ public void create(final SevenZOutputFile target, File directory) throws IOException {
+ create(directory, new ArchiveEntryCreator() {
+ public ArchiveEntry create(File f, String entryName) throws IOException {
+ return target.createArchiveEntry(f, entryName);
+ }
+ }, new ArchiveEntryConsumer() {
+ public void accept(File source, ArchiveEntry e) throws IOException {
+ target.putArchiveEntry(e);
+ if (!e.isDirectory()) {
+ final byte[] buffer = new byte[8024];
+ int n = 0;
+ long count = 0;
+ try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
+ while (-1 != (n = in.read(buffer))) {
+ target.write(buffer, 0, n);
+ count += n;
+ }
+ }
+ }
+ target.closeArchiveEntry();
+ }
+ }, new Finisher() {
+ public void finish() throws IOException {
+ target.finish();
+ }
+ });
+ }
+
+ private boolean prefersSeekableByteChannel(String format) {
+ return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
+ }
+
+ private void create(File directory, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer,
+ Finisher finisher) throws IOException {
+ create("", directory, creator, consumer);
+ finisher.finish();
+ }
+
+ private void create(String prefix, File directory, ArchiveEntryCreator creator, ArchiveEntryConsumer consumer)
+ throws IOException {
+ File[] children = directory.listFiles();
+ if (children == null) {
+ return;
+ }
+ for (File f : children) {
+ String entryName = prefix + f.getName() + (f.isDirectory() ? "/" : "");
+ consumer.accept(f, creator.create(f, entryName));
+ if (f.isDirectory()) {
+ create(entryName, f, creator, consumer);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Chain.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Chain.java b/src/main/java/org/apache/commons/compress/archivers/examples/Chain.java
deleted file mode 100644
index 86d2e1c..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Chain.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import java.util.Iterator;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Encapsulates the execution flow of a chain of operations.
- * @since 1.17
- */
-public class Chain<T> {
-
- private final Iterator<ChainStep<T>> chain;
-
- /**
- * Instantiates a new chain.
- *
- * @param chain the steps to take in order.
- */
- public Chain(Iterator<ChainStep<T>> chain) {
- this.chain = chain;
- }
-
- /**
- * Invokes the next step of the chain.
- *
- * @param payload the payload to pass to the next step
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if an archive format related error occurs
- */
- public void next(ChainPayload<T> payload) throws IOException, ArchiveException {
- if (chain.hasNext()) {
- chain.next().process(payload, this);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ChainDefinition.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ChainDefinition.java b/src/main/java/org/apache/commons/compress/archivers/examples/ChainDefinition.java
deleted file mode 100644
index d8a387f..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ChainDefinition.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.examples;
-
-import java.util.Deque;
-import java.util.LinkedList;
-
-/**
- * The recipe for building a {@link Chain}.
- * @since 1.17
- */
-class ChainDefinition<T> {
- private final Deque<ChainStep<T>> steps = new LinkedList<>();
- private volatile boolean frozen = false;
-
- /**
- * Adds a step.
- * @throws IllegalStateException if the definition is already frozen.
- */
- void add(ChainStep<T> step) {
- if (frozen) {
- throw new IllegalStateException("the definition is already frozen");
- }
- steps.addLast(step);
- }
-
- /**
- * Freezes the definition.
- *
- * <p>Once this method has been invoked {@link #add} can no longer be invoked.</p>
- *
- * @throws IllegalStateException if the last step of the definition is not a sink.
- */
- void freeze() {
- if (!frozen) {
- frozen = true;
- if (!(steps.getLast() instanceof Sink)) {
- throw new IllegalStateException("this definition doesn't end in a sink");
- }
- }
- }
-
- /**
- * Returns a chain for this definition.
- *
- * @throws IllegalStateException if the definition is not frozen.
- */
- Chain<T> chain() {
- if (!frozen) {
- throw new IllegalStateException("the definition hasn't been frozen, yet");
- }
- return new Chain(steps.iterator());
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ChainPayload.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ChainPayload.java b/src/main/java/org/apache/commons/compress/archivers/examples/ChainPayload.java
deleted file mode 100644
index 11d8618..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ChainPayload.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.InputStream;
-
-/**
- * The data that is pushed through a chain.
- * @since 1.17
- */
-public class ChainPayload<T> {
- private final T entry;
- private final String entryName;
- private final Supplier<InputStream> input;
- /**
- * Constructs the payload.
- * @param entry entry the actual payload
- * @param entryName the local name of the entry. This may - for
- * example - be the file name relative to a directory.
- * @param input supplies an input stream to the entry's
- * content. Is not expected to be called more than once.
- */
- public ChainPayload(T entry, String entryName, Supplier<InputStream> input) {
- this.entry = entry;
- this.entryName = entryName;
- this.input = input;
- }
- /**
- * Provides the real payload.
- * @return the real playoad
- *
- */
- public T getEntry() {
- return entry;
- }
- /**
- * Provides the local name of the entry.
- *
- * <p>This may - for example - be the file name relative to a
- * directory.</p>
- *
- * @return local name of the entry
- */
- public String getEntryName() {
- return entryName;
- }
- /**
- * Returns a {@link Supplier} that can be used to read the entry's content.
- *
- * <p>The supplier is not required to be callable more than
- * once.</p>
- *
- * @return supplier of input
- */
- public Supplier<InputStream> getInput() {
- return input;
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ChainRunner.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ChainRunner.java b/src/main/java/org/apache/commons/compress/archivers/examples/ChainRunner.java
deleted file mode 100644
index 5f43589..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ChainRunner.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Contains the execution logic of a full chain including a source.
- * @since 1.17
- */
-class ChainRunner<T> {
- private final Source<T> source;
- private final ChainDefinition<T> chainDef;
- private final Sink<T> sink;
-
- ChainRunner(Source<T> source, ChainDefinition<T> chainDef, Sink<T> sink) {
- this.source = source;
- this.chainDef = chainDef;
- this.sink = sink;
- }
-
- void run() throws IOException, ArchiveException {
- ThrowingIterator<ChainPayload<T>> iter = source.get();
- while (iter.hasNext()) {
- chainDef.chain().next(iter.next());
- }
- sink.finish();
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ChainStep.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ChainStep.java b/src/main/java/org/apache/commons/compress/archivers/examples/ChainStep.java
deleted file mode 100644
index ee0e4b3..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ChainStep.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * A step inside of a {@link Chain}.
- * @since 1.17
- */
-public interface ChainStep<T> {
- /**
- * Process the chain's payload.
- *
- * <p>Any non-terminal step that invokes the {@link Supplier} of
- * the payload is responsible for providing a fresh supplier if
- * the chain is to be continued.</p>
- *
- * @param payload the payload.
- * @param chain chain to return control to once processing is done.
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if an archive format related error occurs
- */
- void process(ChainPayload<T> payload, Chain<T> chain) throws IOException, ArchiveException;
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java b/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java
deleted file mode 100644
index c6ca484..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-/**
- * Recursively returns all files and directories contained inside of a base directory.
- * @since 1.17
- */
-public class DirectoryBasedSource implements Source<File> {
-
- private final File dir;
-
- /**
- * Sets up a directory as source.
- *
- * @param dir the directory to provide entries from.
- * @throws IllegalArgumentException if dir doesn't exist or is not a directory
- */
- public DirectoryBasedSource(File dir) {
- if (!dir.isDirectory()) {
- throw new IllegalArgumentException("dir is not a readable directory");
- }
- this.dir = dir;
- }
-
- @Override
- public ThrowingIterator<ChainPayload<File>> get() throws IOException {
- return new DirectoryIterator("", dir);
- }
-
- @Override
- public void close() {
- }
-
- private static class DirectoryIterator implements ThrowingIterator<ChainPayload<File>> {
- private final Iterator<File> files;
- private final String namePrefix;
- private DirectoryIterator nestedIterator;
- DirectoryIterator(String namePrefix, File dir) throws IOException {
- this.namePrefix = namePrefix;
- File[] fs = dir.listFiles();
- files = fs == null ? Collections.<File>emptyIterator() : Arrays.asList(fs).iterator();
- }
-
- @Override
- public boolean hasNext() throws IOException {
- if (nestedIterator != null && nestedIterator.hasNext()) {
- return true;
- }
- if (nestedIterator != null) {
- nestedIterator = null;
- }
- return files.hasNext();
- }
-
- @Override
- public ChainPayload<File> next() throws IOException {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- if (nestedIterator != null) {
- return nestedIterator.next();
- }
- final File f = files.next();
- String entryName = namePrefix + f.getName();
- if (f.isDirectory()) {
- entryName += "/";
- nestedIterator = new DirectoryIterator(entryName, f);
- }
- return new ChainPayload(f, entryName, new Supplier<InputStream>() {
- @Override
- public InputStream get() throws IOException {
- return new FileInputStream(f);
- }
- });
- }
-
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/DirectorySink.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/DirectorySink.java b/src/main/java/org/apache/commons/compress/archivers/examples/DirectorySink.java
deleted file mode 100644
index 7a34858..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/DirectorySink.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.utils.IOUtils;
-
-/**
- * A sink that expands archive entries into a directory.
- * @since 1.17
- */
-public class DirectorySink extends Sink<ArchiveEntry> {
- private final File dir;
- private final String dirPath;
-
- /**
- * Sets up a directory as sink.
- *
- * @param dir the directory to provide entries from.
- * @throws IOException if the canonical path of the directory cannot be determined
- * @throws IllegalArgumentException if dir doesn't exist or is not a directory
- */
- public DirectorySink(File dir) throws IOException {
- if (!dir.isDirectory()) {
- throw new IllegalArgumentException("dir is not a readable directory");
- }
- this.dir = dir;
- dirPath = dir.getCanonicalPath();
- }
-
- @Override
- public void consume(ChainPayload<ArchiveEntry> payload) throws IOException, ArchiveException {
- File f = new File(dir, payload.getEntryName());
- if (!f.getCanonicalPath().startsWith(dirPath)) {
- throw new IOException("expanding " + payload.getEntryName() + " would create file outside of " + dir);
- }
- if (payload.getEntry().isDirectory()) {
- f.mkdirs();
- } else {
- f.getParentFile().mkdirs();
- try (OutputStream o = new FileOutputStream(f);
- InputStream i = payload.getInput().get()) {
- IOUtils.copy(i, o);
- }
- }
- }
-
- @Override
- public void close() {
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Expand.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Expand.java b/src/main/java/org/apache/commons/compress/archivers/examples/Expand.java
deleted file mode 100644
index d6ece27..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Expand.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Consumes archive entries and passes them to a sink, usually used to
- * expand an archive.
- * @since 1.17
- */
-public class Expand {
- /**
- * Sets up a chain of operations and consumes the entries from a source of archive entries.
- * @since 1.17
- */
- public interface ChainBuilder {
- /**
- * Adds a filter to the chain.
- * @param filter the filter to apply
- * @return an updated builder
- */
- ChainBuilder filter(Filter<ArchiveEntry> filter);
- /**
- * Adds a filter to the chain that filters out entries that cannot be read.
- * @return an updated builder
- */
- ChainBuilder skipUnreadable();
- /**
- * Adds a filter to the chain that suppresses all directory entries.
- * @return an updated builder
- */
- ChainBuilder skipDirectories();
- /**
- * Adds a transformer to the chain.
- * @param transformer transformer to apply
- * @return an updated builder
- */
- ChainBuilder map(Transformer<ArchiveEntry> transformer);
- /**
- * Adds a generic step to the chain.
- * @return an updated builder
- * @param step step to perform
- */
- ChainBuilder withStep(ChainStep<ArchiveEntry> step);
- /**
- * Actually consumes all the entries supplied.
- * @param sink sink that the entries will be sent to
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if the source archive cannot be read for other reasons
- */
- void to(Sink<ArchiveEntry> sink) throws IOException, ArchiveException;
- }
-
- /**
- * Sets the source of entries to process.
- * @param source the source
- * @return a builder for the chain to be created and run
- */
- public static ChainBuilder source(ArchiveEntrySource source) {
- return new Builder(source);
- }
-
- private static class Builder implements ChainBuilder {
- private final ArchiveEntrySource source;
- private ChainDefinition<ArchiveEntry> chainDef = new ChainDefinition<>();
-
- Builder(ArchiveEntrySource source) {
- this.source = source;
- }
-
- @Override
- public ChainBuilder filter(Filter<ArchiveEntry> filter) {
- return withStep(filter);
- }
- @Override
- public ChainBuilder skipUnreadable() {
- return filter(source.skipUnreadable());
- }
- @Override
- public ChainBuilder skipDirectories() {
- return filter(new Filter<ArchiveEntry>() {
- @Override
- public boolean accept(String entryName, ArchiveEntry e) {
- return !e.isDirectory();
- }
- });
- }
- @Override
- public ChainBuilder map(Transformer<ArchiveEntry> transformer) {
- return withStep(transformer);
- }
- @Override
- public ChainBuilder withStep(ChainStep<ArchiveEntry> step) {
- chainDef.add(step);
- return this;
- }
- @Override
- public void to(Sink<ArchiveEntry> sink) throws IOException, ArchiveException {
- chainDef.add(sink);
- chainDef.freeze();
- new ChainRunner<ArchiveEntry>(source, chainDef, sink).run();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ExpandCli.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ExpandCli.java b/src/main/java/org/apache/commons/compress/archivers/examples/ExpandCli.java
deleted file mode 100644
index fd264ce..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ExpandCli.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Simple command line tool that extracts an archive into a directory.
- *
- * <p>Usage: <code>ExpandCli archive dir [format]</code></p>
- * @since 1.17
- */
-public class ExpandCli {
-
- public static void main(String[] args) throws IOException, ArchiveException {
- if (args.length < 2 || args.length > 3) {
- System.err.println("Usage: ExpandCli dir archive [format]");
- System.exit(1);
- } else if (args.length == 2) {
- try (ArchiveEntrySource source = ArchiveSources.forFile(new File(args[0])).detectFormat()) {
- Expand.source(source).to(new DirectorySink(new File(args[1])));
- }
- } else {
- try (ArchiveEntrySource source = ArchiveSources.forFile(new File(args[0])).withFormat(args[2])) {
- Expand.source(source).to(new DirectorySink(new File(args[1])));
- }
- }
- }
-
-}
[2/3] commons-compress git commit: remove more complex examples
package, move remaining example code
Posted by bo...@apache.org.
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java b/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java
new file mode 100644
index 0000000..c4d036c
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java
@@ -0,0 +1,260 @@
+/*
+ * 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.examples;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+import java.util.Enumeration;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.archivers.sevenz.SevenZFile;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipFile;
+import org.apache.commons.compress.utils.IOUtils;
+
+/**
+ * Provides a high level API for expanding archives.
+ * @since 1.17
+ */
+public class Expander {
+
+ private interface ArchiveEntrySupplier {
+ ArchiveEntry getNextReadableEntry() throws IOException;
+ }
+
+ private interface EntryWriter {
+ void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException;
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * <p>Tries to auto-detect the archive's format.</p>
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(File archive, File targetDirectory) throws IOException, ArchiveException {
+ String format = null;
+ try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
+ format = new ArchiveStreamFactory().detect(i);
+ }
+ expand(format, archive, targetDirectory);
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @param format the archive format. This uses the same format as
+ * accepted by {@link ArchiveStreamFactory}.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(String format, File archive, File targetDirectory) throws IOException, ArchiveException {
+ if (prefersSeekableByteChannel(format)) {
+ try (SeekableByteChannel c = FileChannel.open(archive.toPath(), StandardOpenOption.READ)) {
+ expand(format, c, targetDirectory);
+ }
+ return;
+ }
+ try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
+ expand(format, i, targetDirectory);
+ }
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * <p>Tries to auto-detect the archive's format.</p>
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(InputStream archive, File targetDirectory) throws IOException, ArchiveException {
+ expand(new ArchiveStreamFactory().createArchiveInputStream(archive), targetDirectory);
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @param format the archive format. This uses the same format as
+ * accepted by {@link ArchiveStreamFactory}.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(String format, InputStream archive, File targetDirectory)
+ throws IOException, ArchiveException {
+ expand(new ArchiveStreamFactory().createArchiveInputStream(format, archive), targetDirectory);
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @param format the archive format. This uses the same format as
+ * accepted by {@link ArchiveStreamFactory}.
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(String format, SeekableByteChannel archive, File targetDirectory)
+ throws IOException, ArchiveException {
+ if (!prefersSeekableByteChannel(format)) {
+ expand(format, Channels.newInputStream(archive), targetDirectory);
+ } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
+ expand(new ZipFile(archive), targetDirectory);
+ } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
+ expand(new SevenZFile(archive), targetDirectory);
+ } else {
+ throw new ArchiveException("don't know how to handle format " + format);
+ }
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(final ArchiveInputStream archive, File targetDirectory)
+ throws IOException, ArchiveException {
+ expand(new ArchiveEntrySupplier() {
+ @Override
+ public ArchiveEntry getNextReadableEntry() throws IOException {
+ ArchiveEntry next = archive.getNextEntry();
+ while (next != null && !archive.canReadEntryData(next)) {
+ next = archive.getNextEntry();
+ }
+ return next;
+ }
+ }, new EntryWriter() {
+ @Override
+ public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
+ IOUtils.copy(archive, out);
+ }
+ }, targetDirectory);
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(final ZipFile archive, File targetDirectory)
+ throws IOException, ArchiveException {
+ final Enumeration<ZipArchiveEntry> entries = archive.getEntries();
+ expand(new ArchiveEntrySupplier() {
+ @Override
+ public ArchiveEntry getNextReadableEntry() throws IOException {
+ ZipArchiveEntry next = entries.hasMoreElements() ? entries.nextElement() : null;
+ while (next != null && !archive.canReadEntryData(next)) {
+ next = entries.hasMoreElements() ? entries.nextElement() : null;
+ }
+ return next;
+ }
+ }, new EntryWriter() {
+ @Override
+ public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
+ try (InputStream in = archive.getInputStream((ZipArchiveEntry) entry)) {
+ IOUtils.copy(in, out);
+ }
+ }
+ }, targetDirectory);
+ }
+
+ /**
+ * Expands {@code archive} into {@code targetDirectory}.
+ *
+ * @param archive the file to expand
+ * @param targetDirectory the directory to write to
+ * @throws IOException if an I/O error occurs
+ * @throws ArchiveException if the archive cannot be read for other reasons
+ */
+ public void expand(final SevenZFile archive, File targetDirectory)
+ throws IOException, ArchiveException {
+ expand(new ArchiveEntrySupplier() {
+ @Override
+ public ArchiveEntry getNextReadableEntry() throws IOException {
+ return archive.getNextEntry();
+ }
+ }, new EntryWriter() {
+ @Override
+ public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
+ final byte[] buffer = new byte[8024];
+ int n = 0;
+ long count = 0;
+ while (-1 != (n = archive.read(buffer))) {
+ out.write(buffer, 0, n);
+ count += n;
+ }
+ }
+ }, targetDirectory);
+ }
+
+ private boolean prefersSeekableByteChannel(String format) {
+ return ArchiveStreamFactory.ZIP.equalsIgnoreCase(format) || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
+ }
+
+ private void expand(ArchiveEntrySupplier supplier, EntryWriter writer, File targetDirectory)
+ throws IOException {
+ String targetDirPath = targetDirectory.getCanonicalPath();
+ ArchiveEntry nextEntry = supplier.getNextReadableEntry();
+ while (nextEntry != null) {
+ File f = new File(targetDirectory, nextEntry.getName());
+ if (!f.getCanonicalPath().startsWith(targetDirPath)) {
+ throw new IOException("expanding " + nextEntry.getName()
+ + " would craete file outside of " + targetDirectory);
+ }
+ if (nextEntry.isDirectory()) {
+ f.mkdirs();
+ } else {
+ f.getParentFile().mkdirs();
+ try (OutputStream o = Files.newOutputStream(f.toPath())) {
+ writer.writeEntryDataTo(nextEntry, o);
+ }
+ }
+ nextEntry = supplier.getNextReadableEntry();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/FileFilterAdapter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/FileFilterAdapter.java b/src/main/java/org/apache/commons/compress/archivers/examples/FileFilterAdapter.java
deleted file mode 100644
index 9f5a846..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/FileFilterAdapter.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.FileFilter;
-
-/**
- * @since 1.17
- */
-public class FileFilterAdapter extends Filter<File> {
- private final FileFilter filter;
- public FileFilterAdapter(FileFilter f) {
- filter = f;
- }
-
- @Override
- public boolean accept(String entryName, File entry) {
- return filter.accept(entry);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java b/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
deleted file mode 100644
index 7f9fa3d..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/FileToArchiveSink.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.archivers.ArchiveOutputStream;
-import org.apache.commons.compress.utils.IOUtils;
-
-/**
- * Sink that creates an archive from files.
- * @since 1.17
- */
-public class FileToArchiveSink extends Sink<File> {
- private final ArchiveOutputStream os;
-
- /**
- * Wraps an ArchiveOutputStream.
- *
- * @param os the stream to write to
- */
- public FileToArchiveSink(ArchiveOutputStream os) {
- this.os = os;
- }
-
- @Override
- public void consume(ChainPayload<File> payload) throws IOException, ArchiveException {
- ArchiveEntry e = os.createArchiveEntry(payload.getEntry(), payload.getEntryName());
- os.putArchiveEntry(e);
- if (!payload.getEntry().isDirectory()) {
- try (InputStream in = new BufferedInputStream(payload.getInput().get())) {
- IOUtils.copy(in, os);
- }
- }
- os.closeArchiveEntry();
- }
-
- @Override
- public void finish() throws IOException {
- os.finish();
- }
-
- @Override
- public void close() throws IOException {
- os.close();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Filter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Filter.java b/src/main/java/org/apache/commons/compress/archivers/examples/Filter.java
deleted file mode 100644
index 84e670c..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Filter.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Filtering stage of a {@link Expand} or {@link Archive} chain.
- * @since 1.17
- */
-public abstract class Filter<T> implements ChainStep<T> {
- /**
- * Decides whether to process an entry or not.
- *
- * @param entryName name of the entry
- * @param entry the entry
- * @return true if the entry shall be processed.
- */
- public abstract boolean accept(String entryName, T entry);
-
- @Override
- public void process(ChainPayload<T> payload, Chain<T> chain) throws IOException, ArchiveException {
- if (accept(payload.getEntryName(), payload.getEntry())) {
- chain.next(payload);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ListerCli.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ListerCli.java b/src/main/java/org/apache/commons/compress/archivers/examples/ListerCli.java
deleted file mode 100644
index 36f6efa..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ListerCli.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-
-/**
- * Simple command line application that lists the contents of an archive.
- *
- * <p>The name of the archive must be given as a command line argument.</p>
- * <p>The optional second argument defines the archive type, in case the format is not recognized.</p>
- *
- * @since 1.17
- */
-public final class ListerCli {
-
- public static void main(final String[] args) throws Exception {
- if (args.length == 0) {
- usage();
- return;
- }
- System.out.println("Analysing " + args[0]);
- final Sink<ArchiveEntry> sink = new Sink<ArchiveEntry>() {
- @Override
- public void consume(ChainPayload<ArchiveEntry> payload) {
- System.out.println(payload.getEntry().getName());
- }
- @Override
- public void close() {
- }
- };
-
- final File f = new File(args[0]);
- if (!f.isFile()) {
- System.err.println(f + " doesn't exist or is a directory");
- } else if (args.length == 1) {
- try (ArchiveEntrySource source = ArchiveSources.forFile(f).detectFormat()) {
- Expand.source(source).to(sink);
- }
- } else {
- try (ArchiveEntrySource source = ArchiveSources.forFile(f).withFormat(args[1])) {
- Expand.source(source).to(sink);
- }
- }
- }
-
- private static void usage() {
- System.out.println("Parameters: archive-name [archive-type]");
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/SevenZArchiveEntrySource.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/SevenZArchiveEntrySource.java b/src/main/java/org/apache/commons/compress/archivers/examples/SevenZArchiveEntrySource.java
deleted file mode 100644
index 9f38b38..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/SevenZArchiveEntrySource.java
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.channels.SeekableByteChannel;
-import java.util.NoSuchElementException;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.utils.NoCloseInputStream;
-import org.apache.commons.compress.archivers.sevenz.SevenZFile;
-
-/**
- * Supplier based on {@link SevenZFile}s.
- * @since 1.17
- */
-public class SevenZArchiveEntrySource implements ArchiveEntrySource {
-
- private final SevenZFile sf;
-
- public SevenZArchiveEntrySource(File f) throws IOException {
- this(new SevenZFile(f));
- }
-
- public SevenZArchiveEntrySource(SeekableByteChannel c) throws IOException {
- this(new SevenZFile(c));
- }
-
- public SevenZArchiveEntrySource(SevenZFile sf) {
- this.sf = sf;
- }
-
- @Override
- public ThrowingIterator<ChainPayload<ArchiveEntry>> get() throws IOException {
- return new SevenZFileIterator(sf);
- }
-
- @Override
- public void close() throws IOException {
- sf.close();
- }
-
- @Override
- public Filter<ArchiveEntry> skipUnreadable() {
- return new Filter<ArchiveEntry>() {
- @Override
- public boolean accept(String entryName, ArchiveEntry entry) {
- return true;
- }
- };
- }
-
- private static class SevenZFileIterator implements ThrowingIterator<ChainPayload<ArchiveEntry>> {
- private final SevenZFile sf;
- private ArchiveEntry nextEntry;
- private boolean nextEntryConsumed;
- SevenZFileIterator(SevenZFile sf) throws IOException {
- this.sf = sf;
- nextEntry = sf.getNextEntry();
- nextEntryConsumed = false;
- }
-
- @Override
- public boolean hasNext() throws IOException {
- if (nextEntry == null || nextEntryConsumed) {
- nextEntry = sf.getNextEntry();
- nextEntryConsumed = false;
- }
- return nextEntry != null && !nextEntryConsumed;
- }
-
- @Override
- public ChainPayload<ArchiveEntry> next() throws IOException {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- nextEntryConsumed = true;
- return new ChainPayload(nextEntry, nextEntry.getName(), new Supplier<InputStream>() {
- @Override
- public InputStream get() throws IOException {
- return new SevenZFileInputStream(sf);
- }
- });
- }
-
- }
-
- private static class SevenZFileInputStream extends InputStream {
- private final SevenZFile sf;
- SevenZFileInputStream(SevenZFile sf) {
- this.sf = sf;
- }
- @Override
- public int read() throws IOException {
- return sf.read();
- }
- @Override
- public int read(byte[] b) throws IOException {
- return read(b, 0, b.length);
- }
- @Override
- public int read(final byte[] b, final int off, final int len) throws IOException {
- return sf.read(b, off, len);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/SevenZOutputFileSink.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/SevenZOutputFileSink.java b/src/main/java/org/apache/commons/compress/archivers/examples/SevenZOutputFileSink.java
deleted file mode 100644
index f9a1e14..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/SevenZOutputFileSink.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.channels.SeekableByteChannel;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
-import org.apache.commons.compress.utils.IOUtils;
-
-/**
- * Sink that creates a 7z archive from files.
- * @since 1.17
- */
-public class SevenZOutputFileSink extends Sink<File> {
-
- private final SevenZOutputFile outFile;
-
- public SevenZOutputFileSink(File f) throws IOException {
- this(new SevenZOutputFile(f));
- }
-
- public SevenZOutputFileSink(SeekableByteChannel c) throws IOException {
- this(new SevenZOutputFile(c));
- }
-
- public SevenZOutputFileSink(SevenZOutputFile outFile) {
- this.outFile = outFile;
- }
-
- @Override
- public void consume(ChainPayload<File> payload) throws IOException, ArchiveException {
- ArchiveEntry e = outFile.createArchiveEntry(payload.getEntry(), payload.getEntryName());
- outFile.putArchiveEntry(e);
- if (!payload.getEntry().isDirectory()) {
- final byte[] buffer = new byte[8024];
- int n = 0;
- long count = 0;
- try (InputStream in = new BufferedInputStream(payload.getInput().get())) {
- while (-1 != (n = in.read(buffer))) {
- outFile.write(buffer, 0, n);
- count += n;
- }
- }
- }
- outFile.closeArchiveEntry();
- }
-
- @Override
- public void finish() throws IOException {
- outFile.finish();
- }
-
- @Override
- public void close() throws IOException {
- outFile.close();
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Sink.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Sink.java b/src/main/java/org/apache/commons/compress/archivers/examples/Sink.java
deleted file mode 100644
index 7e14369..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Sink.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.Closeable;
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Final stage of a {@link Expand} or {@link Archive} chain.
- * @since 1.17
- */
-public abstract class Sink<T> implements ChainStep<T>, Closeable {
- /**
- * Consume a single entry.
- *
- * @param payload the entry to consume
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if an archive format related error occurs
- */
- public abstract void consume(ChainPayload<T> payload) throws IOException, ArchiveException;
-
- /**
- * Is invoked once all entries have been processed.
- *
- * <p>This implementation is empty.
- *
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if an archive format related error occurs
- */
- public void finish() throws IOException, ArchiveException {
- }
-
- @Override
- public void process(ChainPayload<T> payload, Chain<T> chain) throws IOException, ArchiveException {
- consume(payload);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Source.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Source.java b/src/main/java/org/apache/commons/compress/archivers/examples/Source.java
deleted file mode 100644
index 4a51efe..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Source.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.Closeable;
-
-/**
- * Describes the contract of a source for {@link Archive} or {@link Expand}.
- * @since 1.17
- */
-public interface Source<T> extends Supplier<ThrowingIterator<ChainPayload<T>>>, Closeable {
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/StreamBasedArchiveEntrySource.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/StreamBasedArchiveEntrySource.java b/src/main/java/org/apache/commons/compress/archivers/examples/StreamBasedArchiveEntrySource.java
deleted file mode 100644
index 19aa55b..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/StreamBasedArchiveEntrySource.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.NoSuchElementException;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveException;
-import org.apache.commons.compress.archivers.ArchiveInputStream;
-import org.apache.commons.compress.archivers.ArchiveStreamFactory;
-import org.apache.commons.compress.utils.NoCloseInputStream;
-
-/**
- * Supplier based on {@link ArchiveInputStream}s.
- * @since 1.17
- */
-public class StreamBasedArchiveEntrySource implements ArchiveEntrySource {
-
- private final ArchiveInputStream in;
-
- public StreamBasedArchiveEntrySource(ArchiveInputStream in) {
- this.in = in;
- }
-
- @Override
- public ThrowingIterator<ChainPayload<ArchiveEntry>> get() throws IOException {
- return new ArchiveInputStreamIterator(in);
- }
-
- @Override
- public void close() throws IOException {
- in.close();
- }
-
- @Override
- public Filter<ArchiveEntry> skipUnreadable() {
- return new Filter<ArchiveEntry>() {
- @Override
- public boolean accept(String entryName, ArchiveEntry entry) {
- return in.canReadEntryData(entry);
- }
- };
- }
-
- private static class ArchiveInputStreamIterator implements ThrowingIterator<ChainPayload<ArchiveEntry>> {
- private final ArchiveInputStream in;
- private ArchiveEntry nextEntry;
- private boolean nextEntryConsumed;
- ArchiveInputStreamIterator(ArchiveInputStream in) throws IOException {
- this.in = in;
- nextEntry = in.getNextEntry();
- nextEntryConsumed = false;
- }
-
- @Override
- public boolean hasNext() throws IOException {
- if (nextEntry == null || nextEntryConsumed) {
- nextEntry = in.getNextEntry();
- nextEntryConsumed = false;
- }
- return nextEntry != null && !nextEntryConsumed;
- }
-
- @Override
- public ChainPayload<ArchiveEntry> next() throws IOException {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- nextEntryConsumed = true;
- return new ChainPayload(nextEntry, nextEntry.getName(), new Supplier<InputStream>() {
- @Override
- public InputStream get() throws IOException {
- return new NoCloseInputStream(in);
- }
- });
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Supplier.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Supplier.java b/src/main/java/org/apache/commons/compress/archivers/examples/Supplier.java
deleted file mode 100644
index aba6113..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Supplier.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Used inside of {@link ChainPayload} as well as {@link Archive} and {@link Expand}.
- * @since 1.12
- */
-public interface Supplier<T> {
- /**
- * Supplies the object.
- *
- * @throws IOException if an I/O error occurs
- * @throws ArchiveException if an archive format related error occurs
- * @return the asked for object
- */
- T get() throws IOException, ArchiveException;
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ThrowingIterator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ThrowingIterator.java b/src/main/java/org/apache/commons/compress/archivers/examples/ThrowingIterator.java
deleted file mode 100644
index 4a7bad8..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ThrowingIterator.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Specialized iterator that is allowed to throw Exceptions.
- */
-public interface ThrowingIterator<T> {
- boolean hasNext() throws IOException, ArchiveException;
- T next() throws IOException, ArchiveException;
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/Transformer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Transformer.java b/src/main/java/org/apache/commons/compress/archivers/examples/Transformer.java
deleted file mode 100644
index b091678..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Transformer.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.IOException;
-import org.apache.commons.compress.archivers.ArchiveException;
-
-/**
- * Transforming stage of a {@link Expand} or {@link Archive} chain.
- * @since 1.17
- */
-public abstract class Transformer<T> implements ChainStep<T> {
- /**
- * Transforms an entry.
- *
- * @param entry the entry
- * @return the transformed entry
- */
- public abstract ChainPayload<T> transform(ChainPayload<T> entry);
-
- @Override
- public void process(ChainPayload<T> payload, Chain<T> chain) throws IOException, ArchiveException {
- chain.next(transform(payload));
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/ZipArchiveEntrySource.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/ZipArchiveEntrySource.java b/src/main/java/org/apache/commons/compress/archivers/examples/ZipArchiveEntrySource.java
deleted file mode 100644
index d5e84bc..0000000
--- a/src/main/java/org/apache/commons/compress/archivers/examples/ZipArchiveEntrySource.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * 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.examples;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.channels.SeekableByteChannel;
-import java.util.Enumeration;
-import java.util.NoSuchElementException;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
-import org.apache.commons.compress.archivers.zip.ZipFile;
-
-/**
- * Supplier based on {@link ZipFile}s.
- * @since 1.17
- */
-public class ZipArchiveEntrySource implements ArchiveEntrySource {
-
- private final ZipFile zf;
-
- public ZipArchiveEntrySource(File f) throws IOException {
- this(new ZipFile(f));
- }
-
- public ZipArchiveEntrySource(SeekableByteChannel c) throws IOException {
- this(new ZipFile(c));
- }
-
- public ZipArchiveEntrySource(ZipFile file) {
- zf = file;
- }
-
- @Override
- public ThrowingIterator<ChainPayload<ArchiveEntry>> get() throws IOException {
- return new ZipFileIterator(zf, zf.getEntries());
- }
-
- @Override
- public void close() throws IOException {
- zf.close();
- }
-
- @Override
- public Filter<ArchiveEntry> skipUnreadable() {
- return new Filter<ArchiveEntry>() {
- @Override
- public boolean accept(String entryName, ArchiveEntry entry) {
- return entry instanceof ZipArchiveEntry && zf.canReadEntryData((ZipArchiveEntry) entry);
- }
- };
- }
-
- private static class ZipFileIterator implements ThrowingIterator<ChainPayload<ArchiveEntry>> {
- private final ZipFile zf;
- private final Enumeration<ZipArchiveEntry> iter;
- ZipFileIterator(ZipFile zf, Enumeration<ZipArchiveEntry> iter) {
- this.zf = zf;
- this.iter = iter;
- }
-
- @Override
- public boolean hasNext() throws IOException {
- return iter.hasMoreElements();
- }
-
- @Override
- public ChainPayload<ArchiveEntry> next() throws IOException {
- final ZipArchiveEntry z = iter.nextElement();
- return new ChainPayload(z, z.getName(), new Supplier<InputStream>() {
- @Override
- public InputStream get() throws IOException {
- return zf.getInputStream(z);
- }
- });
- }
-
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/archivers/examples/package.html
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/package.html b/src/main/java/org/apache/commons/compress/archivers/examples/package.html
index 14e6c5a..443d5fc 100644
--- a/src/main/java/org/apache/commons/compress/archivers/examples/package.html
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/package.html
@@ -18,75 +18,8 @@
-->
<body>
- <p>Contains examples code that is not guaranteed to provide a
+ <p>Contains example code that is not guaranteed to provide a
stable API across releases of Commons Compress.</p>
- <p>The majority of this package is concerned with the archival of
- the contents of a directory into a single archive or the expansion
- of an archive's content into a local directory. Example CLI
- programs exist with {@link
- org.apache.commons.compress.archivers.examples.ArchiveCli}, {@link
- org.apache.commons.compress.archivers.examples.ExpandCli} and
- {@link
- org.apache.commons.compress.archivers.examples.ListerCli}. As of
- Commons Compress 1.17 this is the main class of the Commons
- Compress jar, i.e. the class that is run when you start <code>java
- -jar commons-compress.jar</code>.</p>
-
- <h2>Chains</h2>
-
- <p>The basic abstraction of the package is the {@link
- org.apache.commons.compress.archivers.examples.Chain}, a chain of
- {@link org.apache.commons.compress.archivers.examples.ChainStep}s
- that are executed in order for each entry. Entries are supplied by
- a {@link org.apache.commons.compress.archivers.examples.Source}
- and finally consumed by a {@link
- org.apache.commons.compress.archivers.examples.Sink}. While {@code
- Sink}s are {@code ChainStep}s and must be the final step in a
- {@code Chain}, {@code Source}s are not considered part of the
- {@code Chain}.</p>
-
- <p>Special {@code ChainStep}s exist for filtering entries with
- {@link org.apache.commons.compress.archivers.examples.Filter} or
- transforming entries {@link
- org.apache.commons.compress.archivers.examples.Transformer} as
- they pass through the chain.</p>
-
- <h3>Archival and Expansion</h3>
-
- <p>A chain that takes files from the local file system and creates
- an archive from that can be set up and run with the help of the
- {@link org.apache.commons.compress.archivers.examples.Archive}
- class. The most common source is {@link
- org.apache.commons.compress.archivers.examples.DirectoryBasedSource}
- which simply supplies all files contained within the directory
- recursively. The {@link
- org.apache.commons.compress.archivers.examples.ArchiveSinks} class
- provides factory methods for the many ways that can bes used to
- create archives to write to - all those factory methods end up
- with creating instances of {@link
- org.apache.commons.compress.archivers.examples.FileToArchiveSink}
- or {@link
- org.apache.commons.compress.archivers.examples.SevenZOutputFileSink}
- under the covers.</p>
-
- <p>A chain that takes {@link
- org.apache.commons.compress.archivers.ArchiveEntry}s from a source
- and passes them to a sink can be set up and run with the help of
- the {@link org.apache.commons.compress.archivers.examples.Expand}
- class. The most common sink will be a {@link
- org.apache.commons.compress.archivers.examples.DirectorySink}
- which writes the entries to a local directory, but different sinks
- are easy to imagine - and in fact {@code ListerCli} uses {@code
- Expand} and simply provides a sink that just prints out the names
- of the entries. Sources that read from archives can be created via
- the factory methods in {@link
- org.apache.commons.compress.archivers.examples.ArchiveSources}
- which under the covers will create instances of {@link
- org.apache.commons.compress.archivers.examples.StreamBasedArchiveEntrySource},
- {@link
- org.apache.commons.compress.archivers.examples.SevenZArchiveEntrySource}
- or {@link
- org.apache.commons.compress.archivers.examples.ZipArchiveEntrySource}.</p>
</body>
</html>
http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7a10230e/src/main/java/org/apache/commons/compress/utils/NoCloseInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/utils/NoCloseInputStream.java b/src/main/java/org/apache/commons/compress/utils/NoCloseInputStream.java
deleted file mode 100644
index bdc0ee9..0000000
--- a/src/main/java/org/apache/commons/compress/utils/NoCloseInputStream.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.utils;
-
-import java.io.FilterInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * Wrapper that overrides {@link #close} so that it doesn't close the
- * underlying stream.
- *
- * @since 1.17
- */
-public class NoCloseInputStream extends FilterInputStream {
-
- public NoCloseInputStream(InputStream in) {
- super(in);
- }
-
- /**
- * This method does nothing.
- */
- public void close() {
- // do not close the stream
- }
-}