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 - } -}