List of usage examples for java.util.zip ZipFile close
public void close() throws IOException
From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java
/** * unzip CSAR packge/* w ww . j a va 2s .co m*/ * @param fileName filePath * @return */ public static int unzipCSAR(String fileName, String filePath) { final int BUFFER = 2048; int status = 0; try { ZipFile zipFile = new ZipFile(fileName); Enumeration emu = zipFile.entries(); int i = 0; while (emu.hasMoreElements()) { ZipEntry entry = (ZipEntry) emu.nextElement(); //read directory as file first,so only need to create directory if (entry.isDirectory()) { new File(filePath + entry.getName()).mkdirs(); continue; } BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); File file = new File(filePath + entry.getName()); //Because that is random to read zipfile,maybe the file is read first //before the directory is read,so we need to create directory first. File parent = file.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); bis.close(); if (entry.getName().endsWith(".zip")) { File subFile = new File(filePath + entry.getName()); if (subFile.exists()) { int subStatus = unzipCSAR(filePath + entry.getName(), subFile.getParent() + "/"); if (subStatus != 0) { LOG.error("sub file unzip fail!" + subFile.getName()); status = Constant.UNZIP_FAIL; return status; } } } } status = Constant.UNZIP_SUCCESS; zipFile.close(); } catch (Exception e) { status = Constant.UNZIP_FAIL; e.printStackTrace(); } return status; }
From source file:org.roda.core.plugins.plugins.characterization.ODFSignatureUtils.java
private static ByteArrayOutputStream addSignatureToStream(ZipFile zipFile, Element rootManifest, Element rootSignatures) throws IOException, TransformerFactoryConfigurationError, TransformerException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream())) { Enumeration<?> enumeration; for (enumeration = zipFile.entries(); enumeration.hasMoreElements();) { ZipEntry zeOut = (ZipEntry) enumeration.nextElement(); String fileName = zeOut.getName(); if (!fileName.equals(META_INF_DOCUMENTSIGNATURES_XML) && !"META-INF/manifest.xml".equals(fileName)) { zos.putNextEntry(zeOut); try (InputStream is = zipFile.getInputStream(zipFile.getEntry(fileName))) { zos.write(IOUtils.toByteArray(is)); zos.closeEntry();// www. jav a 2s . c o m } } } ZipEntry zeDocumentSignatures = new ZipEntry(META_INF_DOCUMENTSIGNATURES_XML); zos.putNextEntry(zeDocumentSignatures); try (ByteArrayOutputStream baosXML = new ByteArrayOutputStream()) { writeXML(baosXML, rootSignatures, false); zos.write(baosXML.toByteArray()); zos.closeEntry(); } ZipEntry zeManifest = new ZipEntry("META-INF/manifest.xml"); zos.putNextEntry(zeManifest); try (ByteArrayOutputStream baosManifest = new ByteArrayOutputStream()) { writeXML(baosManifest, rootManifest, false); zos.write(baosManifest.toByteArray()); zos.closeEntry(); } zipFile.close(); } return baos; }
From source file:org.jenkinsci.maven.plugins.hpi.RunMojo.java
private boolean isExtractedWebAppDirStale(File extractedWebAppDir, File webApp) throws IOException { if (!extractedWebAppDir.isDirectory()) { getLog().info(extractedWebAppDir + " does not yet exist, will receive " + webApp); return false; }/* w w w. j a v a 2s .c o m*/ if (extractedWebAppDir.lastModified() < webApp.lastModified()) { getLog().info(extractedWebAppDir + " is older than " + webApp + ", will recreate"); return true; } File extractedPath = new File(extractedWebAppDir, VERSION_PATH); if (!extractedPath.isFile()) { getLog().warn("no such file " + extractedPath); return false; } InputStream is = new FileInputStream(extractedPath); String extractedVersion; try { extractedVersion = loadVersion(is); } finally { is.close(); } if (extractedVersion == null) { getLog().warn("no " + VERSION_PROP + " in " + extractedPath); return false; } ZipFile zip = new ZipFile(webApp); String originalVersion; try { ZipEntry entry = zip.getEntry(VERSION_PATH); if (entry == null) { getLog().warn("no " + VERSION_PATH + " in " + webApp); return false; } is = zip.getInputStream(entry); try { originalVersion = loadVersion(is); } finally { is.close(); } } finally { zip.close(); } if (originalVersion == null) { getLog().warn("no " + VERSION_PROP + " in jar:" + webApp.toURI() + "!/" + VERSION_PATH); return false; } if (!extractedVersion.equals(originalVersion)) { getLog().info("Version " + extractedVersion + " in " + extractedWebAppDir + " does not match " + originalVersion + " in " + webApp + ", will recreate"); return true; } getLog().info(extractedWebAppDir + " already up to date with respect to " + webApp); return false; }
From source file:org.ut.biolab.medsavant.client.plugin.PluginController.java
/** * @deprecated// w w w .j av a2s. co m */ public void getGeneManiaData() { Runnable r = new Runnable() { @Override public void run() { String directoryPath = DirectorySettings.getCacheDirectory().getAbsolutePath(); if (!(new File(directoryPath + "/done.txt")).exists()) { URL pathToGMData = NetworkUtils .getKnownGoodURL("http://genomesavant.com/serve/data/genemania/gmdata.zip"); System.out.println("Downloding GeneMania data from " + pathToGMData.toString()); try { if (true) { throw new IOException( "Temporarily preventing gm data from downloading. Because it's so large it should only be downloaded once and on demand"); } File data = RemoteFileCache.getCacheFile(pathToGMData); System.out.println("data is" + data.getAbsolutePath()); ZipFile zipData = new ZipFile(data.getAbsolutePath()); Enumeration entries = zipData.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { (new File(directoryPath + "/" + entry.getName())).mkdirs(); continue; } //System.err.println("Extracting file: " + entry.getName()); copyInputStream(zipData.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(directoryPath + "/" + entry.getName()))); } zipData.close(); FileWriter fstream = new FileWriter(directoryPath + "/done.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("This file indicates that the GeneMANIA data has finished downloading."); out.close(); } catch (IOException ex) { Logger.getLogger(PluginController.class.getName()).log(Level.SEVERE, null, ex); } } } }; Thread t = new Thread(r); t.start(); }
From source file:org.apache.jackrabbit.j2ee.BackwardsCompatibilityIT.java
private void unpack(File archive, File dir) throws IOException { ZipFile zip = new ZipFile(archive); try {/*from w ww . j a v a 2s . com*/ Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().startsWith("META-INF")) { } else if (entry.isDirectory()) { new File(dir, entry.getName()).mkdirs(); } else { File file = new File(dir, entry.getName()); file.getParentFile().mkdirs(); InputStream input = zip.getInputStream(entry); try { OutputStream output = new FileOutputStream(file); try { IOUtils.copy(input, output); } finally { output.close(); } } finally { input.close(); } } } } finally { zip.close(); } }
From source file:apim.restful.exportimport.utils.APIImportUtil.java
/** * This method decompresses the archive/*from w w w .jav a2 s . c o m*/ * * @param sourceFile the archive containing the API * @param destinationDirectory location of the archive to be extracted * @return extractedFolder the name of the zip */ public static String unzipArchive(File sourceFile, File destinationDirectory) throws APIManagementException { InputStream inputStream = null; FileOutputStream fileOutputStream = null; File destinationFile; ZipFile zipfile = null; String extractedFolder = null; try { zipfile = new ZipFile(sourceFile); Enumeration zipEntries = null; if (zipfile != null) { zipEntries = zipfile.entries(); } if (zipEntries != null) { int index = 0; while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); if (entry.isDirectory()) { //This index variable is used to get the extracted folder name; that is root directory if (index == 0) { //Get the folder name without the '/' character at the end extractedFolder = entry.getName().substring(0, entry.getName().length() - 1); } index = -1; new File(destinationDirectory, entry.getName()).mkdir(); continue; } inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); destinationFile = new File(destinationDirectory, entry.getName()); fileOutputStream = new FileOutputStream(destinationFile); copyStreams(inputStream, fileOutputStream); } } } catch (ZipException e) { log.error("Error in retrieving archive files."); throw new APIManagementException("Error in retrieving archive files.", e); } catch (IOException e) { log.error("Error in decompressing API archive files."); throw new APIManagementException("Error in decompressing API archive files.", e); } finally { try { if (zipfile != null) { zipfile.close(); } if (inputStream != null) { inputStream.close(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (IOException e) { log.error("Error in closing streams while decompressing files."); } } return extractedFolder; }
From source file:com.alibaba.otter.shared.common.utils.extension.classpath.FileSystemClassScanner.java
private Class<?> scanInJar(String jarFileName, String className) { ZipFile zipfile = null; try {/* w ww . j a v a 2 s .c o m*/ zipfile = new ZipFile(jarFileName); Enumeration<?> zipenum = zipfile.entries(); ZipEntry entry = null; String tempClassName = null; while (zipenum.hasMoreElements()) { entry = (ZipEntry) zipenum.nextElement(); tempClassName = entry.getName(); if (tempClassName.endsWith(".class")) { tempClassName = StringUtils.replace(FilenameUtils.removeExtension(tempClassName), "/", "."); if (tempClassName.equals(className)) { try { return fileClassLoader.loadClass(className); } catch (Exception ex) { logger.warn("WARN ## load this class has an error,the fileName is = " + className, ex); } } } } } catch (IOException ex) { logger.error(ex.getMessage(), ex); } finally { if (zipfile != null) { try { zipfile.close(); } catch (IOException ex) { logger.warn(ex.getMessage(), ex); } } } return null; }
From source file:org.hyperic.hq.product.ClientPluginDeployer.java
public List unpackJar(String jar) throws IOException { ArrayList jars = new ArrayList(); ZipFile zipfile = new ZipFile(jar); for (Enumeration e = zipfile.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); String name = entry.getName(); if (entry.isDirectory()) { continue; }/*from w w w. j a va 2 s. c om*/ int ix = name.indexOf('/'); if (ix == -1) { continue; } String prefix = name.substring(0, ix); name = name.substring(ix + 1); File file = getFile(prefix, name); if (file == null) { continue; } if (name.endsWith(".jar")) { jars.add(file.toString()); } if (upToDate(entry, file)) { continue; } InputStream is = zipfile.getInputStream(entry); try { write(is, file); } catch (IOException ioe) { zipfile.close(); throw ioe; } finally { is.close(); } } zipfile.close(); return jars; }
From source file:fr.gouv.finances.dgfip.xemelios.importers.archives.ArchiveImporter.java
protected FileInfo doImportPJ(String filePath, Pair collectivite, String nomOrigine, String idUnique, Element document, File volume, String archiveName) throws IOException { File f = null;/* w w w . j av a 2 s . c o m*/ try { ZipFile zf = new ZipFile(volume); f = extractFileFromZip(zf, filePath); zf.close(); } catch (Exception ex) { throw new RuntimeException(ex); } FileInfo fInfo = new FileInfo(); try { // les PJ sont systmatiquement crases fInfo.setDebutImport(System.currentTimeMillis()); PJRef pj = new PJRef(); pj.setCollectivite(collectivite.key); pj.setFileName(nomOrigine); pj.setPjName(idUnique); pj.setUncompressedSize(f.length()); pj.setTmpFileName(f.getAbsolutePath()); pj.setPath(filePath); DataLayerManager.getImplementation().importPj(pj, archiveName, getUser()); fInfo.incElement(new Marker("PJ", null)); document.addAttribute(new Attribute("imported", "Oui")); } catch (Exception ex) { logger.error("doImportPJ(ZipFile,String,Pair,Pair,String,String)", ex); } finally { if (f.exists()) { f.delete(); } fInfo.setFinImport(System.currentTimeMillis()); } return fInfo; }
From source file:jp.co.cyberagent.jenkins.plugins.DeployStrategyAndroid.java
private String getStringFromManifest(final String name) { File tempApk = null;// w w w .ja v a 2s. c o m InputStream is = null; ZipFile zip = null; try { tempApk = File.createTempFile(getBuild().getId(), "nr-" + getBuild().getNumber()); mApkFile.copyTo(new FileOutputStream(tempApk)); zip = new ZipFile(tempApk); ZipEntry mft = zip.getEntry("AndroidManifest.xml"); is = zip.getInputStream(mft); byte[] xml = new byte[is.available()]; is.read(xml); String string = AndroidUtils.decompressXML(xml); int start = string.indexOf(name + "=\"") + name.length() + 2; int end = string.indexOf("\"", start); String version = string.substring(start, end); if (version.startsWith("resourceID 0x")) { int resId = Integer.parseInt(version.substring(13), 16); return getStringFromResource(tempApk, resId); } else { return version; } } catch (Exception e) { getLogger().println(TAG + "Error: " + e.getMessage()); } finally { if (tempApk != null) tempApk.delete(); if (zip != null) try { zip.close(); } catch (IOException e) { getLogger().println(TAG + "Error: " + e.getMessage()); } if (is != null) try { is.close(); } catch (IOException e) { getLogger().println(TAG + "Error: " + e.getMessage()); } } return null; }