@Test public void zipRecStrFile() throws Exception { File destFile = new File(destDir, "recStrFile.zip"); Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcDirName + File.separator); names.add(nestedSrcDirName + File.separator + nestedSrcFileName); String[] filenames = srcDir.list(); for (int i = 0; i < filenames.length; i++) filenames[i] = srcDir.getCanonicalPath() + File.separator + filenames[i]; File test = ZipUtil.zip(filenames, destFile, true, ZipUtil.NO_COMPRESSION); Assert.assertTrue(test.exists()); ZipFile zip = new ZipFile(test); Assert.assertEquals(3, zip.size()); Enumeration<? extends ZipEntry> entries = zip.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Assert.assertTrue(names.contains(entry.getName().replace('/', File.separatorChar))); } } catch (AssertionError ae) { zip.close(); throw ae; } zip.close(); }
public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level) throws IOException { if (sourceFiles == null) { logger.error("The array with files to zip cannot be null"); throw new IllegalArgumentException("The array with files to zip cannot be null"); } if (sourceFiles.length <= 0) { logger.error("The array with files to zip cannot be empty"); throw new IllegalArgumentException("The array with files to zip cannot be empty"); } if (destination == null) { logger.error("The destination file cannot be null"); throw new IllegalArgumentException("The destination file cannot be null"); } if (destination.exists()) { logger.error("The destination file {} already exists", destination.getCanonicalPath()); throw new IllegalArgumentException("The destination file already exists"); } if (level < -1) { logger.warn("Compression level cannot be less than 0 (or -1 for default)"); logger.warn("Reverting to default..."); level = -1; } else if (level > 9) { logger.warn("Compression level cannot be greater than 9"); logger.warn("Reverting to default..."); level = -1; } ZipDriver zd = new ZipDriver(level); ArchiveDetector ad = new DefaultArchiveDetector(ArchiveDetector.NULL, "zip", zd); File zipFile; try { zipFile = new File(destination.getCanonicalFile(), ad); } catch (IOException ioe) { logger.error("Unable to create the zip file: {}", destination.getAbsolutePath()); throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe); } try { if (!zipFile.isArchive()) { logger.error("The destination file does not represent a valid zip archive (.zip extension is required)"); zipFile.deleteAll(); throw new IllegalArgumentException( "The destination file does not represent a valid zip archive (.zip extension is required)"); } if (!zipFile.mkdirs()) throw new IOException("Couldn't create the destination file"); for (java.io.File f : sourceFiles) { if (f == null) { logger.error("Null inputfile in array"); zipFile.deleteAll(); throw new IllegalArgumentException("Null inputfile in array"); } logger.debug("Attempting to zip file {}...", f.getAbsolutePath()); boolean success = false; if (f.exists()) { if (!f.isDirectory() || recursive) { success = new File(zipFile, f.getName()).copyAllFrom(f); if (success) logger.debug("File {} zipped successfuly", f.getAbsolutePath()); else { logger.error("File {} not zipped", f.getAbsolutePath()); zipFile.deleteAll(); throw new IOException("Failed to zip one of the input files: " + f.getAbsolutePath()); } } } else { logger.error("Input file {} doesn't exist", f.getAbsolutePath()); zipFile.deleteAll(); throw new FileNotFoundException("One of the input files does not exist: " + f.getAbsolutePath()); } } } catch (IOException e) { throw e; } finally { umount(zipFile); } return destination; }
ZipUtil { public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level) throws IOException { if (sourceFiles == null) { logger.error("The array with files to zip cannot be null"); throw new IllegalArgumentException("The array with files to zip cannot be null"); } if (sourceFiles.length <= 0) { logger.error("The array with files to zip cannot be empty"); throw new IllegalArgumentException("The array with files to zip cannot be empty"); } if (destination == null) { logger.error("The destination file cannot be null"); throw new IllegalArgumentException("The destination file cannot be null"); } if (destination.exists()) { logger.error("The destination file {} already exists", destination.getCanonicalPath()); throw new IllegalArgumentException("The destination file already exists"); } if (level < -1) { logger.warn("Compression level cannot be less than 0 (or -1 for default)"); logger.warn("Reverting to default..."); level = -1; } else if (level > 9) { logger.warn("Compression level cannot be greater than 9"); logger.warn("Reverting to default..."); level = -1; } ZipDriver zd = new ZipDriver(level); ArchiveDetector ad = new DefaultArchiveDetector(ArchiveDetector.NULL, "zip", zd); File zipFile; try { zipFile = new File(destination.getCanonicalFile(), ad); } catch (IOException ioe) { logger.error("Unable to create the zip file: {}", destination.getAbsolutePath()); throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe); } try { if (!zipFile.isArchive()) { logger.error("The destination file does not represent a valid zip archive (.zip extension is required)"); zipFile.deleteAll(); throw new IllegalArgumentException( "The destination file does not represent a valid zip archive (.zip extension is required)"); } if (!zipFile.mkdirs()) throw new IOException("Couldn't create the destination file"); for (java.io.File f : sourceFiles) { if (f == null) { logger.error("Null inputfile in array"); zipFile.deleteAll(); throw new IllegalArgumentException("Null inputfile in array"); } logger.debug("Attempting to zip file {}...", f.getAbsolutePath()); boolean success = false; if (f.exists()) { if (!f.isDirectory() || recursive) { success = new File(zipFile, f.getName()).copyAllFrom(f); if (success) logger.debug("File {} zipped successfuly", f.getAbsolutePath()); else { logger.error("File {} not zipped", f.getAbsolutePath()); zipFile.deleteAll(); throw new IOException("Failed to zip one of the input files: " + f.getAbsolutePath()); } } } else { logger.error("Input file {} doesn't exist", f.getAbsolutePath()); zipFile.deleteAll(); throw new FileNotFoundException("One of the input files does not exist: " + f.getAbsolutePath()); } } } catch (IOException e) { throw e; } finally { umount(zipFile); } return destination; } }
ZipUtil { public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level) throws IOException { if (sourceFiles == null) { logger.error("The array with files to zip cannot be null"); throw new IllegalArgumentException("The array with files to zip cannot be null"); } if (sourceFiles.length <= 0) { logger.error("The array with files to zip cannot be empty"); throw new IllegalArgumentException("The array with files to zip cannot be empty"); } if (destination == null) { logger.error("The destination file cannot be null"); throw new IllegalArgumentException("The destination file cannot be null"); } if (destination.exists()) { logger.error("The destination file {} already exists", destination.getCanonicalPath()); throw new IllegalArgumentException("The destination file already exists"); } if (level < -1) { logger.warn("Compression level cannot be less than 0 (or -1 for default)"); logger.warn("Reverting to default..."); level = -1; } else if (level > 9) { logger.warn("Compression level cannot be greater than 9"); logger.warn("Reverting to default..."); level = -1; } ZipDriver zd = new ZipDriver(level); ArchiveDetector ad = new DefaultArchiveDetector(ArchiveDetector.NULL, "zip", zd); File zipFile; try { zipFile = new File(destination.getCanonicalFile(), ad); } catch (IOException ioe) { logger.error("Unable to create the zip file: {}", destination.getAbsolutePath()); throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe); } try { if (!zipFile.isArchive()) { logger.error("The destination file does not represent a valid zip archive (.zip extension is required)"); zipFile.deleteAll(); throw new IllegalArgumentException( "The destination file does not represent a valid zip archive (.zip extension is required)"); } if (!zipFile.mkdirs()) throw new IOException("Couldn't create the destination file"); for (java.io.File f : sourceFiles) { if (f == null) { logger.error("Null inputfile in array"); zipFile.deleteAll(); throw new IllegalArgumentException("Null inputfile in array"); } logger.debug("Attempting to zip file {}...", f.getAbsolutePath()); boolean success = false; if (f.exists()) { if (!f.isDirectory() || recursive) { success = new File(zipFile, f.getName()).copyAllFrom(f); if (success) logger.debug("File {} zipped successfuly", f.getAbsolutePath()); else { logger.error("File {} not zipped", f.getAbsolutePath()); zipFile.deleteAll(); throw new IOException("Failed to zip one of the input files: " + f.getAbsolutePath()); } } } else { logger.error("Input file {} doesn't exist", f.getAbsolutePath()); zipFile.deleteAll(); throw new FileNotFoundException("One of the input files does not exist: " + f.getAbsolutePath()); } } } catch (IOException e) { throw e; } finally { umount(zipFile); } return destination; } private  ZipUtil(); }
ZipUtil { public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level) throws IOException { if (sourceFiles == null) { logger.error("The array with files to zip cannot be null"); throw new IllegalArgumentException("The array with files to zip cannot be null"); } if (sourceFiles.length <= 0) { logger.error("The array with files to zip cannot be empty"); throw new IllegalArgumentException("The array with files to zip cannot be empty"); } if (destination == null) { logger.error("The destination file cannot be null"); throw new IllegalArgumentException("The destination file cannot be null"); } if (destination.exists()) { logger.error("The destination file {} already exists", destination.getCanonicalPath()); throw new IllegalArgumentException("The destination file already exists"); } if (level < -1) { logger.warn("Compression level cannot be less than 0 (or -1 for default)"); logger.warn("Reverting to default..."); level = -1; } else if (level > 9) { logger.warn("Compression level cannot be greater than 9"); logger.warn("Reverting to default..."); level = -1; } ZipDriver zd = new ZipDriver(level); ArchiveDetector ad = new DefaultArchiveDetector(ArchiveDetector.NULL, "zip", zd); File zipFile; try { zipFile = new File(destination.getCanonicalFile(), ad); } catch (IOException ioe) { logger.error("Unable to create the zip file: {}", destination.getAbsolutePath()); throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe); } try { if (!zipFile.isArchive()) { logger.error("The destination file does not represent a valid zip archive (.zip extension is required)"); zipFile.deleteAll(); throw new IllegalArgumentException( "The destination file does not represent a valid zip archive (.zip extension is required)"); } if (!zipFile.mkdirs()) throw new IOException("Couldn't create the destination file"); for (java.io.File f : sourceFiles) { if (f == null) { logger.error("Null inputfile in array"); zipFile.deleteAll(); throw new IllegalArgumentException("Null inputfile in array"); } logger.debug("Attempting to zip file {}...", f.getAbsolutePath()); boolean success = false; if (f.exists()) { if (!f.isDirectory() || recursive) { success = new File(zipFile, f.getName()).copyAllFrom(f); if (success) logger.debug("File {} zipped successfuly", f.getAbsolutePath()); else { logger.error("File {} not zipped", f.getAbsolutePath()); zipFile.deleteAll(); throw new IOException("Failed to zip one of the input files: " + f.getAbsolutePath()); } } } else { logger.error("Input file {} doesn't exist", f.getAbsolutePath()); zipFile.deleteAll(); throw new FileNotFoundException("One of the input files does not exist: " + f.getAbsolutePath()); } } } catch (IOException e) { throw e; } finally { umount(zipFile); } return destination; } private  ZipUtil(); static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level); static void unzip(java.io.File zipFile, java.io.File destination); static java.io.File zip(String[] sourceFiles, String destination, boolean recursive, int level); static java.io.File zip(String[] sourceFiles, java.io.File destination, boolean recursive, int level); static java.io.File zip(java.io.File[] sourceFiles, String destination, boolean recursive, int level); static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, int level); static void unzip(String zipFile, String destination); static void unzip(java.io.File zipFile, String destination); static void unzip(String zipFile, java.io.File destination); }
ZipUtil { public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level) throws IOException { if (sourceFiles == null) { logger.error("The array with files to zip cannot be null"); throw new IllegalArgumentException("The array with files to zip cannot be null"); } if (sourceFiles.length <= 0) { logger.error("The array with files to zip cannot be empty"); throw new IllegalArgumentException("The array with files to zip cannot be empty"); } if (destination == null) { logger.error("The destination file cannot be null"); throw new IllegalArgumentException("The destination file cannot be null"); } if (destination.exists()) { logger.error("The destination file {} already exists", destination.getCanonicalPath()); throw new IllegalArgumentException("The destination file already exists"); } if (level < -1) { logger.warn("Compression level cannot be less than 0 (or -1 for default)"); logger.warn("Reverting to default..."); level = -1; } else if (level > 9) { logger.warn("Compression level cannot be greater than 9"); logger.warn("Reverting to default..."); level = -1; } ZipDriver zd = new ZipDriver(level); ArchiveDetector ad = new DefaultArchiveDetector(ArchiveDetector.NULL, "zip", zd); File zipFile; try { zipFile = new File(destination.getCanonicalFile(), ad); } catch (IOException ioe) { logger.error("Unable to create the zip file: {}", destination.getAbsolutePath()); throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe); } try { if (!zipFile.isArchive()) { logger.error("The destination file does not represent a valid zip archive (.zip extension is required)"); zipFile.deleteAll(); throw new IllegalArgumentException( "The destination file does not represent a valid zip archive (.zip extension is required)"); } if (!zipFile.mkdirs()) throw new IOException("Couldn't create the destination file"); for (java.io.File f : sourceFiles) { if (f == null) { logger.error("Null inputfile in array"); zipFile.deleteAll(); throw new IllegalArgumentException("Null inputfile in array"); } logger.debug("Attempting to zip file {}...", f.getAbsolutePath()); boolean success = false; if (f.exists()) { if (!f.isDirectory() || recursive) { success = new File(zipFile, f.getName()).copyAllFrom(f); if (success) logger.debug("File {} zipped successfuly", f.getAbsolutePath()); else { logger.error("File {} not zipped", f.getAbsolutePath()); zipFile.deleteAll(); throw new IOException("Failed to zip one of the input files: " + f.getAbsolutePath()); } } } else { logger.error("Input file {} doesn't exist", f.getAbsolutePath()); zipFile.deleteAll(); throw new FileNotFoundException("One of the input files does not exist: " + f.getAbsolutePath()); } } } catch (IOException e) { throw e; } finally { umount(zipFile); } return destination; } private  ZipUtil(); static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level); static void unzip(java.io.File zipFile, java.io.File destination); static java.io.File zip(String[] sourceFiles, String destination, boolean recursive, int level); static java.io.File zip(String[] sourceFiles, java.io.File destination, boolean recursive, int level); static java.io.File zip(java.io.File[] sourceFiles, String destination, boolean recursive, int level); static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, int level); static void unzip(String zipFile, String destination); static void unzip(java.io.File zipFile, String destination); static void unzip(String zipFile, java.io.File destination); static final int BEST_SPEED; static final int BEST_COMPRESSION; static final int DEFAULT_COMPRESSION; static final int NO_COMPRESSION; }