List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getInputStream
public InputStream getInputStream(ZipArchiveEntry ze) throws IOException, ZipException
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
public static boolean removeZipEntry(File file, Pattern pattern, File targetFile) throws FileNotFoundException, IOException { byte[] buffer = new byte[1024]; java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile))); BufferedOutputStream bo = new BufferedOutputStream(out); InputStream inputStream;/*w w w. ja v a 2 s .co m*/ Enumeration enumeration = zipFile.entries(); while (enumeration.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); String name = zipEntry.getName(); if (pattern.matcher(name).find()) { continue; } out.putNextEntry(zipEntry); inputStream = zipFile.getInputStream(zipEntry); write(inputStream, out, buffer); bo.flush(); } closeQuitely(zipFile); closeQuitely(out); closeQuitely(bo); return true; }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * <p>// w ww . jav a 2 s .c om * unzip. * </p> * * @param zipFile a {@link java.io.File} object. * @param destination a {@link String} object. * @param encoding a {@link String} object. * @return a {@link java.util.List} object. */ public static List<String> unzip(final File zipFile, final String destination, String encoding) { List<String> fileNames = new ArrayList<String>(); String dest = destination; if (!destination.endsWith(File.separator)) { dest = destination + File.separator; } ZipFile file; try { file = null; if (null == encoding) { file = new ZipFile(zipFile); } else { file = new ZipFile(zipFile, encoding); } Enumeration<ZipArchiveEntry> en = file.getEntries(); ZipArchiveEntry ze = null; while (en.hasMoreElements()) { ze = en.nextElement(); File f = new File(dest, ze.getName()); if (ze.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); InputStream is = file.getInputStream(ze); OutputStream os = new FileOutputStream(f); IOUtils.copy(is, os); is.close(); os.close(); fileNames.add(f.getAbsolutePath()); } } file.close(); } catch (IOException e) { e.printStackTrace(); } return fileNames; }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
public static List<String> unzip(final File zipFile, final String destination, String encoding, Map<String, ZipEntry> zipEntryMethodMap, boolean isRelativePath) { List<String> fileNames = new ArrayList<String>(); String dest = destination;/* w ww .j av a2 s . co m*/ if (!destination.endsWith(File.separator)) { dest = destination + File.separator; } ZipFile file; try { file = null; if (null == encoding) { file = new ZipFile(zipFile); } else { file = new ZipFile(zipFile, encoding); } Enumeration<ZipArchiveEntry> en = file.getEntries(); ZipArchiveEntry ze = null; while (en.hasMoreElements()) { ze = en.nextElement(); File f = new File(dest, ze.getName()); if (ze.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); InputStream is = file.getInputStream(ze); OutputStream os = new FileOutputStream(f); IOUtils.copy(is, os); is.close(); os.close(); fileNames.add(f.getAbsolutePath()); if (zipEntryMethodMap != null && ze.getMethod() == STORED) { zipEntryMethodMap.put(isRelativePath ? ze.getName() : f.getAbsolutePath(), ze); } } } file.close(); } catch (IOException e) { e.printStackTrace(); } return fileNames; }
From source file:net.orpiske.ssps.common.archive.zip.ZipArchive.java
private long extractFile(ZipFile zipFile, ZipArchiveEntry entry, File outFile) throws IOException, ZipException, FileNotFoundException { InputStream fin = null;/*from w w w . ja va2 s. co m*/ BufferedInputStream bin = null; FileOutputStream out = null; long ret = 0; try { fin = zipFile.getInputStream(entry); bin = new BufferedInputStream(fin); out = new FileOutputStream(outFile); ret += IOUtils.copy(fin, out); IOUtils.closeQuietly(out); IOUtils.closeQuietly(bin); IOUtils.closeQuietly(fin); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(bin); IOUtils.closeQuietly(fin); } return ret; }
From source file:de.crowdcode.movmvn.core.Unzipper.java
/** * Unzip a file.//w w w .j av a 2 s . c o m * * @param archiveFile * to be unzipped * @param outPath * the place to put the result * @throws IOException * exception * @throws ZipException * exception */ public void unzipFileToDir(final File archiveFile, final File outPath) throws IOException, ZipException { ZipFile zipFile = new ZipFile(archiveFile); Enumeration<ZipArchiveEntry> e = zipFile.getEntries(); while (e.hasMoreElements()) { ZipArchiveEntry entry = e.nextElement(); File file = new File(outPath, entry.getName()); if (entry.isDirectory()) { FileUtils.forceMkdir(file); } else { InputStream is = zipFile.getInputStream(entry); FileOutputStream os = FileUtils.openOutputStream(file); try { IOUtils.copy(is, os); } finally { os.close(); is.close(); } file.setLastModified(entry.getTime()); } } }
From source file:com.liferay.blade.cli.command.InstallExtensionCommandTest.java
private static void _testJarsDiff(File warFile1, File warFile2) throws IOException { DifferenceCalculator differenceCalculator = new DifferenceCalculator(warFile1, warFile2); differenceCalculator.setFilenameRegexToIgnore(Collections.singleton(".*META-INF.*")); differenceCalculator.setIgnoreTimestamps(true); Differences differences = differenceCalculator.getDifferences(); if (!differences.hasDifferences()) { return;//from ww w . j a va 2 s .c om } StringBuilder message = new StringBuilder(); message.append("WAR "); message.append(warFile1); message.append(" and "); message.append(warFile2); message.append(" do not match:"); message.append(System.lineSeparator()); boolean realChange; Map<String, ZipArchiveEntry> added = differences.getAdded(); Map<String, ZipArchiveEntry[]> changed = differences.getChanged(); Map<String, ZipArchiveEntry> removed = differences.getRemoved(); if (added.isEmpty() && !changed.isEmpty() && removed.isEmpty()) { realChange = false; ZipFile zipFile1 = null; ZipFile zipFile2 = null; try { zipFile1 = new ZipFile(warFile1); zipFile2 = new ZipFile(warFile2); for (Map.Entry<String, ZipArchiveEntry[]> entry : changed.entrySet()) { ZipArchiveEntry[] zipArchiveEntries = entry.getValue(); ZipArchiveEntry zipArchiveEntry1 = zipArchiveEntries[0]; ZipArchiveEntry zipArchiveEntry2 = zipArchiveEntries[0]; if (zipArchiveEntry1.isDirectory() && zipArchiveEntry2.isDirectory() && (zipArchiveEntry1.getSize() == zipArchiveEntry2.getSize()) && (zipArchiveEntry1.getCompressedSize() <= 2) && (zipArchiveEntry2.getCompressedSize() <= 2)) { // Skip zipdiff bug continue; } try (InputStream inputStream1 = zipFile1 .getInputStream(zipFile1.getEntry(zipArchiveEntry1.getName())); InputStream inputStream2 = zipFile2 .getInputStream(zipFile2.getEntry(zipArchiveEntry2.getName()))) { List<String> lines1 = StringTestUtil.readLines(inputStream1); List<String> lines2 = StringTestUtil.readLines(inputStream2); message.append(System.lineSeparator()); message.append("--- "); message.append(zipArchiveEntry1.getName()); message.append(System.lineSeparator()); message.append("+++ "); message.append(zipArchiveEntry2.getName()); message.append(System.lineSeparator()); Patch<String> diff = DiffUtils.diff(lines1, lines2); for (Delta<String> delta : diff.getDeltas()) { message.append('\t'); message.append(delta.getOriginal()); message.append(System.lineSeparator()); message.append('\t'); message.append(delta.getRevised()); message.append(System.lineSeparator()); } } realChange = true; break; } } finally { ZipFile.closeQuietly(zipFile1); ZipFile.closeQuietly(zipFile2); } } else { realChange = true; } Assert.assertFalse(message.toString(), realChange); }
From source file:com.fujitsu.dc.core.bar.BarFileInstaller.java
private void checkAndReadManifest(String entryName, ZipArchiveEntry zae, ZipFile zipFile) throws IOException { InputStream inStream = zipFile.getInputStream(zae); try {//w ww .j a v a2s . com JSONManifest manifest = BarFileUtils.readJsonEntry(inStream, entryName, JSONManifest.class); if (!manifest.checkSchema()) { throw DcCoreException.BarInstall.BAR_FILE_INVALID_STRUCTURES.params(entryName); } this.manifestJson = manifest.getJson(); } finally { IOUtils.closeQuietly(inStream); } }
From source file:io.personium.core.bar.BarFileInstaller.java
private void checkAndReadManifest(String entryName, ZipArchiveEntry zae, ZipFile zipFile) throws IOException { InputStream inStream = zipFile.getInputStream(zae); try {/*w w w . j a va 2s. c o m*/ JSONManifest manifest = BarFileUtils.readJsonEntry(inStream, entryName, JSONManifest.class); if (!manifest.checkSchema()) { throw PersoniumCoreException.BarInstall.BAR_FILE_INVALID_STRUCTURES.params(entryName); } this.manifestJson = manifest.getJson(); } finally { IOUtils.closeQuietly(inStream); } }
From source file:com.facebook.buck.util.unarchive.Unzip.java
private void writeZipContents(ZipFile zip, ZipArchiveEntry entry, ProjectFilesystem filesystem, Path target) throws IOException { // Write file try (InputStream is = zip.getInputStream(entry)) { if (entry.isUnixSymlink()) { filesystem.createSymLink(target, filesystem.getPath(new String(ByteStreams.toByteArray(is), Charsets.UTF_8)), /* force */ true); } else {/*from w w w. j ava 2s .c o m*/ try (OutputStream out = filesystem.newFileOutputStream(target)) { ByteStreams.copy(is, out); } } } Path filePath = filesystem.resolve(target); File file = filePath.toFile(); // restore mtime for the file file.setLastModified(entry.getTime()); // TODO(simons): Implement what the comment below says we should do. // // Sets the file permissions of the output file given the information in {@code entry}'s // extra data field. According to the docs at // http://www.opensource.apple.com/source/zip/zip-6/unzip/unzip/proginfo/extra.fld there // are two extensions that might support file permissions: Acorn and ASi UNIX. We shall // assume that inputs are not from an Acorn SparkFS. The relevant section from the docs: // // <pre> // The following is the layout of the ASi extra block for Unix. The // local-header and central-header versions are identical. // (Last Revision 19960916) // // Value Size Description // ----- ---- ----------- // (Unix3) 0x756e Short tag for this extra block type ("nu") // TSize Short total data size for this block // CRC Long CRC-32 of the remaining data // Mode Short file permissions // SizDev Long symlink'd size OR major/minor dev num // UID Short user ID // GID Short group ID // (var.) variable symbolic link filename // // Mode is the standard Unix st_mode field from struct stat, containing // user/group/other permissions, setuid/setgid and symlink info, etc. // </pre> // // From the stat man page, we see that the following mask values are defined for the file // permissions component of the st_mode field: // // <pre> // S_ISUID 0004000 set-user-ID bit // S_ISGID 0002000 set-group-ID bit (see below) // S_ISVTX 0001000 sticky bit (see below) // // S_IRWXU 00700 mask for file owner permissions // // S_IRUSR 00400 owner has read permission // S_IWUSR 00200 owner has write permission // S_IXUSR 00100 owner has execute permission // // S_IRWXG 00070 mask for group permissions // S_IRGRP 00040 group has read permission // S_IWGRP 00020 group has write permission // S_IXGRP 00010 group has execute permission // // S_IRWXO 00007 mask for permissions for others // (not in group) // S_IROTH 00004 others have read permission // S_IWOTH 00002 others have write permission // S_IXOTH 00001 others have execute permission // </pre> // // For the sake of our own sanity, we're going to assume that no-one is using symlinks, // but we'll check and throw if they are. // // Before we do anything, we should check the header ID. Pfft! // // Having jumped through all these hoops, it turns out that InfoZIP's "unzip" store the // values in the external file attributes of a zip entry (found in the zip's central // directory) assuming that the OS creating the zip was one of an enormous list that // includes UNIX but not Windows, it first searches for the extra fields, and if not found // falls through to a code path that supports MS-DOS and which stores the UNIX file // attributes in the upper 16 bits of the external attributes field. // // We'll support neither approach fully, but we encode whether this file was executable // via storing 0100 in the fields that are typically used by zip implementations to store // POSIX permissions. If we find it was executable, use the platform independent java // interface to make this unpacked file executable. Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); if (permissions.contains(PosixFilePermission.OWNER_EXECUTE) && file.getCanonicalFile().exists()) { MostFiles.makeExecutable(filePath); } }
From source file:info.magnolia.ui.framework.command.ImportZipCommand.java
protected void handleFileEntry(ZipFile zip, ZipArchiveEntry entry) throws IOException, RepositoryException { String fileName = entry.getName(); if (StringUtils.contains(fileName, "/")) { fileName = StringUtils.substringAfterLast(fileName, "/"); }//from w ww . ja v a 2 s.c o m String extension = StringUtils.substringAfterLast(fileName, "."); InputStream stream = zip.getInputStream(entry); FileOutputStream os = null; try { UploadReceiver receiver = createReceiver(); String folderPath = extractEntryPath(entry); if (folderPath.startsWith("/")) { folderPath = folderPath.substring(1); } Node folder = getJCRNode(context); if (StringUtils.isNotBlank(folderPath)) { if (folder.hasNode(folderPath)) { folder = folder.getNode(folderPath); } else { folder = NodeUtil.createPath(folder, folderPath, NodeTypes.Folder.NAME, true); } } receiver.setFieldType(UploadField.FieldType.BYTE_ARRAY); receiver.receiveUpload(fileName, StringUtils.defaultIfEmpty(MIMEMapping.getMIMEType(extension), DEFAULT_MIME_TYPE)); receiver.setValue(IOUtils.toByteArray(stream)); doHandleEntryFromReceiver(folder, receiver); } catch (IOException e) { throw e; } finally { IOUtils.closeQuietly(stream); IOUtils.closeQuietly(os); } }