List of usage examples for java.util.zip ZipFile close
public void close() throws IOException
From source file:org.eclipse.mylyn.internal.context.core.InteractionContextExternalizer.java
static String getFirstContextHandle(File sourceFile) throws CoreException { try {//from www. j a va 2 s.c om ZipFile zipFile = new ZipFile(sourceFile); try { for (Enumeration<?> e = zipFile.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); String name = entry.getName(); if (name.endsWith(InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD)) { try { String decodedName = URLDecoder.decode(name, InteractionContextManager.CONTEXT_FILENAME_ENCODING); if (decodedName.length() > InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD .length()) { return decodedName.substring(0, decodedName.length() - InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD.length()); } } catch (IllegalArgumentException ignored) { // not a valid context entry } } } return null; } finally { zipFile.close(); } } catch (IOException e) { throw new CoreException(new Status(IStatus.ERROR, ContextCorePlugin.ID_PLUGIN, "Could not get context handle from " + sourceFile, e)); //$NON-NLS-1$ } }
From source file:com.squareup.wire.schema.internal.parser.RpcMethodScanner.java
private List<RpcMethodDefinition> searchJar(String jarpath, String serviceName) throws IOException { List<RpcMethodDefinition> defs = new ArrayList<>(); File jarFile = new File(jarpath); if (!jarFile.exists() || jarFile.isDirectory()) { return defs; }/*from ww w.j a v a 2 s .c om*/ ZipInputStream zip = new ZipInputStream(new FileInputStream(jarFile)); ZipEntry ze; while ((ze = zip.getNextEntry()) != null) { String entryName = ze.getName(); if (entryName.endsWith(".proto")) { ZipFile zipFile = new ZipFile(jarFile); try (InputStream in = zipFile.getInputStream(ze)) { defs.addAll(inspectProtoFile(in, serviceName)); if (!defs.isEmpty()) { zipFile.close(); break; } } zipFile.close(); } } zip.close(); return defs; }
From source file:org.dita2indesign.cmdline.Docx2Xml.java
/** * @param zipInFile//from ww w .ja v a 2 s . c om * Zip file to be unzipped * @param outputDir * Directory to which the zipped files will be unpacked. * @throws Exception * @throws ZipException */ public static void unzip(File zipInFile, File outputDir) throws Exception { Enumeration<? extends ZipEntry> entries; ZipFile zipFile = new ZipFile(zipInFile); ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipInFile)); ZipEntry entry = (ZipEntry) zipInputStream.getNextEntry(); File curOutDir = outputDir; while (entry != null) { if (entry.isDirectory()) { // This is not robust, just for demonstration purposes. curOutDir = new File(curOutDir, entry.getName()); curOutDir.mkdirs(); continue; } File outFile = new File(curOutDir, entry.getName()); File tempDir = outFile.getParentFile(); if (!tempDir.exists()) tempDir.mkdirs(); outFile.createNewFile(); BufferedOutputStream outstream = new BufferedOutputStream(new FileOutputStream(outFile)); int n; byte[] buf = new byte[1024]; while ((n = zipInputStream.read(buf, 0, 1024)) > -1) outstream.write(buf, 0, n); outstream.flush(); outstream.close(); zipInputStream.closeEntry(); entry = zipInputStream.getNextEntry(); } zipInputStream.close(); zipFile.close(); }
From source file:org.apache.oozie.tools.OozieDBImportCLI.java
private static void importAllDBTables(String zipFileName) throws StoreException, IOException, JPAExecutorException { EntityManager entityManager = null;//from w w w .j a va 2s . co m ZipFile zipFile = null; try { entityManager = Services.get().get(JPAService.class).getEntityManager(); entityManager.setFlushMode(FlushModeType.COMMIT); zipFile = new ZipFile(zipFileName); checkDBVersion(entityManager, zipFile); importFrom(entityManager, zipFile, "WF_JOBS", WorkflowJobBean.class, OOZIEDB_WF_JSON); importFrom(entityManager, zipFile, "WF_ACTIONS", WorkflowActionBean.class, OOZIEDB_AC_JSON); importFrom(entityManager, zipFile, "COORD_JOBS", CoordinatorJobBean.class, OOZIEDB_CJ_JSON); importFrom(entityManager, zipFile, "COORD_ACTIONS", CoordinatorActionBean.class, OOZIEDB_CA_JSON); importFrom(entityManager, zipFile, "BUNDLE_JOBS", BundleJobBean.class, OOZIEDB_BNJ_JSON); importFrom(entityManager, zipFile, "BUNDLE_ACTIONS", BundleActionBean.class, OOZIEDB_BNA_JSON); importFrom(entityManager, zipFile, "SLA_REGISTRATION", SLARegistrationBean.class, OOZIEDB_SLAREG_JSON); importFrom(entityManager, zipFile, "SLA_SUMMARY", SLASummaryBean.class, OOZIEDB_SLASUM_JSON); } finally { if (entityManager != null) { entityManager.close(); } if (zipFile != null) { zipFile.close(); } } }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * Inflate the provided {@link ZipFile} in the provided output directory. * /*w ww .ja v a 2s. c o m*/ * @param archive * the {@link ZipFile} to inflate. * @param outputDirectory * the directory where to inflate the archive. * @throws IOException * in case something bad happens. * @throws FileNotFoundException * in case something bad happens. */ public static void inflate(ZipFile archive, File outputDirectory, String fileName) throws IOException, FileNotFoundException { final Enumeration<? extends ZipEntry> entries = archive.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (!entry.isDirectory()) { final String name = entry.getName(); final String ext = FilenameUtils.getExtension(name); final InputStream in = new BufferedInputStream(archive.getInputStream(entry)); final File outFile = new File(outputDirectory, fileName != null ? new StringBuilder(fileName).append(".").append(ext).toString() : name); final OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); IOUtils.copyStream(in, out, true, true); } } } finally { try { archive.close(); } catch (Throwable e) { if (LOGGER.isTraceEnabled()) LOGGER.error("unable to close archive.\nMessage is: " + e.getMessage(), e); } } }
From source file:com.asakusafw.bulkloader.testutil.UnitTestUtil.java
/** * Create file list from Zip file.// w ww .j a va2 s . c o m * @param originalZipFile source Zip file * @param targetFileList target file list file */ public static void createFileList(File originalZipFile, File targetFileList) throws IOException { ZipFile zip = new ZipFile(originalZipFile); try { FileOutputStream output = new FileOutputStream(targetFileList); try { FileList.Writer writer = FileList.createWriter(output, true); Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry next = entries.nextElement(); InputStream input = zip.getInputStream(next); try { OutputStream target = writer.openNext(FileList.content(next.getName().replace('\\', '/'))); try { IOUtils.pipingAndClose(input, target); } finally { target.close(); } } finally { input.close(); } } writer.close(); } finally { output.close(); } } finally { zip.close(); } }
From source file:org.apache.maven.archetype.common.DefaultArchetypeArtifactManager.java
private void closeZipFile(ZipFile zipFile) { try {/* w ww . j a v a2s . c om*/ zipFile.close(); } catch (Exception e) { getLogger().error("Failed to close " + zipFile.getName() + " zipFile."); } }
From source file:org.dbgl.util.FileUtils.java
public static long extractZipSizeInBytes(final File archive, final File dirToBeExtracted) throws IOException { long bytes = 0; ZipFile zf = new ZipFile(archive); for (Enumeration<? extends ZipEntry> entries = zf.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); if (areRelated(dirToBeExtracted, new File(entry.getName()))) bytes += entry.getSize();/*w w w.j a va 2 s .co m*/ } zf.close(); return bytes; }
From source file:org.roda.common.certification.ODFSignatureUtils.java
public static void runDigitalSignatureStrip(Path input, Path output) throws IOException { OutputStream os = new FileOutputStream(output.toFile()); BufferedOutputStream bos = new BufferedOutputStream(os); ZipOutputStream zout = new ZipOutputStream(bos); ZipFile zipFile = new ZipFile(input.toString()); Enumeration<?> enumeration; for (enumeration = zipFile.entries(); enumeration.hasMoreElements();) { ZipEntry entry = (ZipEntry) enumeration.nextElement(); String entryName = entry.getName(); if (!META_INF_DOCUMENTSIGNATURES_XML.equalsIgnoreCase(entryName) && entry.getSize() > 0) { InputStream zipStream = zipFile.getInputStream(entry); ZipEntry destEntry = new ZipEntry(entryName); zout.putNextEntry(destEntry); byte[] data = new byte[(int) entry.getSize()]; while ((zipStream.read(data, 0, (int) entry.getSize())) != -1) { }//from www. j a v a2 s. co m zout.write(data); zout.closeEntry(); IOUtils.closeQuietly(zipStream); } } IOUtils.closeQuietly(zout); IOUtils.closeQuietly(bos); IOUtils.closeQuietly(os); zipFile.close(); }
From source file:com.sshtools.j2ssh.authentication.UserGridCredential.java
private static void checkCACertificates(CoGProperties cogproperties) throws IOException { // check the directories exist and create if they don't String globusDir = System.getProperty("user.home") + "/.globus"; if (!(new File(globusDir).exists())) { boolean success = (new File(globusDir).mkdir()); if (!success) { throw new IOException("Couldn't create directory: " + globusDir); }/*from www .j a v a2 s. c om*/ } String caCertLocations = globusDir + "/certificates"; File caCertLocationsF = new File(caCertLocations); if (!caCertLocationsF.exists()) { boolean success = (new File(caCertLocations).mkdir()); if (!success) { throw new IOException("Couldn't create directory: " + caCertLocations); } } if (!caCertLocationsF.isDirectory()) { throw new IOException("Location: " + caCertLocations + " is not a directory"); } File tmp = null; try { // save the zipfile temporarily tmp = File.createTempFile("certificates", ".zip"); copyFile(thisDummy.getClass().getResourceAsStream("certificates.zip"), new FileOutputStream(tmp)); ZipFile zf = new ZipFile(tmp); try { Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry ze = (ZipEntry) e.nextElement(); String name = ze.getName(); if (!(new File(caCertLocations + File.separator + name).exists())) { copyFile(zf.getInputStream(ze), new FileOutputStream(new File(caCertLocations + File.separator + name))); } } } finally { if (zf != null) zf.close(); } } catch (IOException e) { throw new IOException("Couldn't load certificates... " + e); } finally { // delete temp file if (tmp != null) { if (tmp.exists()) tmp.delete(); } } }