List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getEntries
public Enumeration getEntries()
From source file:de.nx42.maps4cim.util.Compression.java
/** * Reads the first file entry in a zip file and returns it's contents * as uncompressed byte-array/*from w ww. java 2s. co m*/ * @param zipFile the zip file to read from * @return the first file entry (uncompressed) * @throws IOException if there is an error accessing the zip file */ public static byte[] readFirstZipEntry(File zipFile) throws IOException { // open zip ZipFile zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> entries = zf.getEntries(); // read first entry to byte[] ZipArchiveEntry entry = entries.nextElement(); InputStream is = zf.getInputStream(entry); byte[] raw = ByteStreams.toByteArray(is); // close all streams and return byte[] is.close(); zf.close(); return raw; }
From source file:de.nx42.maps4cim.util.Compression.java
/** * Reads the first file entry in a zip file and writes it in uncompressed * form to the desired file.// w ww . ja va 2 s.co m * @param zipFile the zip file to read from * @param dest the file to write the first zip file entry to * @return same as destination * @throws IOException if there is an error accessing the zip file or the * destination file */ public static File readFirstZipEntry(File zipFile, File dest) throws IOException { // open zip and get first entry ZipFile zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> entries = zf.getEntries(); ZipArchiveEntry entry = entries.nextElement(); // write to file InputStream in = zf.getInputStream(entry); OutputStream out = new FileOutputStream(dest); ByteStreams.copy(in, out); // close all streams and return the new file in.close(); out.close(); zf.close(); return dest; }
From source file:com.android.tradefed.util.ZipUtil2.java
/** * Utility method to extract entire contents of zip file into given directory * * @param zipFile the {@link ZipFile} to extract * @param destDir the local dir to extract file to * @throws IOException if failed to extract file */// ww w. jav a 2 s . co m public static void extractZip(ZipFile zipFile, File destDir) throws IOException { Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); File childFile = new File(destDir, entry.getName()); childFile.getParentFile().mkdirs(); if (entry.isDirectory()) { childFile.mkdirs(); applyUnixModeIfNecessary(entry, childFile); continue; } else { FileUtil.writeToFile(zipFile.getInputStream(entry), childFile); applyUnixModeIfNecessary(entry, childFile); } } }
From source file:com.hw.util.CompressUtils.java
private static void unzipFolder(File uncompressFile, File descPathFile, boolean override) { ZipFile zipFile = null; try {/*w w w .j a v a 2 s . c o m*/ zipFile = new ZipFile(uncompressFile, "GBK"); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry zipEntry = entries.nextElement(); String name = zipEntry.getName(); name = name.replace("\\", "/"); File currentFile = new File(descPathFile, name); //? if (currentFile.isFile() && currentFile.exists() && !override) { continue; } if (name.endsWith("/")) { currentFile.mkdirs(); continue; } else { currentFile.getParentFile().mkdirs(); } FileOutputStream fos = null; try { fos = new FileOutputStream(currentFile); InputStream is = zipFile.getInputStream(zipEntry); IOUtils.copy(is, fos); } finally { IOUtils.closeQuietly(fos); } } } catch (IOException e) { throw new RuntimeException("", e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { } } } }
From source file:com.amazonaws.codepipeline.jenkinsplugin.ExtractionTools.java
private static void extractZipFile(final File destination, final ZipFile zipFile) throws IOException { final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry entry = entries.nextElement(); final File entryDestination = getDestinationFile(destination, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs();/*from w w w . jav a 2 s . co m*/ } else { entryDestination.getParentFile().mkdirs(); final InputStream in = zipFile.getInputStream(entry); try (final OutputStream out = new FileOutputStream(entryDestination)) { IOUtils.copy(in, out); IOUtils.closeQuietly(in); } } } }
From source file:com.daphne.es.maintain.editor.web.controller.utils.CompressUtils.java
@SuppressWarnings("unchecked") private static void unzipFolder(File uncompressFile, File descPathFile, boolean override) { ZipFile zipFile = null; try {//from w w w . j a va2s .c om zipFile = new ZipFile(uncompressFile, "GBK"); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry zipEntry = entries.nextElement(); String name = zipEntry.getName(); name = name.replace("\\", "/"); File currentFile = new File(descPathFile, name); //? if (currentFile.isFile() && currentFile.exists() && !override) { continue; } if (name.endsWith("/")) { currentFile.mkdirs(); continue; } else { currentFile.getParentFile().mkdirs(); } FileOutputStream fos = null; try { fos = new FileOutputStream(currentFile); InputStream is = zipFile.getInputStream(zipEntry); IOUtils.copy(is, fos); } finally { IOUtils.closeQuietly(fos); } } } catch (IOException e) { throw new RuntimeException("", e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { } } } }
From source file:com.mirth.connect.util.ArchiveUtils.java
/** * Extracts folders/files from a zip archive using zip-optimized code from commons-compress. */// w ww . j av a 2 s .c om private static void extractZipArchive(File archiveFile, File destinationFolder) throws CompressException { ZipFile zipFile = null; try { zipFile = new ZipFile(archiveFile); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); ZipArchiveEntry entry = null; byte[] buffer = new byte[BUFFER_SIZE]; for (; entries.hasMoreElements(); entry = entries.nextElement()) { File outputFile = new File( destinationFolder.getAbsolutePath() + IOUtils.DIR_SEPARATOR + entry.getName()); if (entry.isDirectory()) { FileUtils.forceMkdir(outputFile); } else { InputStream inputStream = zipFile.getInputStream(entry); OutputStream outputStream = FileUtils.openOutputStream(outputFile); try { IOUtils.copyLarge(inputStream, outputStream, buffer); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } } } } catch (Exception e) { throw new CompressException(e); } finally { ZipFile.closeQuietly(zipFile); } }
From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java
private static MediaType detectKmz(ZipFile zip) { boolean kmlFound = false; Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String name = entry.getName(); if (!entry.isDirectory() && name.indexOf('/') == -1 && name.indexOf('\\') == -1) { if (name.endsWith(".kml") && !kmlFound) { kmlFound = true;/*from w ww .j a va 2 s. c o m*/ } else { return null; } } } if (kmlFound) { return MediaType.application("vnd.google-earth.kmz"); } else { return null; } }
From source file:com.asakusafw.shafu.core.util.IoUtils.java
/** * Extracts a {@code *.zip} archive into the target folder. * @param monitor the progress monitor/* w ww. ja v a 2s.c om*/ * @param archiveFile the archive file * @param targetDirectory the target folder * @throws IOException if failed to extract the archive */ public static void extractZip(IProgressMonitor monitor, File archiveFile, File targetDirectory) throws IOException { SubMonitor sub = SubMonitor.convert(monitor, Messages.IoUtils_monitorExtractZip, 10); try { ZipFile zip = new ZipFile(archiveFile); try { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); if (entry.isDirectory()) { createDirectory(targetDirectory, entry); } else { InputStream input = zip.getInputStream(entry); try { File file = createFile(targetDirectory, entry, input); setFileMode(file, entry.getUnixMode()); } finally { input.close(); } sub.worked(1); sub.setWorkRemaining(10); } } } finally { zip.close(); } } finally { if (monitor != null) { monitor.done(); } } }
From source file:com.silverpeas.util.ZipManager.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. *//*w ww.ja v a 2 s.c o m*/ public static int getNbFiles(File archive) { ZipFile zipFile = null; int nbFiles = 0; try { 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) { SilverTrace.warn("util", "ZipManager.getNbFiles()", "util.EXE_ERROR_WHILE_COUNTING_FILE", "sourceFile = " + archive.getPath(), ioe); } finally { if (zipFile != null) { ZipFile.closeQuietly(zipFile); } } return nbFiles; }