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; }
@Test public void badInputZipStrStr() throws Exception { File destFile = new File(destDir, "badInputStrStr.zip"); try { try { ZipUtil.zip((String[]) null, destFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when input String array is null"); Assert.fail("Zip should fail when input String array is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input String array (String, String): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), null, nestedSrcFile.getCanonicalPath() }, destFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input filename is null"); Assert.fail("Zip should fail when any input filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input filename (String, String): OK"); } try { ZipUtil.zip( new String[] { srcFile.getCanonicalPath(), dummieFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, destFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input filename does not exist"); Assert.fail("Zip should fail when any input filename does not exist"); } catch (FileNotFoundException e) { logger.debug("Detecting non-existing input filename (String, String): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, (String) null, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination filename is null"); Assert.fail("Zip should fail when destination filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination filename (String, String): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, "", true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination filename is empty"); Assert.fail("Zip should fail when destination filename is empty"); } catch (IllegalArgumentException e) { logger.debug("Detecting empty destination filename (String, String): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, sampleZip.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination filename already exists"); Assert.fail("Zip should fail when destination filename already exists"); } catch (IllegalArgumentException e) { logger.debug("Detecting existing destination filename (String, String): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, dummieFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when the destination filename does not represent a zip file"); Assert.fail("Zip should fail when the destination filename does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination filename not representing a valid zip file (String, String): OK"); } } catch (Exception e) { logger.error("Another exception was expected, but got {} instead: {}", e.getClass().getName(), e.getMessage()); Assert.fail("Another exception was expected, but got " + e.getClass().getName() + " instead: " + e.getMessage()); } }
@Test public void badInputZipStrFile() throws Exception { File destFile = new File(destDir, "badInputStrFile.zip"); try { try { ZipUtil.zip((String[]) null, destFile, true, 0); logger.error("Zip should fail when input String array is null"); Assert.fail("Zip should fail when input String array is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input File array (String, File): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), null, nestedSrcFile.getCanonicalPath() }, destFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input filename is null"); Assert.fail("Zip should fail when any input filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input filename (String, File): OK"); } try { ZipUtil.zip( new String[] { srcFile.getCanonicalPath(), dummieFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, destFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input filename does not exist"); Assert.fail("Zip should fail when any input filename does not exist"); } catch (FileNotFoundException e) { logger.debug("Detecting non-existing input filename (String, File): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, (File) null, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination File is null"); Assert.fail("Zip should fail when destination File is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination File (String, File): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, sampleZip, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination file already exists"); Assert.fail("Zip should fail when destination file already exists"); } catch (IllegalArgumentException e) { logger.debug("Detecting existing destination File (String, File): OK"); } try { ZipUtil.zip(new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath() }, dummieFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when the destination File does not represent a zip file"); Assert.fail("Zip should fail when the destination File does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination File not representing a valid zip file (String, File): OK"); } } catch (Exception e) { logger.error("Another exception was expected, but got {} instead: {}", e.getClass().getName(), e.getMessage()); Assert.fail("Another exception was expected, but got " + e.getClass().getName() + " instead: " + e.getMessage()); } }
@Test public void badInputZipFileStr() throws Exception { File destFile = new File(destDir.getCanonicalPath(), "badInputFileStr.zip"); try { try { ZipUtil.zip((File[]) null, destFile.getCanonicalPath(), true, ZipUtil.DEFAULT_COMPRESSION); logger.error("Zip should fail when input File array is null"); Assert.fail("Zip should fail when input File array is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input File array (File, String): OK"); } try { ZipUtil.zip(new File[] { srcFile, null, nestedSrcFile }, destFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input file is null"); Assert.fail("Zip should fail when any input file is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input filename (File, String): OK"); } try { ZipUtil.zip(new File[] { srcFile, dummieFile, nestedSrcFile }, destFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input file does not exist"); Assert.fail("Zip should fail when any input file does not exist"); } catch (FileNotFoundException e) { logger.debug("Detecting non-existing input filename (File, String): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, (String) null, true, ZipUtil.DEFAULT_COMPRESSION); logger.error("Zip should fail when destination filename is null"); Assert.fail("Zip should fail when destination filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination filename (File, String): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, "", true, ZipUtil.DEFAULT_COMPRESSION); logger.error("Zip should fail when destination filename is empty"); Assert.fail("Zip should fail when destination filename is empty"); } catch (IllegalArgumentException e) { logger.debug("Detecting empty destination filename (File, String): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, sampleZip.getCanonicalPath(), true, ZipUtil.DEFAULT_COMPRESSION); logger.error("Zip should fail when destination filename already exists"); Assert.fail("Zip should fail when destination filename already exists"); } catch (IllegalArgumentException e) { logger.debug("Detecting existing destination File (File, String): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, dummieFile.getCanonicalPath(), true, ZipUtil.DEFAULT_COMPRESSION); logger.error("Zip should fail when the destination filename does not represent a zip file"); Assert.fail("Zip should fail when the destination filename does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination filename not representing a valid zip file (File, String): OK"); } } catch (Exception e) { logger.error("Another exception was expected, but got {} instead: {}", e.getClass().getName(), e.getMessage()); Assert.fail("Another exception was expected, but got " + e.getClass().getName() + " instead: " + e.getMessage()); } }
@Test public void badInputZipFileFile() throws Exception { File destFile = new File(destDir, "badInputFileFile.zip"); try { try { ZipUtil.zip((File[]) null, destFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when input File array is null"); Assert.fail("Zip should fail when input File array is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input File array (File, File): OK"); } try { ZipUtil.zip(new File[] { srcFile, null, nestedSrcFile }, destFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input file is null"); Assert.fail("Zip should fail when any input file is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input filename (File, File): OK"); } try { ZipUtil.zip(new File[] { srcFile, dummieFile, nestedSrcFile }, destFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when any input file does not exist"); Assert.fail("Zip should fail when any input file does not exist"); } catch (FileNotFoundException e) { logger.debug("Detecting non-existing input filename (File, File): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, (File) null, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination File is null"); Assert.fail("Zip should fail when destination File is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination File (File, File): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, sampleZip, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when destination File already exists"); Assert.fail("Zip should fail when destination File already exists"); } catch (IllegalArgumentException e) { logger.debug("Detecting existing destination File (File, File): OK"); } try { ZipUtil.zip(new File[] { srcFile, nestedSrcFile }, dummieFile, true, ZipUtil.NO_COMPRESSION); logger.error("Zip should fail when the destination File does not represent a zip file"); Assert.fail("Zip should fail when the destination File does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination File not representing a valid zip file (File, File): OK"); } } catch (Exception e) { logger.error("Another exception was expected, but got {} instead: {}", e.getClass().getName(), e.getMessage()); Assert.fail("Another exception was expected, but got " + e.getClass().getName() + " instead: " + e.getMessage()); } }
@Test public void zipNoRecStrStr() throws Exception { File destFile = new File(destDir, "noRecStrStr.zip"); Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcFileName); File test = ZipUtil.zip( new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath(), nestedSrcDir.getCanonicalPath() }, destFile.getCanonicalPath(), false, ZipUtil.NO_COMPRESSION); Assert.assertTrue(test.exists()); ZipFile zip = new ZipFile(test); Assert.assertEquals(2, zip.size()); Enumeration<? extends ZipEntry> entries = zip.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Assert.assertTrue(names.contains(entry.getName())); } } catch (AssertionError ae) { zip.close(); throw ae; } zip.close(); }
@Test public void zipNoRecStrFile() throws Exception { File destFile = new File(destDir, "noRecStrFile.zip"); Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcFileName); File test = ZipUtil.zip( new String[] { srcFile.getCanonicalPath(), nestedSrcFile.getCanonicalPath(), nestedSrcDir.getCanonicalPath() }, destFile, false, ZipUtil.NO_COMPRESSION); Assert.assertTrue(test.exists()); ZipFile zip = new ZipFile(test); Assert.assertEquals(2, zip.size()); Enumeration<? extends ZipEntry> entries = zip.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Assert.assertTrue(names.contains(entry.getName())); } } catch (AssertionError ae) { zip.close(); throw ae; } zip.close(); }
@Test public void zipNoRecFileStr() throws Exception { File destFile = new File(destDir, "noRecFileStr.zip"); Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcFileName); File test = ZipUtil.zip(new File[] { srcFile, nestedSrcFile, nestedSrcDir }, destFile.getCanonicalPath(), false, ZipUtil.NO_COMPRESSION); Assert.assertTrue(test.exists()); ZipFile zip = new ZipFile(test); Assert.assertEquals(2, zip.size()); Enumeration<? extends ZipEntry> entries = zip.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Assert.assertTrue(names.contains(entry.getName())); } } catch (AssertionError ae) { zip.close(); throw ae; } zip.close(); }
@Test public void zipNoRecFileFile() throws Exception { File destFile = new File(destDir, "noRecFileFile.zip"); Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcFileName); File test = ZipUtil.zip(new File[] { srcFile, nestedSrcFile, nestedSrcDir }, destFile, false, ZipUtil.NO_COMPRESSION); Assert.assertTrue(test.exists()); ZipFile zip = new ZipFile(test); Assert.assertEquals(2, zip.size()); Enumeration<? extends ZipEntry> entries = zip.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Assert.assertTrue(names.contains(entry.getName())); } } catch (AssertionError ae) { zip.close(); throw ae; } zip.close(); }
@Test public void zipRecStrStr() throws Exception { File destFile = new File(destDir, "recStrStr.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.getCanonicalPath(), 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(); }
@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(); }
@Test public void zipRecFileStr() throws Exception { String destFilePath = destDir.getCanonicalPath() + File.separator + "recFileStr.zip"; Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcDirName + File.separator); names.add(nestedSrcDirName + File.separator + nestedSrcFileName); File test = ZipUtil.zip(srcDir.listFiles(), destFilePath, 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(); }
@Test public void zipRecFileFile() throws Exception { File destFile = new File(destDir, "recFileFile.zip"); Vector<String> names = new Vector<String>(); names.add(srcFileName); names.add(nestedSrcDirName + File.separator); names.add(nestedSrcDirName + File.separator + nestedSrcFileName); File test = ZipUtil.zip(srcDir.listFiles(), 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(); }