List of usage examples for java.util.zip ZipFile entries
public Enumeration<? extends ZipEntry> entries()
From source file:Zip.java
/** * Reads a Zip file, iterating through each entry and dumping the contents * to the console.// w ww .j a v a 2 s.co m */ public static void readZipFile(String fileName) { ZipFile zipFile = null; try { // ZipFile offers an Enumeration of all the files in the Zip file zipFile = new ZipFile(fileName); for (Enumeration e = zipFile.entries(); e.hasMoreElements();) { ZipEntry zipEntry = (ZipEntry) e.nextElement(); System.out.println(zipEntry.getName() + " contains:"); // use BufferedReader to get one line at a time BufferedReader zipReader = new BufferedReader( new InputStreamReader(zipFile.getInputStream(zipEntry))); while (zipReader.ready()) { System.out.println(zipReader.readLine()); } zipReader.close(); } } catch (IOException ioe) { System.out.println("An IOException occurred: " + ioe.getMessage()); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException ioe) { } } } }
From source file:com.google.gdt.eclipse.designer.webkit.WebKitSupportWin32.java
private static void extract(ZipFile zipFile) throws Exception { // remove any possibly corrupted contents FileUtils.deleteQuietly(WEBKIT_DIR); WEBKIT_DIR.mkdirs();/* w ww . j a v a2 s . c o m*/ Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { new File(WEBKIT_DIR, entry.getName()).mkdirs(); continue; } InputStream inputStream = zipFile.getInputStream(entry); File outputFile = new File(WEBKIT_DIR, entry.getName()); FileOutputStream outputStream = new FileOutputStream(outputFile); IOUtils.copy(inputStream, outputStream); IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } }
From source file:com.aurel.track.dbase.InitReportTemplateBL.java
/** * Unzip a ZIP file into a directory/*from w w w . j av a 2 s. c om*/ * @param zipFile * @param dir */ private static void unzipFileIntoDirectory(ZipFile zipFile, File dir) { Enumeration files = zipFile.entries(); File f = null; FileOutputStream fos = null; while (files.hasMoreElements()) { try { ZipEntry entry = (ZipEntry) files.nextElement(); InputStream eis = zipFile.getInputStream(entry); byte[] buffer = new byte[1024]; int bytesRead = 0; f = new File(dir.getAbsolutePath() + File.separator + entry.getName()); if (entry.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); f.createNewFile(); } fos = new FileOutputStream(f); while ((bytesRead = eis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } } catch (IOException e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); continue; } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { // ignore } } } } }
From source file:net.technicpack.utilslib.ZipUtils.java
public static void unzipFile(File zip, File output, IZipFileFilter fileFilter, DownloadListener listener) throws IOException, InterruptedException { if (!zip.exists()) { Utils.getLogger().log(Level.SEVERE, "File to unzip does not exist: " + zip.getAbsolutePath()); return;//from www .j av a2s .c o m } if (!output.exists()) { output.mkdirs(); } ZipFile zipFile = new ZipFile(zip); int size = zipFile.size() + 1; int progress = 1; try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { if (Thread.interrupted()) throw new InterruptedException(); ZipEntry entry = null; try { entry = entries.nextElement(); } catch (IllegalArgumentException ex) { //We must catch & rethrow as a zip exception because some crappy code in the zip lib will //throw illegal argument exceptions for malformed zips. throw new ZipException("IllegalArgumentException while parsing next element."); } if (!entry.getName().contains("../") && (fileFilter == null || fileFilter.shouldExtract(entry.getName()))) { File outputFile = new File(output, entry.getName()); if (outputFile.getParentFile() != null) { outputFile.getParentFile().mkdirs(); } if (!entry.isDirectory()) { unzipEntry(zipFile, entry, outputFile); } } if (listener != null) { float totalProgress = (float) progress / (float) size; listener.stateChanged("Extracting " + entry.getName() + "...", totalProgress * 100.0f); } progress++; } } finally { zipFile.close(); } }
From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java
/** * Extracts the given apk into the given destination directory * // w ww . j a v a 2s . co m * @param archive * - the file to extract * @param dest * - the destination directory * @throws IOException */ public static void extractApk(File archive, File dest) throws IOException { @SuppressWarnings("resource") // Closing it later results in an error ZipFile zipFile = new ZipFile(archive); Enumeration<? extends ZipEntry> entries = zipFile.entries(); BufferedOutputStream bos = null; BufferedInputStream bis = null; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryFileName = entry.getName(); byte[] buffer = new byte[16384]; int len; File dir = buildDirectoryHierarchyFor(entryFileName, dest);// destDir if (!dir.exists()) { dir.mkdirs(); } if (!entry.isDirectory()) { if (entry.getSize() == 0) { LOGGER.warn("Found ZipEntry \'" + entry.getName() + "\' with size 0 in " + archive.getName() + ". Looks corrupted."); continue; } try { bos = new BufferedOutputStream(new FileOutputStream(new File(dest, entryFileName)));// destDir,... bis = new BufferedInputStream(zipFile.getInputStream(entry)); while ((len = bis.read(buffer)) > 0) { bos.write(buffer, 0, len); } bos.flush(); } catch (IOException ioe) { LOGGER.warn("Failed to extract entry \'" + entry.getName() + "\' from archive. Results for " + archive.getName() + " may not be accurate"); } finally { if (bos != null) bos.close(); if (bis != null) bis.close(); // if (zipFile != null) zipFile.close(); } } } }
From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java
/** * Decompress.// w w w.j a va 2 s. co m * * @param prefix * the prefix * @param inputFile * the input file * @param tempFile * the temp file * @return the file * @throws IOException * Signals that an I/O exception has occurred. */ public static File decompress(final String prefix, final File inputFile, final File tempFile) throws IOException { final File tmpDestDir = createTodayPrefixedDirectory(prefix, new File(tempFile.getParent())); ZipFile zipFile = new ZipFile(inputFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); if (entry.isDirectory()) { // Assume directories are stored parents first then // children. (new File(tmpDestDir, entry.getName())).mkdir(); continue; } File newFile = new File(tmpDestDir, entry.getName()); FileOutputStream fos = new FileOutputStream(newFile); try { byte[] buf = new byte[1024]; int len; while ((len = stream.read(buf)) >= 0) { saveCompressedStream(buf, fos, len); } } catch (IOException e) { zipFile.close(); IOException ioe = new IOException("Not valid ZIP archive file type."); ioe.initCause(e); throw ioe; } finally { fos.flush(); fos.close(); stream.close(); } } zipFile.close(); if ((tmpDestDir.listFiles().length == 1) && (tmpDestDir.listFiles()[0].isDirectory())) { return getShpFile(tmpDestDir.listFiles()[0]); } // File[] files = tmpDestDir.listFiles(new FilenameFilter() { // // public boolean accept(File dir, String name) { // return FilenameUtils.getExtension(name).equalsIgnoreCase("shp"); // } // }); // // return files.length > 0 ? files[0] : null; return getShpFile(tmpDestDir); }
From source file:com.idocbox.common.file.ZipExtracter.java
/** * extract given entry in zip file to destination directory. * if given entry is a directory, extract all folder and files under it to * destination directory.//from w w w . j av a2s .c o m * @param zipFileName name of zip file. * @param entry it can be a directory or a file.(if it is end with "/",it * will be treated as a directory.) . For example, * conf/ represents subdirectory in zip file. * Use "/" to represent root of zip file. * * @param desDir destination directory. * @param startDirLevel the level to start create directory. * Its value is 1,2,... * @throws IOException * @throws ZipException */ public static void extract(final String zipFileName, final String entry, final String desDir, final int... startDirLevel) throws ZipException, IOException { File f = new File(zipFileName); if (f.exists()) { //check destination directory. File desf = new File(desDir); if (!desf.exists()) { desf.mkdirs(); } boolean toExtractDir = false; if (entry.endsWith("/")) { toExtractDir = true; } //create zip file object. ZipFile zf = new ZipFile(f); if (toExtractDir) { Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zf.entries(); ZipEntry ent = null; while (entries.hasMoreElements()) { ent = entries.nextElement(); if (isUnderDir(ent, entry)) { //the entry is under directory //which should to be extracted. //extract the entry. extract(zf, ent, desDir, startDirLevel); } else { //nothing to do. } } } else {//extract given zip entry file to destination directory. ZipEntry zipEntry = zf.getEntry(entry); if (null != zipEntry) { extract(zf, zipEntry, desDir, startDirLevel); } } if (null != zf) { //close zip file. zf.close(); } } }
From source file:net.sf.jabref.importer.ZipFileChooser.java
/** * Entries that can be selected with this dialog. * * @param zipFile Zip-File//w w w. j a v a2 s . com * @return entries that can be selected */ private static ZipEntry[] getSelectableZipEntries(ZipFile zipFile) { List<ZipEntry> entries = new ArrayList<>(); Enumeration<? extends ZipEntry> e = zipFile.entries(); for (ZipEntry entry : Collections.list(e)) { if (!entry.isDirectory() && entry.getName().endsWith(".class")) { entries.add(entry); } } return entries.toArray(new ZipEntry[entries.size()]); }
From source file:org.eclipse.mylyn.internal.context.core.InteractionContextExternalizer.java
static String getFirstContextHandle(File sourceFile) throws CoreException { try {//www.ja va 2s . c o m ZipFile zipFile = new ZipFile(sourceFile); try { for (Enumeration<?> e = zipFile.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); String name = entry.getName(); if (name.endsWith(InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD)) { try { String decodedName = URLDecoder.decode(name, InteractionContextManager.CONTEXT_FILENAME_ENCODING); if (decodedName.length() > InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD .length()) { return decodedName.substring(0, decodedName.length() - InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD.length()); } } catch (IllegalArgumentException ignored) { // not a valid context entry } } } return null; } finally { zipFile.close(); } } catch (IOException e) { throw new CoreException(new Status(IStatus.ERROR, ContextCorePlugin.ID_PLUGIN, "Could not get context handle from " + sourceFile, e)); //$NON-NLS-1$ } }
From source file:org.blockartistry.mod.Restructured.assets.ZipProcessor.java
private static void traverseZips(final File path, final Predicate<ZipEntry> test, final Predicate<Object[]> process) { for (final File file : getZipFiles(path)) { try {/* ww w .j a va 2s . c o m*/ final ZipFile zip = new ZipFile(file); final String prefix = StringUtils.removeEnd(file.getName(), ".zip").toLowerCase().replaceAll("[-.]", "_"); Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (test.apply(entry)) { final InputStream stream = zip.getInputStream(entry); final String name = StringUtils.removeEnd(entry.getName(), ".schematic"); process.apply(new Object[] { prefix, name, stream }); stream.close(); } } zip.close(); } catch (final Exception ex) { ex.printStackTrace(); } } }