/** * Creates a compressor from the given CompressionType. * * @param compression the type of the compression algorithm * @return a new {@link Compressor} instance that uses the specified compression algorithm. */ public static Compressor createCompressor(CompressionType compression) { return new CommonsCompressor(compression); }
@Override public InputStream decompressingStream(InputStream compressedStream) throws IOException { try { return CommonsStreamFactory.createCompressorInputStream(getCompressionType(), compressedStream); } catch (CompressorException e) { throw new IOException(e); } }
private String getCompressedFilename(File source) { return source.getName() + getFilenameExtension(); }
@Override public void decompress(File source, File destination) throws IOException { assertSource(source); assertDestination(destination); if (destination.isDirectory()) { destination = new File(destination, getDecompressedFilename(source)); } CompressorInputStream compressed = null; FileOutputStream output = null; try { compressed = createCompressorInputStream(getCompressionType(), source); output = new FileOutputStream(destination); IOUtils.copy(compressed, output); } catch (CompressorException e) { throw new IOException(e); } finally { IOUtils.closeQuietly(compressed); IOUtils.closeQuietly(output); } }
@Override public void compress(File source, File destination) throws IllegalArgumentException, IOException { assertSource(source); assertDestination(destination); if (destination.isDirectory()) { destination = new File(destination, getCompressedFilename(source)); } CompressorOutputStream compressed = null; BufferedInputStream input = null; try { input = new BufferedInputStream(new FileInputStream(source)); compressed = createCompressorOutputStream(this, destination); IOUtils.copy(input, compressed); } catch (CompressorException e) { throw new IOException(e); } finally { IOUtils.closeQuietly(compressed); IOUtils.closeQuietly(input); } }
@Override public void extract(InputStream archive, File destination) throws IOException { IOUtils.requireDirectory(destination); archiver.extract(compressor.decompressingStream(archive), 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 void decompress(File source, File destination) throws IOException { assertSource(source); assertDestination(destination); if (destination.isDirectory()) { destination = new File(destination, getDecompressedFilename(source)); } CompressorInputStream compressed = null; FileOutputStream output = null; try { compressed = createCompressorInputStream(getCompressionType(), source); output = new FileOutputStream(destination); IOUtils.copy(compressed, output); } catch (CompressorException e) { throw new IOException(e); } finally { IOUtils.closeQuietly(compressed); IOUtils.closeQuietly(output); } }
@Override public void compress(File source, File destination) throws IllegalArgumentException, IOException { assertSource(source); assertDestination(destination); if (destination.isDirectory()) { destination = new File(destination, getCompressedFilename(source)); } CompressorOutputStream compressed = null; BufferedInputStream input = null; try { input = new BufferedInputStream(new FileInputStream(source)); compressed = createCompressorOutputStream(this, destination); IOUtils.copy(input, compressed); } catch (CompressorException e) { throw new IOException(e); } finally { IOUtils.closeQuietly(compressed); IOUtils.closeQuietly(input); } }
@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); } }
@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 InputStream decompressingStream(InputStream compressedStream) throws IOException { try { return CommonsStreamFactory.createCompressorInputStream(getCompressionType(), compressedStream); } catch (CompressorException e) { throw new IOException(e); } }
private String getCompressedFilename(File source) { return source.getName() + getFilenameExtension(); }
@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 a compressor from the given CompressionType. * * @param compression the type of the compression algorithm * @return a new {@link Compressor} instance that uses the specified compression algorithm. */ public static Compressor createCompressor(CompressionType compression) { return new CommonsCompressor(compression); }
@Override public String getFilenameExtension() { return getCompressionType().getDefaultFileExtension(); }
@Override public String getFilenameExtension() { return archiver.getFilenameExtension() + compressor.getFilenameExtension(); }
@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 getCompressionType().getDefaultFileExtension(); }