List of usage examples for org.apache.commons.compress.archivers.zip ZipFile ZipFile
public ZipFile(String name) throws IOException
From source file:org.overlord.commons.dev.server.util.ArchiveUtils.java
/** * Unpacks the given archive file into the output directory. * @param archiveFile an archive file/*from ww w .ja v a 2 s. c o m*/ * @param toDir where to unpack the archive to * @throws IOException */ public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(archiveFile); Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries(); while (zipEntries.hasMoreElements()) { ZipArchiveEntry entry = zipEntries.nextElement(); String entryName = entry.getName(); File outFile = new File(toDir, entryName); if (!outFile.getParentFile().exists()) { if (!outFile.getParentFile().mkdirs()) { throw new IOException( "Failed to create parent directory: " + outFile.getParentFile().getCanonicalPath()); //$NON-NLS-1$ } } if (entry.isDirectory()) { if (!outFile.exists() && !outFile.mkdir()) { throw new IOException("Failed to create directory: " + outFile.getCanonicalPath()); //$NON-NLS-1$ } else if (outFile.exists() && !outFile.isDirectory()) { throw new IOException("Failed to create directory (already exists but is a file): " //$NON-NLS-1$ + outFile.getCanonicalPath()); } } else { InputStream zipStream = null; OutputStream outFileStream = null; zipStream = zipFile.getInputStream(entry); outFileStream = new FileOutputStream(outFile); try { IOUtils.copy(zipStream, outFileStream); } finally { IOUtils.closeQuietly(zipStream); IOUtils.closeQuietly(outFileStream); } } } } finally { ZipFile.closeQuietly(zipFile); } }
From source file:org.overlord.sramp.atom.archive.ArchiveUtils.java
/** * Unpacks the given archive file into the output directory. * @param archiveFile an archive file/*from w w w . j a va2s. c o m*/ * @param toDir where to unpack the archive to * @throws IOException */ public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(archiveFile); Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntriesInPhysicalOrder(); while (zipEntries.hasMoreElements()) { ZipArchiveEntry entry = zipEntries.nextElement(); String entryName = entry.getName(); File outFile = new File(toDir, entryName); if (!outFile.getParentFile().exists()) { if (!outFile.getParentFile().mkdirs()) { throw new IOException(Messages.i18n.format("FAILED_TO_CREATE_PARENT_DIR", //$NON-NLS-1$ outFile.getParentFile().getCanonicalPath())); } } if (entry.isDirectory()) { if (!outFile.mkdir()) { throw new IOException( Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath())); //$NON-NLS-1$ } } else { InputStream zipStream = null; OutputStream outFileStream = null; zipStream = zipFile.getInputStream(entry); outFileStream = new FileOutputStream(outFile); try { IOUtils.copy(zipStream, outFileStream); } finally { IOUtils.closeQuietly(zipStream); IOUtils.closeQuietly(outFileStream); } } } } finally { ZipFile.closeQuietly(zipFile); } }
From source file:org.rauschig.jarchivelib.ZipFileArchiver.java
@Override protected ArchiveInputStream createArchiveInputStream(File archive) throws IOException { return new ZipFileArchiveInputStream(new ZipFile(archive)); }
From source file:org.sead.nds.repository.BagGenerator.java
public void validateBag(String bagId) { log.info("Validating Bag"); ZipFile zf = null;//from w ww .j a v a 2 s .c o m InputStream is = null; try { zf = new ZipFile(getBagFile(bagId)); ZipArchiveEntry entry = zf.getEntry(getValidName(bagId) + "/manifest-sha1.txt"); if (entry != null) { log.info("SHA1 hashes used"); hashtype = "SHA1 Hash"; is = zf.getInputStream(entry); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while (line != null) { log.debug("Hash entry: " + line); int breakIndex = line.indexOf(' '); String hash = line.substring(0, breakIndex); String path = line.substring(breakIndex + 1); log.debug("Adding: " + path + " with hash: " + hash); sha1Map.put(path, hash); line = br.readLine(); } IOUtils.closeQuietly(is); } else { entry = zf.getEntry(getValidName(bagId) + "/manifest-sha512.txt"); if (entry != null) { log.info("SHA512 hashes used"); hashtype = "SHA512 Hash"; is = zf.getInputStream(entry); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while (line != null) { int breakIndex = line.indexOf(' '); String hash = line.substring(0, breakIndex); String path = line.substring(breakIndex + 1); sha1Map.put(path, hash); line = br.readLine(); } IOUtils.closeQuietly(is); } } log.info("HashMap Map contains: " + sha1Map.size() + " etries"); checkFiles(sha1Map, zf); } catch (IOException io) { log.error("Could not validate Hashes", io); } catch (Exception e) { log.error("Could not validate Hashes", e); } return; }
From source file:org.sead.nds.repository.BagGenerator.java
private void validateBagFile(File bagFile) throws IOException { // Run a confirmation test - should verify all files and hashes ZipFile zf = new ZipFile(bagFile); // Check files calculates the hashes and file sizes and reports on // whether hashes are correct // The file sizes are added to totalDataSize which is compared with the // stats sent in the request checkFiles(sha1Map, zf);/* w w w .j a v a2 s .c o m*/ log.info("Data Count: " + dataCount); log.info("Data Size: " + totalDataSize); // Check stats if (pubRequest.getJSONObject("Aggregation Statistics").getLong("Number of Datasets") != dataCount) { log.warn("Request contains incorrect data count: should be: " + dataCount); } // Total size is calced during checkFiles if (pubRequest.getJSONObject("Aggregation Statistics").getLong("Total Size") != totalDataSize) { log.warn("Request contains incorrect Total Size: should be: " + totalDataSize); } zf.close(); }
From source file:org.silverpeas.core.util.ZipUtil.java
/** * Indicates the number of files (not directories) inside the archive. * * @param archive the archive whose content is analyzed. * @return the number of files (not directories) inside the archive. *///from www . j a va2 s . c om public static int getNbFiles(File archive) { int nbFiles = 0; try (ZipFile zipFile = new ZipFile(archive)) { @SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry ze = entries.nextElement(); if (!ze.isDirectory()) { nbFiles++; } } } catch (IOException ioe) { SilverLogger.getLogger(ZipUtil.class).error("Error while counting file in archive " + archive.getPath(), ioe); } return nbFiles; }
From source file:org.silverpeas.core.util.ZipUtilTest.java
/** * Test of compressStreamToZip method, of class ZipManager. * * @throws Exception//from w w w.ja v a 2 s .c om */ @Test public void testCompressStreamToZip() throws Exception { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("FrenchScrum.odp"); String filePathNameToCreate = separatorChar + "dir1" + separatorChar + "dir2" + separatorChar + "FrenchScrum.odp"; File outfile = new File(tempDir, "testCompressStreamToZip.zip"); ZipUtil.compressStreamToZip(inputStream, filePathNameToCreate, outfile.getPath()); inputStream.close(); assertThat(outfile, is(notNullValue())); assertThat(outfile.exists(), is(true)); assertThat(outfile.isFile(), is(true)); int result = ZipUtil.getNbFiles(outfile); assertThat(result, is(1)); ZipFile zipFile = new ZipFile(outfile); assertThat(zipFile.getEntry("/dir1/dir2/FrenchScrum.odp"), is(notNullValue())); zipFile.close(); }
From source file:org.slc.sli.ingestion.landingzone.validation.ZipFileValidator.java
@Override public boolean isValid(File zipFile, AbstractMessageReport report, ReportStats reportStats, Source source, Map<String, Object> parameters) { boolean isValid = false; // we know more of our source LOG.info("Validating zipFile: {}", zipFile.getAbsolutePath()); ZipFile zf = null;//from ww w .j a v a2s.c o m try { zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> zes = zf.getEntries(); while (zes.hasMoreElements()) { ZipArchiveEntry ze = zes.nextElement(); LOG.debug(" ZipArchiveEntry: name: {}, size {}", ze.getName(), ze.getSize()); if (isDirectory(ze)) { report.error(reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0010, zipFile.getName()); return false; } if (ze.getName().endsWith(".ctl")) { isValid = true; } } // no manifest (.ctl file) found in the zip file if (!isValid) { report.error(reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0009, zipFile.getName()); } } catch (UnsupportedZipFeatureException ex) { report.error(ex, reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0022, zipFile.getName()); isValid = false; } catch (FileNotFoundException ex) { report.error(ex, reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0020, zipFile.getName()); isValid = false; } catch (IOException ex) { LOG.warn("Caught IO exception processing " + zipFile.getAbsolutePath()); report.error(ex, reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0021, zipFile.getName()); isValid = false; } finally { ZipFile.closeQuietly(zf); } return isValid; }
From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java
/** * Extracts content of the ZIP file to the target folder. * * @param zipFile ZIP archive/*from w ww . ja va 2 s . com*/ * @param targetDir Directory to extract files to * @param mkdirs Allow creating missing directories on the file paths * @throws IOException IO Exception * @throws FileNotFoundException */ public static void extract(File zipFile, File targetDir, boolean mkdirs) throws IOException { ZipFile zf = null; try { zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> zes = zf.getEntries(); while (zes.hasMoreElements()) { ZipArchiveEntry entry = zes.nextElement(); if (!entry.isDirectory()) { File targetFile = new File(targetDir, entry.getName()); if (mkdirs) { targetFile.getParentFile().mkdirs(); } InputStream is = null; try { is = zf.getInputStream(entry); copyInputStreamToFile(is, targetFile); } finally { IOUtils.closeQuietly(is); } } } } finally { ZipFile.closeQuietly(zf); } }
From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java
/** * Returns a stream for a file stored in the ZIP archive. * * @param zipFile ZIP archive//from w ww . j a v a 2 s . c om * @param fileName Name of the file to get stream for * @return Input Stream for the requested file entry * @throws IOException IO Exception * @throws FileNotFoundException */ public static InputStream getInputStreamForFile(File zipFile, String fileName) throws IOException { ZipFile zf = null; InputStream fileInputStream = null; try { zf = new ZipFile(zipFile); ZipArchiveEntry entry = zf.getEntry(fileName); if (entry == null || entry.isDirectory()) { String msg = MessageFormat.format("No file entry is found for {0} withing the {0} archive", fileName, zipFile); throw new FileNotFoundException(msg); } final ZipFile fzf = zf; fileInputStream = new BufferedInputStream(zf.getInputStream(entry)) { @Override public void close() throws IOException { super.close(); ZipFile.closeQuietly(fzf); } }; } finally { if (fileInputStream == null) { ZipFile.closeQuietly(zf); } } return fileInputStream; }