List of usage examples for org.apache.commons.compress.archivers.zip ZipFile close
public void close() throws IOException
From source file:net.sourceforge.pmd.it.ZipFileExtractor.java
/** * Extracts the given zip file into the tempDir. * @param zipPath the zip file to extract * @param tempDir the target directory/* ww w . ja v a 2s .co m*/ * @throws Exception if any error happens during extraction */ public static void extractZipFile(Path zipPath, Path tempDir) throws Exception { ZipFile zip = new ZipFile(zipPath.toFile()); try { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); File file = tempDir.resolve(entry.getName()).toFile(); if (entry.isDirectory()) { assertTrue(file.mkdirs()); } else { try (InputStream data = zip.getInputStream(entry); OutputStream fileOut = new FileOutputStream(file);) { IOUtils.copy(data, fileOut); } if ((entry.getUnixMode() & OWNER_EXECUTABLE) == OWNER_EXECUTABLE) { file.setExecutable(true); } } } } finally { zip.close(); } }
From source file:net.test.aliyun.oss.ImportShooterData.java
@Override public void onStart(AppContext appContext) throws Throwable { String BasePath = "D:/shooterData/"; OSSClient client = appContext.getInstance(OSSClient.class); String tempPath = appContext.getEnvironment().envVar(Environment.HASOR_TEMP_PATH); ////from w w w .ja va 2 s. c o m File[] zipPacks = new File(BasePath).listFiles(); long intCount = 0; long size = 0; for (File zipfile : zipPacks) { String fileName = zipfile.getName(); fileName = fileName.split("\\.")[0]; ZipFile zipPack = new ZipFile(zipfile); Enumeration<ZipArchiveEntry> enumZip = zipPack.getEntries(); System.out.println(fileName); while (enumZip.hasMoreElements()) { ZipArchiveEntry ent = enumZip.nextElement(); if (ent.isDirectory() == true) { continue; } String itemName = ent.getName(); // // ObjectMetadata info = this.passInfo(tempPath, zipPack, ent); // info.addUserMetadata("oldFileName", itemName); // // String key = fileName + "/" + UUID.randomUUID().toString().replace("-", "") + ".rar"; //InputStream inStream = zipPack.getInputStream(ent); //PutObjectResult res = client.putObject("files-subtitle", key, inStream, info); // intCount++; long itemSize = ent.getSize(); String stated = String.format("%s-%s/%s\t%s\t%s", intCount, fileName, itemName, itemSize, ""); System.out.println(stated + " -> " + ""); size = size + itemSize; } zipPack.close(); } System.out.println(intCount + "\t" + size); }
From source file:org.apache.ant.compress.resources.ZipResource.java
/** * Return an InputStream for reading the contents of this Resource. * @return an InputStream object.// w w w.j a v a 2 s . co m * @throws IOException if the zip file cannot be opened, * or the entry cannot be read. */ public InputStream getInputStream() throws IOException { if (isReference()) { return ((Resource) getCheckedRef()).getInputStream(); } File f = getZipfile(); if (f == null) { return super.getInputStream(); } final ZipFile z = new ZipFile(f, getEncoding()); ZipArchiveEntry ze = z.getEntry(getName()); if (ze == null) { z.close(); throw new BuildException("no entry " + getName() + " in " + getArchive()); } return new FilterInputStream(z.getInputStream(ze)) { public void close() throws IOException { FileUtils.close(in); z.close(); } protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } } }; }
From source file:org.apache.brooklyn.rest.resources.CatalogResource.java
@Override @Beta/* www .j a va 2 s . co m*/ public Response createFromArchive(byte[] zipInput) { if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.ROOT, null)) { throw WebResourceUtils.forbidden("User '%s' is not authorized to add catalog item", Entitlements.getEntitlementContext().user()); } BundleMaker bm = new BundleMaker(mgmtInternal()); File f = null, f2 = null; try { f = Os.newTempFile("brooklyn-posted-archive", "zip"); try { Files.write(zipInput, f); } catch (IOException e) { Exceptions.propagate(e); } ZipFile zf; try { zf = new ZipFile(f); } catch (IOException e) { throw new IllegalArgumentException("Invalid ZIP/JAR archive: " + e); } ZipArchiveEntry bom = zf.getEntry("catalog.bom"); if (bom == null) { bom = zf.getEntry("/catalog.bom"); } if (bom == null) { throw new IllegalArgumentException("Archive must contain a catalog.bom file in the root"); } String bomS; try { bomS = Streams.readFullyString(zf.getInputStream(bom)); } catch (IOException e) { throw new IllegalArgumentException("Error reading catalog.bom from ZIP/JAR archive: " + e); } try { zf.close(); } catch (IOException e) { log.debug("Swallowed exception closing zipfile. Full error logged at trace: {}", e.getMessage()); log.trace("Exception closing zipfile", e); } VersionedName vn = BasicBrooklynCatalog.getVersionedName(BasicBrooklynCatalog.getCatalogMetadata(bomS)); Manifest mf = bm.getManifest(f); if (mf == null) { mf = new Manifest(); } String bundleNameInMF = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME); if (Strings.isNonBlank(bundleNameInMF)) { if (!bundleNameInMF.equals(vn.getSymbolicName())) { throw new IllegalArgumentException("JAR MANIFEST symbolic-name '" + bundleNameInMF + "' does not match '" + vn.getSymbolicName() + "' defined in BOM"); } } else { bundleNameInMF = vn.getSymbolicName(); mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, bundleNameInMF); } String bundleVersionInMF = mf.getMainAttributes().getValue(Constants.BUNDLE_VERSION); if (Strings.isNonBlank(bundleVersionInMF)) { if (!bundleVersionInMF.equals(vn.getVersion().toString())) { throw new IllegalArgumentException("JAR MANIFEST version '" + bundleVersionInMF + "' does not match '" + vn.getVersion() + "' defined in BOM"); } } else { bundleVersionInMF = vn.getVersion().toString(); mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, bundleVersionInMF); } if (mf.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION) == null) { mf.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0"); } f2 = bm.copyAddingManifest(f, mf); BasicManagedBundle bundleMetadata = new BasicManagedBundle(bundleNameInMF, bundleVersionInMF, null); Bundle bundle; try (FileInputStream f2in = new FileInputStream(f2)) { bundle = ((ManagementContextInternal) mgmt()).getOsgiManager().get() .installUploadedBundle(bundleMetadata, f2in, false); } catch (Exception e) { throw Exceptions.propagate(e); } Iterable<? extends CatalogItem<?, ?>> catalogItems = MutableList .copyOf(((ManagementContextInternal) mgmt()).getOsgiManager().get().loadCatalogBom(bundle)); return buildCreateResponse(catalogItems); } catch (RuntimeException ex) { throw WebResourceUtils.badRequest(ex); } finally { if (f != null) f.delete(); if (f2 != null) f2.delete(); } }
From source file:org.apache.tika.parser.pkg.ZipContainerDetector.java
private static MediaType detectZipFormat(TikaInputStream tis) { try {/*from w w w . j av a 2 s. co m*/ //try opc first because opening a package //will not necessarily throw an exception for //truncated files. MediaType type = detectOPCBased(tis); if (type != null) { return type; } ZipFile zip = new ZipFile(tis.getFile()); // TODO: hasFile()? try { type = detectOpenDocument(zip); if (type == null) { type = detectIWork13(zip); } if (type == null) { type = detectIWork(zip); } if (type == null) { type = detectJar(zip); } if (type == null) { type = detectKmz(zip); } if (type == null) { type = detectIpa(zip); } if (type != null) { return type; } } finally { // TODO: shouldn't we record the open // container so it can be later // reused...? // tis.setOpenContainer(zip); try { zip.close(); } catch (IOException e) { // ignore } } } catch (IOException e) { // ignore } // Fallback: it's still a zip file, we just don't know what kind of one return MediaType.APPLICATION_ZIP; }
From source file:org.apache.tika.server.CXFTestBase.java
protected Map<String, String> readZipArchive(InputStream inputStream) throws IOException { Map<String, String> data = new HashMap<String, String>(); Path tempFile = writeTemporaryArchiveFile(inputStream, "zip"); ZipFile zip = new ZipFile(tempFile.toFile()); Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(zip.getInputStream(entry), bos); data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray())); }//from ww w . j a v a 2s. c om zip.close(); Files.delete(tempFile); return data; }
From source file:org.apache.tika.server.CXFTestBase.java
protected String readArchiveText(InputStream inputStream) throws IOException { Path tempFile = writeTemporaryArchiveFile(inputStream, "zip"); ZipFile zip = new ZipFile(tempFile.toFile()); zip.getEntry(UnpackerResource.TEXT_FILENAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(zip.getInputStream(zip.getEntry(UnpackerResource.TEXT_FILENAME)), bos); zip.close(); Files.delete(tempFile);/*from ww w .j a v a2 s . com*/ return bos.toString(UTF_8.name()); }
From source file:org.beangle.commons.archiver.ZipUtils.java
public static List<String> unzip(final File zipFile, final String destination, String encoding) { List<String> fileNames = CollectUtils.newArrayList(); String dest = destination;//from www . java2 s .c om 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); @SuppressWarnings("unchecked") 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:org.beangle.commons.archiver.ZipUtils.java
public static boolean isZipFile(File zipFile) { try {//from ww w . j av a 2s . co m ZipFile zf = new ZipFile(zipFile); boolean isZip = zf.getEntries().hasMoreElements(); zf.close(); return isZip; } catch (IOException e) { return false; } }
From source file:org.beangle.ems.util.ZipUtils.java
/** * <p>/* ww w . jav a 2 s .co m*/ * unzip. * </p> * * @param zipFile a {@link java.io.File} object. * @param destination a {@link java.lang.String} object. * @param encoding a {@link java.lang.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 = CollectUtils.newArrayList(); 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); IOs.copy(is, os); is.close(); os.close(); fileNames.add(f.getAbsolutePath()); } } file.close(); } catch (IOException e) { e.printStackTrace(); } return fileNames; }