private void closeCurrentEntryStream() { InputStream stream = getCurrentEntryStream(); IOUtils.closeQuietly(stream); currentEntryStream = null; }
private void closeCurrentEntryStream() { InputStream stream = getCurrentEntryStream(); IOUtils.closeQuietly(stream); currentEntryStream = null; }
/** * Copies the content of an InputStream into a destination File. * * @param source the InputStream to copy * @param destination the target File * @throws IOException if an error occurs */ public static void copy(InputStream source, File destination) throws IOException { OutputStream output = null; try { output = new FileOutputStream(destination); copy(source, output); } finally { closeQuietly(output); } }
/** * Copies the content of an InputStream into a destination File. * * @param source the InputStream to copy * @param destination the target File * @throws IOException if an error occurs */ public static void copy(InputStream source, File destination) throws IOException { OutputStream output = null; try { output = new FileOutputStream(destination); copy(source, output); } finally { closeQuietly(output); } }
@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 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 int read(byte[] b, int off, int len) throws IOException { int read = getCurrentEntryStream().read(b, off, len); if (read == -1) { IOUtils.closeQuietly(getCurrentEntryStream()); } count(read); return read; }
@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 int read(byte[] b, int off, int len) throws IOException { int read = getCurrentEntryStream().read(b, off, len); if (read == -1) { IOUtils.closeQuietly(getCurrentEntryStream()); } count(read); return read; }
@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 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 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 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); } }
/** * Creates a new {@link ArchiveEntry} in the given {@link ArchiveOutputStream}, and copies the given {@link File} * into the new entry. * * @param file the file to add to the archive * @param entryName the name of the archive entry * @param archive the archive to write to * @throws IOException when an I/O error occurs during FileInputStream creation or during copying */ protected void createArchiveEntry(File file, String entryName, ArchiveOutputStream archive) throws IOException { ArchiveEntry entry = archive.createArchiveEntry(file, entryName); // TODO #23: read permission from file, write it to the ArchiveEntry archive.putArchiveEntry(entry); if (!entry.isDirectory()) { FileInputStream input = null; try { input = new FileInputStream(file); IOUtils.copy(input, archive); } finally { IOUtils.closeQuietly(input); } } archive.closeArchiveEntry(); } }
/** * Creates a new {@link ArchiveEntry} in the given {@link ArchiveOutputStream}, and copies the given {@link File} * into the new entry. * * @param file the file to add to the archive * @param entryName the name of the archive entry * @param archive the archive to write to * @throws IOException when an I/O error occurs during FileInputStream creation or during copying */ protected void createArchiveEntry(File file, String entryName, ArchiveOutputStream archive) throws IOException { ArchiveEntry entry = archive.createArchiveEntry(file, entryName); // TODO #23: read permission from file, write it to the ArchiveEntry archive.putArchiveEntry(entry); if (!entry.isDirectory()) { FileInputStream input = null; try { input = new FileInputStream(file); IOUtils.copy(input, archive); } finally { IOUtils.closeQuietly(input); } } archive.closeArchiveEntry(); } }
@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 archiveFile = createNewArchiveFile(archive, getFilenameExtension(), destination); ArchiveOutputStream outputStream = null; try { outputStream = createArchiveOutputStream(archiveFile); writeToArchive(sources, outputStream); outputStream.flush(); } finally { IOUtils.closeQuietly(outputStream); } return archiveFile; }