ZipUtil { public static void unzip(java.io.File zipFile, java.io.File destination) throws IOException { boolean success; if (zipFile == null) { logger.error("The zip file cannot be null"); throw new IllegalArgumentException("The zip file must be set"); } if (!zipFile.exists()) { logger.error("The zip file does not exist: {}", zipFile.getCanonicalPath()); throw new FileNotFoundException("The zip file does not exist: " + zipFile.getCanonicalPath()); } if (destination == null) { logger.error("The destination file cannot be null"); throw new IllegalArgumentException("Destination file cannot be null"); } File f; try { f = new File(zipFile.getCanonicalFile()); } 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 (f.isArchive() && f.isDirectory()) { if (destination.exists()) { if (!destination.isDirectory()) { logger.error("Destination file must be a directory"); throw new IllegalArgumentException("Destination file must be a directory"); } } try { destination.mkdirs(); } catch (SecurityException e) { logger.error("Cannot create destination directory: {}", e.getMessage()); throw new IOException("Cannot create destination directory", e); } success = f.copyAllTo(destination); if (success) logger.debug("File {} unzipped successfully", zipFile.getCanonicalPath()); else { logger.warn("File {} was not correctly unzipped", zipFile.getCanonicalPath()); throw new IOException("File " + zipFile.getCanonicalPath() + " was not correctly unzipped"); } } else { logger.error("The input file is not a valid zip file"); throw new IllegalArgumentException("The input file is not a valid zip file"); } } catch (IOException e) { throw (e); } finally { umount(f); } } 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 badInputUnzipStrStr() throws Exception { File destFile = new File(destDir, "badInputStrStr"); try { try { ZipUtil.unzip((String) null, destFile.getCanonicalPath()); logger.error("Unzip should fail when input filename is null"); Assert.fail("Unzip should fail when input filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input filename (String, String): OK"); } try { ZipUtil.unzip("", destFile.getCanonicalPath()); logger.error("Unzip should fail when input filename is empty"); Assert.fail("Unzip should fail when input filename is empty"); } catch (IllegalArgumentException e) { logger.debug("Detecting empty input filename (String, String): OK"); } try { ZipUtil.unzip(sampleZip.getCanonicalPath(), (String) null); logger.error("Unzip should fail when destination filename is null"); Assert.fail("Unzip should fail when destination filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination filename (String, String): OK"); } try { ZipUtil.unzip(sampleZip.getCanonicalPath(), ""); logger.error("Unzip should fail when destination filename is empty"); Assert.fail("Unzip should fail when destination filename is empty"); } catch (IllegalArgumentException e) { logger.debug("Detecting empty destination filename (String, String): OK"); } try { ZipUtil.unzip(dummieFile.getCanonicalPath(), destFile.getCanonicalPath()); logger.error("Unzip should fail when the input filename doesn't exists"); Assert.fail("Unzip should fail when the input filename doesn't exists"); } catch (FileNotFoundException e) { logger.debug("Detecting existing input filename (String, String): OK"); } try { ZipUtil.unzip(srcFile.getCanonicalPath(), destFile.getCanonicalPath()); logger.error("Unzip should fail when the input filename does not represent a zip file"); Assert.fail("Unzip should fail when the input filename does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting input filename not representing a valid zip file (String, String): OK"); } try { ZipUtil.unzip(sampleZip.getCanonicalPath(), srcFile.getCanonicalPath()); logger.error("Unzip should fail when the destination filename does not represent a directory"); Assert.fail("Unzip should fail when the destination filename does not represent a directory"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination filename not representing a directory (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 badInputUnzipStrFile() throws Exception { File destFile = new File(destDir, "badInputStrFile"); try { try { ZipUtil.unzip((String) null, destFile); logger.error("Unzip should fail when input filename is null"); Assert.fail("Unzip should fail when input filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input filename (String, File): OK"); } try { ZipUtil.unzip("", destFile); logger.error("Unzip should fail when input filename is empty"); Assert.fail("Unzip should fail when input filename is empty"); } catch (IllegalArgumentException e) { logger.debug("Detecting empty input filename (String, File): OK"); } try { ZipUtil.unzip(dummieFile.getCanonicalPath(), destFile); logger.error("Unzip should fail when the input filename doesn't exists"); Assert.fail("Unzip should fail when the input filename doesn't exists"); } catch (FileNotFoundException e) { logger.debug("Detecting existing input filename (String, File): OK"); } try { ZipUtil.unzip(sampleZip.getCanonicalPath(), (File) null); logger.error("Unzip should fail when destination filename is null"); Assert.fail("Unzip should fail when destination filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination filename (String, File): OK"); } try { ZipUtil.unzip(srcFile.getCanonicalPath(), destFile); logger.error("Unzip should fail when the input filename does not represent a zip file"); Assert.fail("Unzip should fail when the input filename does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting input filename not representing a valid zip file (String, File): OK"); } try { ZipUtil.unzip(sampleZip.getCanonicalPath(), srcFile); logger.error("Unzip should fail when the destination File does not represent a directory"); Assert.fail("Unzip should fail when the destination File does not represent a directory"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination File not representing a directory (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 badInputUnzipFileStr() throws Exception { File destFile = new File(destDir, "badInputFileStr"); try { try { ZipUtil.unzip((File) null, destFile.getCanonicalPath()); logger.error("Unzip should fail when input File is null"); Assert.fail("Unzip should fail when input File is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input File (File, String): OK"); } try { ZipUtil.unzip(dummieFile, destFile.getCanonicalPath()); logger.error("Unzip should fail when input File doesn't exist"); Assert.fail("Unzip should fail when input File doesn't exist"); } catch (FileNotFoundException e) { logger.debug("Detecting non-existing input File (File, String): OK"); } try { ZipUtil.unzip(sampleZip, (String) null); logger.error("Unzip should fail when destination filename is null"); Assert.fail("Unzip should fail when destination filename is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination filename (File, String): OK"); } try { ZipUtil.unzip(sampleZip.getCanonicalPath(), ""); logger.error("Unzip should fail when destination filename is empty"); Assert.fail("Unzip should fail when destination filename is empty"); } catch (IllegalArgumentException e) { logger.debug("Detecting empty destination filename (File, String): OK"); } try { ZipUtil.unzip(srcFile, destFile.getCanonicalPath()); logger.error("Unzip should fail when the input File does not represent a zip file"); Assert.fail("Unzip should fail when the input File does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting input File not representing a valid zip file (File, String): OK"); } try { ZipUtil.unzip(sampleZip, srcFile.getCanonicalPath()); logger.error("Unzip should fail when the destination filename does not represent a directory"); Assert.fail("Unzip should fail when the destination filename does not represent a directory"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination filename not representing a directory (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 badInputUnzipFileFile() throws Exception { File destFile = new File(destDir, "badInputFileFile"); try { try { ZipUtil.unzip((File) null, destFile); logger.error("Unzip should fail when input File is null"); Assert.fail("Unzip should fail when input File is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null input File (File, File): OK"); } try { ZipUtil.unzip(dummieFile, destFile); logger.error("Unzip should fail when input File doesn't exist"); Assert.fail("Unzip should fail when input File doesn't exist"); } catch (FileNotFoundException e) { logger.debug("Detecting non-existing input File (File, File): OK"); } try { ZipUtil.unzip(sampleZip, (File) null); logger.error("Unzip should fail when destination File is null"); Assert.fail("Unzip should fail when destination File is null"); } catch (IllegalArgumentException e) { logger.debug("Detecting null destination File (File, File): OK"); } try { ZipUtil.unzip(srcFile, destFile); logger.error("Unzip should fail when the input File does not represent a zip file"); Assert.fail("Unzip should fail when the input File does not represent a zip file"); } catch (IllegalArgumentException e) { logger.debug("Detecting input File not representing a valid zip file (File, File): OK"); } try { ZipUtil.unzip(sampleZip, srcFile); logger.error("Unzip should fail when the destination File does not represent a directory"); Assert.fail("Unzip should fail when the destination File does not represent a directory"); } catch (IllegalArgumentException e) { logger.debug("Detecting destination File not representing a directory (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 testUnzip() throws Exception { ZipUtil.unzip(sampleZip, destDir); ZipFile test = new ZipFile(sampleZip); Enumeration<? extends ZipEntry> entries = test.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Assert.assertTrue(new File(destDir, entry.getName()).exists()); } } catch (AssertionError ae) { test.close(); throw ae; } test.close(); }