List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getEntries
public Enumeration getEntries()
From source file:org.everit.osgi.dev.maven.util.FileManager.java
public final void unpackZipFile(final File file, final File destinationDirectory) throws IOException { destinationDirectory.mkdirs();//w w w . j a v a2s. c om ZipFile zipFile = new ZipFile(file); try { Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String name = entry.getName(); File destFile = new File(destinationDirectory, name); if (entry.isDirectory()) { destFile.mkdirs(); } else { File parentFolder = destFile.getParentFile(); parentFolder.mkdirs(); InputStream inputStream = zipFile.getInputStream(entry); overCopyFile(inputStream, destFile); FileManager.setPermissionsOnFile(destFile, entry); } } } finally { zipFile.close(); } }
From source file:org.fabrician.maven.plugins.CompressUtils.java
public static void copyZipToArchiveOutputStream(File zipSrc, FilenamePatternFilter filter, ArchiveOutputStream out, String alternateBaseDir) throws IOException { ZipFile zip = new ZipFile(zipSrc); for (Enumeration<ZipArchiveEntry> zipEnum = zip.getEntries(); zipEnum.hasMoreElements();) { ZipArchiveEntry source = zipEnum.nextElement(); if (filter != null && !filter.accept(source.getName())) { System.out.println("Excluding " + source.getName()); continue; }//from ww w.jav a 2 s . co m InputStream in = null; try { in = zip.getInputStream(source); out.putArchiveEntry(createArchiveEntry(source, out, alternateBaseDir)); IOUtils.copy(in, out); out.closeArchiveEntry(); } finally { close(in); } } }
From source file:org.fabrician.maven.plugins.CompressUtils.java
public static boolean entryExistsInZip(File zipFile, String entryName) throws IOException { ZipFile zip = new ZipFile(zipFile); for (Enumeration<ZipArchiveEntry> zipEnum = zip.getEntries(); zipEnum.hasMoreElements();) { ZipArchiveEntry source = zipEnum.nextElement(); if (source.getName().equals(entryName)) { return true; }/*from w w w . ja va 2 s. c o m*/ } return false; }
From source file:org.interreg.docexplore.util.ZipUtils.java
public static void unzip(File zipfile, File directory, float[] progress, float progressOffset, float progressAmount, boolean overwrite) throws IOException { org.apache.commons.compress.archivers.zip.ZipFile zfile = new org.apache.commons.compress.archivers.zip.ZipFile( zipfile);//from ww w .j a va 2s. c o m int nEntries = 0; Enumeration<ZipArchiveEntry> entries = zfile.getEntries(); while (entries.hasMoreElements()) if (!entries.nextElement().isDirectory()) nEntries++; int cnt = 0; entries = zfile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); File file = new File(directory, entry.getName()); if (entry.isDirectory()) file.mkdirs(); else { if (!file.exists() || overwrite) { file.getParentFile().mkdirs(); InputStream in = zfile.getInputStream(entry); try { copy(in, file); } finally { in.close(); } } cnt++; if (progress != null) progress[0] = progressOffset + cnt * progressAmount / nEntries; } } zfile.close(); }
From source file:org.itstechupnorth.walrus.text.GutenburgParser.java
private Reader open() throws IOException, ZipException, FileNotFoundException, UnsupportedEncodingException { final Reader reader; final InputStream input; if (FilenameUtils.isExtension(file.getName(), ZIP)) { try {/*from w w w .j a v a 2 s.c o m*/ final ZipFile zip = new ZipFile(file); input = zip.getInputStream((ZipArchiveEntry) zip.getEntries().nextElement()); } catch (IllegalArgumentException e) { throw new RuntimeWarningException(e.getMessage() + "(" + file + ")", e); } } else { input = new FileInputStream(file); } reader = new InputStreamReader(new BufferedInputStream(input), "UTF-8"); return reader; }
From source file:org.itstechupnorth.walrus.zip.ArchiveManagerTest.java
public void testOneRound() throws Exception { final File file = file("round.zip"); ArchiveSaver manager = new ArchiveSaver(file); manager.open();/*from ww w . j a v a2 s .c o m*/ ArticleBuffer buffer = new ArticleBuffer(); final String title = "Hello"; buffer.title(title.toCharArray(), 0, 5); buffer.text("World".toCharArray(), 0, 5); manager.save(buffer); manager.close(); ZipFile zip = new ZipFile(file); assertEquals("UTF8", zip.getEncoding()); @SuppressWarnings("rawtypes") final Enumeration entries = zip.getEntries(); assertEquals(true, entries.hasMoreElements()); final ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement(); assertEquals(title, entry.getComment()); assertEquals(false, entries.hasMoreElements()); }
From source file:org.jboss.as.forge.util.Files.java
public static boolean extractAppServer(final String zipPath, final File target, final boolean overwrite) throws IOException { if (target.exists() && !overwrite) { throw new IllegalStateException(Messages.INSTANCE.getMessage("files.not.empty.directory")); }// w w w. ja v a 2s. c o m // Create a temporary directory final File tmpDir = new File(getTempDirectory(), "jboss-as-" + zipPath.hashCode()); if (tmpDir.exists()) { deleteRecursively(tmpDir); } try { final byte buff[] = new byte[1024]; ZipFile file = null; try { file = new ZipFile(zipPath); final Enumeration<ZipArchiveEntry> entries = file.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry entry = entries.nextElement(); // Create the extraction target final File extractTarget = new File(tmpDir, entry.getName()); if (entry.isDirectory()) { extractTarget.mkdirs(); } else { final File parent = new File(extractTarget.getParent()); parent.mkdirs(); final BufferedInputStream in = new BufferedInputStream(file.getInputStream(entry)); try { final BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(extractTarget)); try { int read; while ((read = in.read(buff)) != -1) { out.write(buff, 0, read); } } finally { Streams.safeClose(out); } } finally { Streams.safeClose(in); } // Set the file permissions if (entry.getUnixMode() > 0) { setPermissions(extractTarget, FilePermissions.of(entry.getUnixMode())); } } } } catch (IOException e) { throw new IOException(Messages.INSTANCE.getMessage("files.extraction.error", file), e); } finally { ZipFile.closeQuietly(file); // Streams.safeClose(file); } // If the target exists, remove then rename if (target.exists()) { deleteRecursively(target); } // First child should be a directory and there should only be one child final File[] children = tmpDir.listFiles(); if (children != null && children.length == 1) { return moveDirectory(children[0], target); } return moveDirectory(tmpDir, target); } finally { deleteRecursively(tmpDir); } }
From source file:org.jboss.datavirt.commons.dev.server.util.ArchiveUtils.java
/** * Unpacks the given archive file into the output directory. * @param archiveFile an archive file//from w w w . j a 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()); } } if (entry.isDirectory()) { if (!outFile.mkdir()) { throw new IOException("Failed to create directory: " + 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.kuali.ole.docstore.model.bagit.BagExtractor.java
/** * @param bagFile// ww w. jav a2 s . com * @return */ public static boolean verifyBag(File bagFile) throws IOException, FormatHelper.UnknownFormatException { SimpleResult result = null; Bag bag; LOG.info("synchronized verifyBagggggg " + bagFile.getAbsolutePath()); synchronized (bf) { LOG.info("synchronizeddddd"); System.out.println("yes"); // try { Bag.Format format = FormatHelper.getFormat(bagFile); LOG.info("format " + format); //gov.loc.repository.bagit.filesystem.FileSystem fileFileSystem = new FileFileSystem(bagFile); gov.loc.repository.bagit.filesystem.FileSystem zipFileSystem = new ZipFileSystem(bagFile); //LOG.info("FileFileSystem "+fileFileSystem); Iterator ite = zipFileSystem.getRoot().listChildren().iterator(); while (ite.hasNext()) { FileSystemNode fs = (FileSystemNode) ite.next(); ZipFile zipFile = new ZipFile(bagFile); Enumeration<ZipArchiveEntry> entryEnum = zipFile.getEntries(); while (entryEnum.hasMoreElements()) { ZipArchiveEntry entry = entryEnum.nextElement(); String entryFilepath = entry.getName(); LOG.info("entryFilepath " + entryFilepath); if (entryFilepath.endsWith("/")) { LOG.info("in if entryFilepath.endsWith"); entryFilepath = entryFilepath.substring(0, entryFilepath.length() - 1); } else { //entryFilepath = entryFilepath.substring(0, entryFilepath.length()-1); LOG.info("in else entryFilepath.endsWith entryFilepath " + entryFilepath); } File parentFile = new File(entryFilepath).getParentFile(); List<String> parentPaths = new ArrayList<String>(); while (parentFile != null) { parentPaths.add((parentFile.getPath())); parentFile = parentFile.getParentFile(); } LOG.info("parentPaths " + parentPaths); } } //} // catch (FormatHelper.UnknownFormatException e) { // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. // } bag = bf.createBag(bagFile); LOG.info("synchronized AFTER createBag"); } result = bag.verifyValid(); LOG.info("result " + result.getMessages()); LOG.info("isSuccess " + result.isSuccess()); return result.isSuccess(); }
From source file:org.moe.cli.utils.ArchiveUtils.java
public static void unzipArchive(ZipFile zipFile, File destination) throws IOException { Enumeration<ZipArchiveEntry> e = zipFile.getEntries(); InputStream is = null;/*from ww w.ja v a 2s. c o m*/ FileOutputStream fStream = null; try { while (e.hasMoreElements()) { ZipArchiveEntry entry = e.nextElement(); if (entry.isDirectory()) { String dest = entry.getName(); File destFolder = new File(destination, dest); if (!destFolder.exists()) { destFolder.mkdirs(); } } else { if (!entry.isUnixSymlink()) { String dest = entry.getName(); File destFile = new File(destination, dest); is = zipFile.getInputStream(entry); // get the input stream fStream = new FileOutputStream(destFile); copyFiles(is, fStream); } else { String link = zipFile.getUnixSymlink(entry); String entryName = entry.getName(); int parentIdx = entryName.lastIndexOf("/"); String newLink = entryName.substring(0, parentIdx) + "/" + link; File destFile = new File(destination, newLink); File linkFile = new File(destination, entryName); Files.createSymbolicLink(Paths.get(linkFile.getPath()), Paths.get(destFile.getPath())); } } } } finally { if (is != null) { is.close(); } if (fStream != null) { fStream.close(); } } }