List of usage examples for java.util.zip ZipFile getInputStream
public InputStream getInputStream(ZipEntry entry) throws IOException
From source file:de.tudarmstadt.ukp.clarin.webanno.brat.dao.DaoUtilsTest.java
@SuppressWarnings("resource") @Test//from www . j a v a 2 s .c o m public void testZipFolder() { File toBeZippedFiles = new File("src/test/resources"); File zipedFiles = new File("target/" + toBeZippedFiles.getName() + ".zip"); try { ZipUtils.zipFolder(toBeZippedFiles, zipedFiles); } catch (Exception e) { LOG.info("Zipping fails" + ExceptionUtils.getRootCauseMessage(e)); } FileInputStream fs; try { fs = new FileInputStream(zipedFiles); ZipInputStream zis = new ZipInputStream(fs); ZipEntry zE; while ((zE = zis.getNextEntry()) != null) { for (File toBeZippedFile : toBeZippedFiles.listFiles()) { if ((FilenameUtils.getBaseName(toBeZippedFile.getName())) .equals(FilenameUtils.getBaseName(zE.getName()))) { // Extract the zip file, save it to temp and compare ZipFile zipFile = new ZipFile(zipedFiles); ZipEntry zipEntry = zipFile.getEntry(zE.getName()); InputStream is = zipFile.getInputStream(zipEntry); File temp = File.createTempFile("temp", ".txt"); OutputStream out = new FileOutputStream(temp); IOUtils.copy(is, out); FileUtils.contentEquals(toBeZippedFile, temp); } } zis.closeEntry(); } } catch (FileNotFoundException e) { LOG.info("zipped file not found " + ExceptionUtils.getRootCauseMessage(e)); } catch (IOException e) { LOG.info("Unable to get file " + ExceptionUtils.getRootCauseMessage(e)); } }
From source file:org.formic.util.Extractor.java
/** * Extracts the specified archive to the supplied destination. * * @param archive The archive to extract. * @param dest The directory to extract it to. *///from w w w . j a v a2s . co m public static void extractArchive(File archive, File dest, ArchiveExtractionListener listener) throws IOException { ZipFile file = null; try { file = new ZipFile(archive); if (listener != null) { listener.startExtraction(file.size()); } Enumeration entries = file.entries(); for (int ii = 0; entries.hasMoreElements(); ii++) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (!entry.isDirectory()) { // create parent directories if necessary. String name = dest + "/" + entry.getName(); if (name.indexOf('/') != -1) { File dir = new File(name.substring(0, name.lastIndexOf('/'))); if (!dir.exists()) { dir.mkdirs(); } } if (listener != null) { listener.startExtractingFile(ii, entry.getName()); } FileOutputStream out = new FileOutputStream(name); InputStream in = file.getInputStream(entry); IOUtils.copy(in, out); in.close(); out.close(); if (listener != null) { listener.finishExtractingFile(ii, entry.getName()); } } } } finally { try { file.close(); } catch (Exception ignore) { } } if (listener != null) { listener.finishExtraction(); } }
From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java
public static void unZipAll(File source, File destination) throws IOException { System.out.println("Unzipping - " + source.getName()); int BUFFER = 2048; ZipFile zip = new ZipFile(source); try {/* w ww . j a v a 2s . co m*/ destination.getParentFile().mkdirs(); Enumeration 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(destination, currentEntry); //destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = null; FileOutputStream fos = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk fos = new FileOutputStream(destFile); 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); } } catch (Exception e) { System.out.println("unable to extract entry:" + entry.getName()); throw e; } finally { if (dest != null) { dest.close(); } if (fos != null) { fos.close(); } if (is != null) { is.close(); } } } else { //Create directory destFile.mkdirs(); } if (currentEntry.endsWith(".zip")) { // found a zip file, try to extract unZipAll(destFile, destinationParent); if (!destFile.delete()) { System.out.println("Could not delete zip"); } } } } catch (Exception e) { e.printStackTrace(); System.out.println("Failed to successfully unzip:" + source.getName()); } finally { zip.close(); } System.out.println("Done Unzipping:" + source.getName()); }
From source file:org.gradle.api.internal.changedetection.state.JvmClassHasher.java
private void visit(ZipFile zipFile, ZipEntry zipEntry, Hasher hasher) { InputStream inputStream = null; try {/*from ww w . j av a 2s .c o m*/ inputStream = zipFile.getInputStream(zipEntry); byte[] src = ByteStreams.toByteArray(inputStream); hashClassBytes(hasher, src); } catch (Exception e) { throw new UncheckedIOException("Could not calculate the signature for class file " + zipEntry.getName(), e); } finally { IOUtils.closeQuietly(inputStream); } }
From source file:com.gisgraphy.importer.ImporterHelper.java
/** * unzip a file in the same directory as the zipped file * /*from w w w.ja v a 2 s . c o m*/ * @param file * The file to unzip */ public static void unzipFile(File file) { logger.info("will Extracting file: " + file.getName()); Enumeration<? extends ZipEntry> entries; ZipFile zipFile; try { zipFile = new ZipFile(file); entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { // Assume directories are stored parents first then // children. (new File(entry.getName())).mkdir(); continue; } logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator + entry.getName()); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(file.getParent() + File.separator + entry.getName()))); } zipFile.close(); } catch (IOException e) { logger.error("can not unzip " + file.getName() + " : " + e.getMessage(), e); throw new ImporterException(e); } }
From source file:org.apache.taverna.scufl2.rdfxml.TestResourcesInZip.java
@Test public void singleFile() throws Exception { UCFPackage resources = originalBundle.getResources(); assertEquals(APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE, resources.getPackageMediaType()); resources.addResource("Hello there", "hello.txt", "text/plain"); File bundleFile = tempFile(); bundleIO.writeBundle(originalBundle, bundleFile, APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE); assertEquals(APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE, resources.getPackageMediaType()); assertEquals(1, resources.getRootFiles().size()); assertEquals("workflowBundle.rdf", resources.getRootFiles().get(0).getPath()); ZipFile zipFile = new ZipFile(bundleFile); ZipEntry hello = zipFile.getEntry("hello.txt"); assertEquals("hello.txt", hello.getName()); assertEquals("Hello there", IOUtils.toString(zipFile.getInputStream(hello), "ASCII")); }
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 w ww. j av a 2 s . co m*/ 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.apache.taverna.scufl2.rdfxml.TestResourcesInZip.java
@Test public void differentMediaType() throws Exception { UCFPackage resources = originalBundle.getResources(); resources.setPackageMediaType("application/x-something-else"); assertEquals("application/x-something-else", resources.getPackageMediaType()); resources.addResource("Hello there", "hello.txt", "text/plain"); File bundleFile = tempFile(); bundleIO.writeBundle(originalBundle, bundleFile, APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE); assertEquals("application/x-something-else", resources.getPackageMediaType()); assertEquals(0, resources.getRootFiles().size()); // RDFXMLWriter does not touch the rootFile or media type if it's non-null ZipFile zipFile = new ZipFile(bundleFile); ZipEntry hello = zipFile.getEntry("hello.txt"); assertEquals("hello.txt", hello.getName()); assertEquals("Hello there", IOUtils.toString(zipFile.getInputStream(hello), "ASCII")); }
From source file:org.geoserver.rest.util.IOUtils.java
/** * Inflate the provided {@link ZipFile} in the provided output directory. * /*from ww w . j a v a 2s. c om*/ * @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); } else { //if the entry is a directory attempt to make it new File(outputDirectory, entry.getName()).mkdirs(); } } } finally { try { archive.close(); } catch (Throwable e) { if (LOGGER.isLoggable(Level.FINE)) LOGGER.isLoggable(Level.FINE); } } }
From source file:com.android.tradefed.util.FileUtil.java
/** * Utility method to extract one specific file from zip file into a tmp file * * @param zipFile//from www . j av a2 s . c o m * the {@link ZipFile} to extract * @param filePath * the filePath of to extract * @throws IOException * if failed to extract file * @return the {@link File} or null if not found */ public static File extractFileFromZip(ZipFile zipFile, String filePath) throws IOException { ZipEntry entry = zipFile.getEntry(filePath); if (entry == null) { return null; } File createdFile = FileUtil.createTempFile("extracted", FileUtil.getExtension(filePath)); FileUtil.writeToFile(zipFile.getInputStream(entry), createdFile); return createdFile; }