List of usage examples for java.util.zip ZipFile close
public void close() throws IOException
From source file:de.shadowhunt.subversion.internal.AbstractHelper.java
private void extractArchive(final File zip, final File prefix) throws Exception { final ZipFile zipFile = new ZipFile(zip); final Enumeration<? extends ZipEntry> enu = zipFile.entries(); while (enu.hasMoreElements()) { final ZipEntry zipEntry = enu.nextElement(); final String name = zipEntry.getName(); final File file = new File(prefix, name); if (name.charAt(name.length() - 1) == Resource.SEPARATOR_CHAR) { if (!file.isDirectory() && !file.mkdirs()) { throw new IOException("can not create directory structure: " + file); }/*from www . j a v a 2 s.com*/ continue; } final File parent = file.getParentFile(); if (parent != null) { if (!parent.isDirectory() && !parent.mkdirs()) { throw new IOException("can not create directory structure: " + parent); } } try (final InputStream is = zipFile.getInputStream(zipEntry)) { try (final OutputStream os = new FileOutputStream(file)) { IOUtils.copy(is, os); } } } zipFile.close(); }
From source file:com.servoy.extension.install.CopyZipEntryImporter.java
public void handleFile() { if (expFile != null && expFile.exists() && expFile.isFile() && expFile.canRead() && expFile.getName().endsWith(FileBasedExtensionProvider.EXTENSION_PACKAGE_FILE_EXTENSION) && installDir != null && installDir.exists() && installDir.isDirectory() && installDir.canWrite()) { ZipFile zipFile = null; try {/*from ww w .j a va 2 s . c o m*/ zipFile = new ZipFile(expFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.isDirectory()) { String fileName = entry.getName().replace('\\', '/'); fileName = fileName.replace(WEBTEMPLATES_SOURCE_FOLDER, WEBTEMPLATES_DESTINATION_FOLDER); File outputFile = new File(installDir, fileName); handleZipEntry(outputFile, zipFile, entry); } } } catch (IOException ex) { String tmp = "Exception while handling entries of expFile: " + expFile; //$NON-NLS-1$ messages.addError(tmp); Debug.error(tmp, ex); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { // ignore } } enforceBackUpFolderLimit(); } handleExpFile(); } else { // shouldn't happen String tmp = "Invalid install/uninstall file/destination: " + expFile + ", " + installDir; //$NON-NLS-1$//$NON-NLS-2$ messages.addError(tmp); Debug.error(tmp); } }
From source file:com.cisco.ca.cstg.pdi.services.ConfigurationServiceImpl.java
private boolean validateZipFile(File file) throws IOException { boolean isMetaDataPresent = STATUS_FALSE; boolean isConfigFilePresent = STATUS_FALSE; ZipFile zipFile = null; try {//from w w w.j av a 2 s .c o m zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); if (zipEntry.getName().equals(Constants.PDI_CONFIG_FILE_NAME)) { isConfigFilePresent = STATUS_TRUE; } else if (zipEntry.getName().equals(Constants.PDI_META_DATA_FILE_NAME)) { isMetaDataPresent = STATUS_TRUE; } } } catch (IOException e) { LOGGER.error("Error occured while trying to validate zip files.", e); throw e; } finally { if (zipFile != null) { zipFile.close(); } } if (isMetaDataPresent && isConfigFilePresent) { return STATUS_TRUE; } return STATUS_FALSE; }
From source file:com.github.chenxiaolong.dualbootpatcher.switcher.service.BootUIActionTask.java
/** * Get currently installed version of boot UI * * @param iface Mbtool interface//from ww w . j a va2 s . c o m * @return The {@link Version} installed or null if an error occurs or the version number is * invalid * @throws IOException * @throws MbtoolException */ @Nullable private Version getCurrentVersion(MbtoolInterface iface) throws IOException, MbtoolException { String mountPoint = getCacheMountPoint(iface); String zipPath = mountPoint + MAPPINGS[0].target; File tempZip = new File(getContext().getCacheDir() + "/bootui.zip"); tempZip.delete(); try { iface.pathCopy(zipPath, tempZip.getAbsolutePath()); iface.pathChmod(tempZip.getAbsolutePath(), 0644); // Set SELinux label try { String label = iface.pathSelinuxGetLabel(tempZip.getParent(), false); iface.pathSelinuxSetLabel(tempZip.getAbsolutePath(), label, false); } catch (MbtoolCommandException e) { Log.w(TAG, tempZip + ": Failed to set SELinux label", e); } } catch (MbtoolCommandException e) { return null; } ZipFile zf = null; String versionStr = null; String gitVersionStr = null; try { zf = new ZipFile(tempZip); final Enumeration<? extends ZipEntry> entries = zf.entries(); while (entries.hasMoreElements()) { final ZipEntry ze = entries.nextElement(); if (ze.getName().equals(PROPERTIES_FILE)) { Properties prop = new Properties(); prop.load(zf.getInputStream(ze)); versionStr = prop.getProperty(PROP_VERSION); gitVersionStr = prop.getProperty(PROP_GIT_VERSION); break; } } } catch (IOException e) { Log.e(TAG, "Failed to read bootui.zip", e); } finally { if (zf != null) { try { zf.close(); } catch (IOException e) { // Ignore } } tempZip.delete(); } Log.d(TAG, "Boot UI version: " + versionStr); Log.d(TAG, "Boot UI git version: " + gitVersionStr); if (versionStr != null) { return Version.from(versionStr); } else { return null; } }
From source file:org.geoserver.kml.KMLReflectorTest.java
@Test public void testKmzEmbededPointImageSize() throws Exception { WMSMapContent mapContent = createMapContext(MockData.POINTS, "big-mark"); File temp = File.createTempFile("test", "kmz", new File("target")); temp.delete();/* ww w .ja v a 2 s .c o m*/ temp.mkdir(); temp.deleteOnExit(); File zip = new File(temp, "kmz.zip"); zip.deleteOnExit(); // create hte map producer KMZMapOutputFormat mapProducer = new KMZMapOutputFormat(getWMS()); KMLMap map = mapProducer.produceMap(mapContent); FileOutputStream output = new FileOutputStream(zip); new KMLMapResponse(new KMLEncoder(), getWMS()).write(map, output, null); output.flush(); output.close(); assertTrue(zip.exists()); // unzip and test it ZipFile zipFile = new ZipFile(zip); ZipEntry kmlEntry = zipFile.getEntry("wms.kml"); InputStream kmlStream = zipFile.getInputStream(kmlEntry); Document kmlResult = XMLUnit.buildTestDocument(new InputSource(kmlStream)); Double scale = Double.parseDouble(XMLUnit.newXpathEngine() .getMatchingNodes("(//kml:Style)[1]/kml:IconStyle/kml:scale", kmlResult).item(0).getTextContent()); assertEquals(49d / 16d, scale, 0.01); zipFile.close(); }
From source file:org.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java
public File appendPOMToJar(String pom, String jarPath, GAV gav) { File originalJarFile = new File(jarPath); File appendedJarFile = new File(jarPath + ".tmp"); try {/*from w ww . j ava2 s. c om*/ ZipFile war = new ZipFile(originalJarFile); ZipOutputStream append = new ZipOutputStream(new FileOutputStream(appendedJarFile)); // first, copy contents from existing war Enumeration<? extends ZipEntry> entries = war.entries(); while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); // System.out.println("copy: " + e.getName()); append.putNextEntry(e); if (!e.isDirectory()) { IOUtil.copy(war.getInputStream(e), append); } append.closeEntry(); } // append pom. ZipEntry e = new ZipEntry(getPomXmlPath(gav)); // System.out.println("append: " + e.getName()); append.putNextEntry(e); append.write(pom.getBytes()); append.closeEntry(); // close war.close(); append.close(); } catch (ZipException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //originalJarFile.delete(); //appendedJarFile.renameTo(originalJarFile); return appendedJarFile; }
From source file:UnpackedJarFile.java
public static void unzipToDirectory(ZipFile zipFile, File destDir) throws IOException { Enumeration entries = zipFile.entries(); try {//from w ww.j a v a 2 s .c o m while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { File dir = new File(destDir, entry.getName()); createDirectory(dir); } else { File file = new File(destDir, entry.getName()); createDirectory(file.getParentFile()); OutputStream out = null; InputStream in = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); in = zipFile.getInputStream(entry); writeAll(in, out); } finally { if (null != out) { out.close(); } if (null != in) { in.close(); } } } } } finally { zipFile.close(); } }
From source file:org.jboss.tools.tycho.sitegenerator.FetchSourcesFromManifests.java
static public void unzipToDirectory(File file, String newPath) throws ZipException, IOException { int BUFFER = 2048; ZipFile zip = new ZipFile(file); (new File(newPath)).mkdirs(); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(newPath, currentEntry); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs();/*from w ww .j av a2 s. c o m*/ if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } zip.close(); }
From source file:com.josue.lottery.eap.service.core.LotoImporter.java
private void readZip(File file) { ZipFile zipFile = null; try {//from w w w. j av a 2s. c o m logger.info("unzipping ************"); zipFile = new ZipFile(file); File dataDir = new File(System.getProperty("jboss.server.data.dir")); File dest = new File(dataDir, "deziped.htm"); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().endsWith(".HTM")) { InputStream stream = zipFile.getInputStream(entry); OutputStream oStr = null; try { oStr = new FileOutputStream(dest); IOUtils.copy(stream, oStr); parseHtml(dest); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(oStr); } } } } catch (IOException ex) { logger.error(ex); } finally { try { zipFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:com.ibm.amc.FileManager.java
public static File decompress(URI temporaryFileUri) { final File destination = new File(getUploadDirectory(), temporaryFileUri.getSchemeSpecificPart()); if (!destination.mkdirs()) { throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR, "CWZBA2001E_DIRECTORY_CREATION_FAILED", destination.getPath());/*from w w w . j a v a 2s. c o m*/ } ZipFile zipFile = null; try { zipFile = new ZipFile(getFileForUri(temporaryFileUri)); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File newDirOrFile = new File(destination, entry.getName()); if (newDirOrFile.getParentFile() != null && !newDirOrFile.getParentFile().exists()) { if (!newDirOrFile.getParentFile().mkdirs()) { throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR, "CWZBA2001E_DIRECTORY_CREATION_FAILED", newDirOrFile.getParentFile().getPath()); } } if (entry.isDirectory()) { if (!newDirOrFile.mkdir()) { throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR, "CWZBA2001E_DIRECTORY_CREATION_FAILED", newDirOrFile.getPath()); } } else { BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); int size; byte[] buffer = new byte[ZIP_BUFFER]; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newDirOrFile), ZIP_BUFFER); while ((size = bis.read(buffer, 0, ZIP_BUFFER)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); bis.close(); } } } catch (Exception e) { throw new AmcRuntimeException(e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { logger.debug("decompress", "close failed with " + e); } } } return destination; }