/** * Recursion entry point for {@link #writeToArchive(File, File[], ArchiveOutputStream)}. * <br> * Recursively writes all given source {@link File}s into the given {@link ArchiveOutputStream}. * * @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[] sources, ArchiveOutputStream archive) throws IOException { for (File source : sources) { if (!source.exists()) { throw new FileNotFoundException(source.getPath()); } else if (!source.canRead()) { throw new FileNotFoundException(source.getPath() + " (Permission denied)"); } writeToArchive(source.getParentFile(), new File[]{ source }, archive); } }
/** * Recursion entry point for {@link #writeToArchive(File, File[], ArchiveOutputStream)}. * <br> * Recursively writes all given source {@link File}s into the given {@link ArchiveOutputStream}. * * @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[] sources, ArchiveOutputStream archive) throws IOException { for (File source : sources) { if (!source.exists()) { throw new FileNotFoundException(source.getPath()); } else if (!source.canRead()) { throw new FileNotFoundException(source.getPath() + " (Permission denied)"); } writeToArchive(source.getParentFile(), new File[]{ source }, archive); } }
/** * 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); } } }
/** * 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 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; }