List of usage examples for java.util.zip ZipFile ZipFile
public ZipFile(File file) throws ZipException, IOException
From source file:com.ibm.util.merge.CompareArchives.java
/** * @param archive1/* w ww. java2 s .c o m*/ * @param archive2 * @throws ZipException * @throws IOException */ public static final void assertZipEquals(String archive1, String archive2) throws ZipException, IOException { // Get Archives ZipFile zipFile1 = new ZipFile(new File(archive1)); ZipFile zipFile2 = new ZipFile(new File(archive2)); // Get Member Hash HashMap<String, ZipEntry> files1 = getMembers(zipFile1); HashMap<String, ZipEntry> files2 = getMembers(zipFile2); // Compare Files assertMembersEqual(zipFile1, files1, zipFile2, files2); }
From source file:org.paxml.bean.UnzipTag.java
public static void unzip(File file, File dir) { dir.mkdirs();/*w w w . j a v a 2s . com*/ ZipFile zipFile = null; try { zipFile = new ZipFile(file); Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { new File(dir, entry.getName()).mkdirs(); continue; } InputStream in = null; OutputStream out = null; try { zipFile.getInputStream(entry); out = new BufferedOutputStream(new FileOutputStream(entry.getName())); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } catch (IOException ioe) { throw new PaxmlRuntimeException( "Cannot unzip file: " + file.getAbsolutePath() + " under dir: " + dir.getAbsolutePath(), ioe); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { // do nothing } } } }
From source file:com.diffplug.gradle.ZipMisc.java
/** * Reads the given entry from the zip.//from ww w . java2s . c o m * * @param input a zip file * @param toRead a path within that zip file * @param reader will be called with an InputStream containing the contents of that entry in the zip file */ public static void read(File input, String toRead, Throwing.Specific.Consumer<InputStream, IOException> reader) throws IOException { try (ZipFile file = new ZipFile(input); InputStream stream = file.getInputStream(file.getEntry(toRead));) { reader.accept(stream); } }
From source file:io.apigee.buildTools.enterprise4g.utils.ZipUtils.java
public void unzipArchive(File archive, File outputDir) { try {//from w ww . ja v a 2s . c om ZipFile zipfile = new ZipFile(archive); for (Enumeration e = zipfile.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); unzipEntry(zipfile, entry, outputDir); } } catch (Exception e) { log.error("Error while extracting file " + archive, e); } }
From source file:com.freedomotic.util.Unzip.java
/** * * @param zipFile/*from w ww . ja v a2 s .c om*/ * @throws ZipException * @throws IOException */ public static void unzip(String zipFile) throws IOException { if (StringUtils.isEmpty(zipFile)) { LOG.error("File path not provided, no unzipping performed"); return; } File file = new File(zipFile); if (!file.exists()) { LOG.error("File not existing, no unzipping performed"); return; } try (ZipFile zip = new ZipFile(file);) { String newPath = zipFile.substring(0, zipFile.length() - 4); //simulates the unzip here feature newPath = newPath.substring(0, newPath.lastIndexOf(File.separator)); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = 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(); if (!entry.isDirectory()) { try (BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);) { int currentByte; // establish buffer for writing file byte[] data = new byte[BUFFER]; // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } } catch (IOException ex) { LOG.error(Freedomotic.getStackTraceInfo(ex)); } } if (currentEntry.endsWith(".zip")) { // found a zip file, try to open unzip(destFile.getAbsolutePath()); } } } }
From source file:com.shazam.fork.model.InstrumentationInfo.java
/** * Parse key information from an instrumentation APK's manifest. * @param apkTestFile the instrumentation APK * @return the instrumentation info instance *//*w w w. ja va 2 s.com*/ @Nonnull public static InstrumentationInfo parseFromFile(File apkTestFile) { InputStream is = null; try { ZipFile zip = new ZipFile(apkTestFile); ZipEntry entry = zip.getEntry("AndroidManifest.xml"); is = zip.getInputStream(entry); AXMLParser parser = new AXMLParser(is); int eventType = parser.getType(); String appPackage = null; String testPackage = null; String testRunnerClass = null; while (eventType != AXMLParser.END_DOCUMENT) { if (eventType == AXMLParser.START_TAG) { String parserName = parser.getName(); boolean isManifest = "manifest".equals(parserName); boolean isInstrumentation = "instrumentation".equals(parserName); if (isManifest || isInstrumentation) { for (int i = 0; i < parser.getAttributeCount(); i++) { String parserAttributeName = parser.getAttributeName(i); if (isManifest && "package".equals(parserAttributeName)) { testPackage = parser.getAttributeValueString(i); } else if (isInstrumentation && "targetPackage".equals(parserAttributeName)) { appPackage = parser.getAttributeValueString(i); } else if (isInstrumentation && "name".equals(parserAttributeName)) { testRunnerClass = parser.getAttributeValueString(i); } } } } eventType = parser.next(); } checkNotNull(testPackage, "Could not find test application package."); checkNotNull(appPackage, "Could not find application package."); checkNotNull(testRunnerClass, "Could not find test runner class."); // Support relative declaration of instrumentation test runner. if (testRunnerClass.startsWith(".")) { testRunnerClass = testPackage + testRunnerClass; } else if (!testRunnerClass.contains(".")) { testRunnerClass = testPackage + "." + testRunnerClass; } return new InstrumentationInfo(appPackage, testPackage, testRunnerClass); } catch (IOException e) { throw new RuntimeException("Unable to parse test app AndroidManifest.xml.", e); } finally { IOUtils.closeQuietly(is); } }
From source file:Main.java
private static String getChannelFromApk(Context context, String channelKey) { ApplicationInfo appinfo = context.getApplicationInfo(); String sourceDir = appinfo.sourceDir; String key = "META-INF/" + channelKey; String ret = ""; ZipFile zipfile = null;/*from w w w. jav a 2 s . co m*/ try { zipfile = new ZipFile(sourceDir); Enumeration<?> entries = zipfile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = ((ZipEntry) entries.nextElement()); String entryName = entry.getName(); if (entryName.startsWith(key)) { ret = entryName; break; } } } catch (IOException e) { e.printStackTrace(); } finally { if (zipfile != null) { try { zipfile.close(); } catch (IOException e) { e.printStackTrace(); } } } String[] split = ret.split("_"); String channel = ""; if (split != null && split.length >= 2) { channel = ret.substring(split[0].length() + 1); } return channel; }
From source file:cc.recommenders.io.ReadingArchive.java
public ReadingArchive(File file) { Asserts.assertTrue(file.exists());//from w ww . j a v a 2s . com Asserts.assertTrue(file.isFile()); try { zipFile = new ZipFile(file); entries = zipFile.entries(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java
/** * Extracts the specified folder from the specified archive, into the supplied output directory. * /*from ww w. j a v a 2 s . c o m*/ * @param archivePath * the archive Path * @param folderToExtract * the folder to extract * @param outputDirectory * the output directory * @return the extracted folder path * @throws IOException * if a problem occurs while extracting */ public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract, String outputDirectory) throws IOException { File destinationFolder = new File(outputDirectory); destinationFolder.mkdirs(); ZipFile zip = null; try { zip = new ZipFile(new File(archivePath)); Enumeration<?> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); if (currentEntry.startsWith(folderToExtract)) { File destFile = new File(destinationFolder, currentEntry); destFile.getParentFile().mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER_SIZE]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER_SIZE); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, currentByte); } } finally { if (dest != null) { dest.flush(); } IOUtils.closeQuietly(dest); IOUtils.closeQuietly(is); } } } } } finally { if (zip != null) { zip.close(); } } return new File(destinationFolder, folderToExtract); }
From source file:example.csv.PostalReader.java
@Override public void open(Serializable checkpoint) throws Exception { ZipFile zipFile = new ZipFile(ZIP_PATH.toFile()); ZipEntry csvEntry = zipFile.getEntry("42NAGASA.CSV"); InputStreamReader reader = new InputStreamReader(zipFile.getInputStream(csvEntry), Charset.forName("Windows-31J")); parser = CSVFormat.RFC4180.withHeader(HEADER).parse(reader); csvIter = parser.iterator();//from w ww . j a v a2s.c o m }