@Override public void extract(File archive, File destination) throws IOException { assertExtractSource(archive); IOUtils.requireDirectory(destination); ArchiveInputStream input = null; try { input = createArchiveInputStream(archive); extract(input, destination); } finally { IOUtils.closeQuietly(input); } }
@Override public File create(String archive, File destination, File source) throws IOException { return create(archive, destination, IOUtils.filesContainedIn(source)); }
/** * Recursively writes all given source {@link File}s into the given {@link ArchiveOutputStream}. The paths of the * sources in the archive will be relative to the given parent {@code File}. * * @param parent the parent file node for computing a relative path (see {@link IOUtils#relativePath(File, File)}) * @param sources the files to write in to the archive * @param archive the archive to write into * @throws IOException when an I/O error occurs */ protected void writeToArchive(File parent, File[] sources, ArchiveOutputStream archive) throws IOException { for (File source : sources) { String relativePath = IOUtils.relativePath(parent, source); createArchiveEntry(source, relativePath, archive); if (source.isDirectory()) { writeToArchive(parent, source.listFiles(), archive); } } }
@Override public void extract(InputStream archive, File destination) throws IOException { ArchiveInputStream input = createArchiveInputStream(archive); extract(input, destination); }
@Override public File create(String archive, File destination, File... sources) throws IOException { IOUtils.requireDirectory(destination); File archiveFile = createNewArchiveFile(archive, getFilenameExtension(), destination); ArchiveOutputStream outputStream = null; try { outputStream = createArchiveOutputStream(archiveFile); writeToArchive(sources, outputStream); outputStream.flush(); } finally { IOUtils.closeQuietly(outputStream); } return archiveFile; }
@Override public File create(String archive, File destination, File... sources) throws IOException { IOUtils.requireDirectory(destination); File temp = File.createTempFile(destination.getName(), archiver.getFilenameExtension(), destination); File destinationArchive = null; try { temp = archiver.create(temp.getName(), temp.getParentFile(), sources); destinationArchive = new File(destination, getArchiveFileName(archive)); compressor.compress(temp, destinationArchive); } finally { temp.delete(); } return destinationArchive; }
/** * @see {@link ArchiveStreamFactory#createArchiveInputStream(String, InputStream)} */ static ArchiveInputStream createArchiveInputStream(CommonsArchiver archiver, InputStream in) throws ArchiveException { return createArchiveInputStream(archiver.getArchiveFormat(), in); }
@Override public void extract(InputStream archive, File destination) throws IOException { IOUtils.requireDirectory(destination); archiver.extract(compressor.decompressingStream(archive), destination); }
/** * Creates an Archiver for the given archive format that uses compression. * * @param archiveFormat the archive format * @param compression the compression algorithm * @return a new Archiver instance that also handles compression */ public static Archiver createArchiver(ArchiveFormat archiveFormat, CompressionType compression) { CommonsArchiver archiver = new CommonsArchiver(archiveFormat); CommonsCompressor compressor = new CommonsCompressor(compression); return new ArchiverCompressorDecorator(archiver, compressor); }
@Override public String getFilenameExtension() { return archiver.getFilenameExtension() + compressor.getFilenameExtension(); }
@Override public ArchiveStream stream(File archive) throws IOException { return new CommonsArchiveStream(createArchiveInputStream(archive)); }
@Override public File create(String archive, File destination, File... sources) throws IOException { IOUtils.requireDirectory(destination); File archiveFile = createNewArchiveFile(archive, getFilenameExtension(), destination); ArchiveOutputStream outputStream = null; try { outputStream = createArchiveOutputStream(archiveFile); writeToArchive(sources, outputStream); outputStream.flush(); } finally { IOUtils.closeQuietly(outputStream); } return archiveFile; }
@Override public void extract(InputStream archive, File destination) throws IOException { ArchiveInputStream input = createArchiveInputStream(archive); extract(input, destination); }
@Override public File create(String archive, File destination, File... sources) throws IOException { IOUtils.requireDirectory(destination); File temp = File.createTempFile(destination.getName(), archiver.getFilenameExtension(), destination); File destinationArchive = null; try { temp = archiver.create(temp.getName(), temp.getParentFile(), sources); destinationArchive = new File(destination, getArchiveFileName(archive)); compressor.compress(temp, destinationArchive); } finally { temp.delete(); } return destinationArchive; }
@Override public String getFilenameExtension() { return getArchiveFormat().getDefaultFileExtension(); }
@Override public void extract(File archive, File destination) throws IOException { IOUtils.requireDirectory(destination); /* * The decompressor has to map F-N-F to I-A-E in some cases to preserve compatibility, * and we don't want that here. */ if (!archive.exists()) { throw new FileNotFoundException(String.format("Archive %s does not exist.", archive.getAbsolutePath())); } InputStream archiveStream = null; try { archiveStream = new BufferedInputStream(new FileInputStream(archive)); archiver.extract(compressor.decompressingStream(archiveStream), destination); } catch (FileNotFoundException e) { // Java throws F-N-F for no access, and callers expect I-A-E for that. throw new IllegalArgumentException(String.format("Access control or other error opening %s", archive.getAbsolutePath()), e); } finally { IOUtils.closeQuietly(archiveStream); } }
/** * Creates an Archiver for the given archive format that uses compression. * * @param archiveFormat the archive format * @param compression the compression algorithm * @return a new Archiver instance that also handles compression */ public static Archiver createArchiver(ArchiveFormat archiveFormat, CompressionType compression) { CommonsArchiver archiver = new CommonsArchiver(archiveFormat); CommonsCompressor compressor = new CommonsCompressor(compression); return new ArchiverCompressorDecorator(archiver, compressor); }
@Override public String getFilenameExtension() { return archiver.getFilenameExtension() + compressor.getFilenameExtension(); }
@Override public ArchiveStream stream(File archive) throws IOException { return new CommonsArchiveStream(createArchiveInputStream(archive)); }
@Override public void extract(File archive, File destination) throws IOException { assertExtractSource(archive); IOUtils.requireDirectory(destination); ArchiveInputStream input = null; try { input = createArchiveInputStream(archive); extract(input, destination); } finally { IOUtils.closeQuietly(input); } }