List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getInputStream
public InputStream getInputStream(ZipArchiveEntry ze) throws IOException, ZipException
From source file:at.spardat.xma.xdelta.JarPatcher.java
/** * Main method to make {@link #applyDelta(ZipFile, ZipFile, ZipArchiveOutputStream, BufferedReader)} available at * the command line.<br>//from w w w .j av a2s .c o m * usage JarPatcher source patch output * * @param args the arguments * @throws IOException Signals that an I/O exception has occurred. */ public static void main(String[] args) throws IOException { String patchName = null; String outputName = null; String sourceName = null; if (args.length == 0) { System.err.println("usage JarPatcher patch [output [source]]"); System.exit(1); } else { patchName = args[0]; if (args.length > 1) { outputName = args[1]; if (args.length > 2) { sourceName = args[2]; } } } ZipFile patch = new ZipFile(patchName); ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list"); if (listEntry == null) { System.err.println("Invalid patch - list entry 'META-INF/file.list' not found"); System.exit(2); } BufferedReader list = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry))); String next = list.readLine(); if (sourceName == null) { sourceName = next; } next = list.readLine(); if (outputName == null) { outputName = next; } int ignoreSourcePaths = Integer.parseInt(System.getProperty("patcher.ignoreSourcePathElements", "0")); int ignoreOutputPaths = Integer.parseInt(System.getProperty("patcher.ignoreOutputPathElements", "0")); Path sourcePath = Paths.get(sourceName); Path outputPath = Paths.get(outputName); if (ignoreOutputPaths >= outputPath.getNameCount()) { patch.close(); StringBuilder b = new StringBuilder().append("Not enough path elements to ignore in output (") .append(ignoreOutputPaths).append(" in ").append(outputName).append(")"); throw new IOException(b.toString()); } if (ignoreSourcePaths >= sourcePath.getNameCount()) { patch.close(); StringBuilder b = new StringBuilder().append("Not enough path elements to ignore in source (") .append(sourcePath).append(" in ").append(sourceName).append(")"); throw new IOException(b.toString()); } sourcePath = sourcePath.subpath(ignoreSourcePaths, sourcePath.getNameCount()); outputPath = outputPath.subpath(ignoreOutputPaths, outputPath.getNameCount()); File sourceFile = sourcePath.toFile(); File outputFile = outputPath.toFile(); if (!(outputFile.getAbsoluteFile().getParentFile().mkdirs() || outputFile.getAbsoluteFile().getParentFile().exists())) { patch.close(); throw new IOException("Failed to create " + outputFile.getAbsolutePath()); } new JarPatcher(patchName, sourceFile.getName()).applyDelta(patch, new ZipFile(sourceFile), new ZipArchiveOutputStream(new FileOutputStream(outputFile)), list); list.close(); }
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./*from w ww .j a v a 2 s.c o 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:de.nx42.maps4cim.util.Compression.java
/** * Reads the first file entry in a zip file and returns it's contents * as uncompressed byte-array/* w w w . j ava 2 s .c o 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:com.android.tradefed.util.ZipUtil2.java
/** * Utility method to extract one specific file from zip file into a tmp file * * @param zipFile the {@link ZipFile} to extract * @param filePath the filePath of to extract * @throws IOException if failed to extract file * @return the {@link File} or null if not found */// www .j av a2 s . com public static File extractFileFromZip(ZipFile zipFile, String filePath) throws IOException { ZipArchiveEntry entry = zipFile.getEntry(filePath); if (entry == null) { return null; } File createdFile = FileUtil.createTempFile("extracted", FileUtil.getExtension(filePath)); FileUtil.writeToFile(zipFile.getInputStream(entry), createdFile); applyUnixModeIfNecessary(entry, createdFile); return createdFile; }
From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java
/** * OpenDocument files, along with EPub files, have a mimetype * entry in the root of their Zip file. This entry contains the * mimetype of the overall file, stored as a single string. *//*from w w w . jav a 2 s. co m*/ private static MediaType detectOpenDocument(ZipFile zip) { try { ZipArchiveEntry mimetype = zip.getEntry("mimetype"); if (mimetype != null) { InputStream stream = zip.getInputStream(mimetype); try { return MediaType.parse(IOUtils.toString(stream, "UTF-8")); } finally { stream.close(); } } else { return null; } } catch (IOException e) { return null; } }
From source file:com.vividsolutions.jump.io.CompressedFile.java
/** * Utility file open function - handles compressed and un-compressed files. * /* w ww . ja v a2s. c om*/ * @param filePath * name of the file to search for. * @param compressedEntry * name of the compressed file. * * <p> * If compressedEntry = null, opens a FileInputStream on filePath * </p> * * <p> * If filePath ends in ".zip" - opens the compressed Zip and * looks for the file called compressedEntry * </p> * * <p> * If filePath ends in ".gz" - opens the compressed .gz file. * </p> */ public static InputStream openFile(String filePath, String compressedEntry) throws IOException { System.out.println(filePath + " extract " + compressedEntry); if (isTar(filePath)) { InputStream is = new BufferedInputStream(new FileInputStream(filePath)); if (filePath.toLowerCase().endsWith("gz")) is = new GzipCompressorInputStream(is, true); else if (filePath.matches("(?i).*bz2?")) is = new BZip2CompressorInputStream(is, true); else if (filePath.matches("(?i).*xz")) is = new XZCompressorInputStream(is, true); TarArchiveInputStream tis = new TarArchiveInputStream(is); if (compressedEntry == null) return is; TarArchiveEntry entry; while ((entry = tis.getNextTarEntry()) != null) { if (entry.getName().equals(compressedEntry)) return tis; } throw createArchiveFNFE(filePath, compressedEntry); } else if (compressedEntry == null && isGZip(filePath)) { // gz compressed file -- easy InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new GzipCompressorInputStream(is, true); } else if (compressedEntry == null && isBZip(filePath)) { // bz compressed file -- easy InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new BZip2CompressorInputStream(is, true); //return new org.itadaki.bzip2.BZip2InputStream(is, false); } else if (compressedEntry == null && isXZ(filePath)) { InputStream is = new BufferedInputStream(new FileInputStream(filePath)); return new XZCompressorInputStream(is, true); } else if (compressedEntry != null && isZip(filePath)) { ZipFile zipFile = new ZipFile(filePath); ZipArchiveEntry zipEntry = zipFile.getEntry(compressedEntry); if (zipEntry != null) return zipFile.getInputStream(zipEntry); throw createArchiveFNFE(filePath, compressedEntry); } else if (compressedEntry != null && isSevenZ(filePath)) { SevenZFileGiveStream sevenZFile = new SevenZFileGiveStream(new File(filePath)); SevenZArchiveEntry entry; while ((entry = sevenZFile.getNextEntry()) != null) { if (entry.getName().equals(compressedEntry)) return sevenZFile.getCurrentEntryInputStream(); } throw createArchiveFNFE(filePath, compressedEntry); } // return plain stream if no compressedEntry else if (compressedEntry == null) { return new FileInputStream(filePath); } else { throw new IOException("Couldn't determine compressed file type for file '" + filePath + "' supposedly containing '" + compressedEntry + "'."); } }
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 *///from w ww . jav a 2 s. c o 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.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();// ww w.j a v a2 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.asakusafw.shafu.core.util.IoUtils.java
/** * Extracts a {@code *.zip} archive into the target folder. * @param monitor the progress monitor/*from w ww.ja v a2 s .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.android.repository.util.InstallerUtil.java
private static boolean readZipEntry(ZipFile zipFile, ZipArchiveEntry entry, OutputStream dest, long expectedSize, @NonNull ProgressIndicator progress) throws IOException { int size;//from w w w.j av a 2 s. c o m byte[] buf = new byte[8192]; double fraction = progress.getFraction(); try (BufferedOutputStream bufferedDest = new BufferedOutputStream(dest); InputStream s = new BufferedInputStream(zipFile.getInputStream(entry))) { while ((size = s.read(buf)) > -1) { bufferedDest.write(buf, 0, size); fraction += ((double) entry.getCompressedSize() / expectedSize) * ((double) size / entry.getSize()); progress.setFraction(fraction); if (progress.isCanceled()) { return true; } } } return false; }