List of usage examples for java.util.zip ZipFile getEntry
public ZipEntry getEntry(String name)
From source file:io.inkstand.scribble.rules.ZipAssert.java
/** * Verifies the zip file contains an entry with the specified character content * @param zf/*from www .ja v a 2s . c o m*/ * the zip file to be searched * @param entryPath * the path to the entry to be verified, the path must not start with a '/' * @param expectedContent * the content as a string that is expected to be the content of the file. * @throws java.io.IOException */ public static void assertZipContent(final ZipFile zf, final String entryPath, final String expectedContent) throws IOException { final ZipEntry entry = zf.getEntry(entryPath); assertNotNull("Entry " + entryPath + " does not exist", entry); assertFalse("Entry " + entryPath + " is a directory", entry.isDirectory()); try (InputStream is = zf.getInputStream(entry)) { assertNotNull("Entry " + entryPath + " has no content", is); final String actualContent = IOUtils.toString(is); assertEquals(expectedContent, actualContent); } }
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 ww .ja va2 s .co m*/ @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:org.openoffice.maven.packager.OxtMojoTest.java
private static void checkArchive() throws ZipException, IOException { assertTrue(OXT_FILE + " is not a file", OXT_FILE.isFile()); ZipFile zip = new ZipFile(OXT_FILE); ZipEntry entry = zip.getEntry("META-INF/manifest.xml"); assertNotNull("no manifest inside", entry); zip.close();//www .j a va2s.co m }
From source file:org.kontalk.model.Account.java
private static byte[] readBytesFromZip(ZipFile zipFile, String filename) throws KonException { ZipEntry zipEntry = zipFile.getEntry(filename); byte[] bytes = null; try {//from ww w. j av a2s .c o m bytes = IOUtils.toByteArray(zipFile.getInputStream(zipEntry)); } catch (IOException ex) { LOGGER.warning("can't read key file from archive: " + ex.getLocalizedMessage()); throw new KonException(KonException.Error.IMPORT_READ_FILE, ex); } return bytes; }
From source file:net.technicpack.utilslib.ZipUtils.java
public static boolean extractFile(File zip, File output, String fileName) throws IOException, InterruptedException { if (!zip.exists() || fileName == null) { return false; }// w w w . ja va 2 s. co m ZipFile zipFile = new ZipFile(zip); try { ZipEntry entry = zipFile.getEntry(fileName); if (entry == null) { Utils.getLogger().log(Level.WARNING, "File " + fileName + " not found in " + zip.getAbsolutePath()); return false; } File outputFile = new File(output, entry.getName()); if (outputFile.getParentFile() != null) { outputFile.getParentFile().mkdirs(); } unzipEntry(zipFile, zipFile.getEntry(fileName), outputFile); return true; } catch (IOException e) { Utils.getLogger().log(Level.WARNING, "Error extracting file " + fileName + " from " + zip.getAbsolutePath()); return false; } finally { zipFile.close(); } }
From source file:com.oneis.app.FileController.java
public static ZipFileExtractionResults zipFileExtraction(String zipFilePathname, String contentsDefault, String contentsRequested) throws IOException { ZipFileExtractionResults results = new ZipFileExtractionResults(); ZipFile zipFile = new ZipFile(zipFilePathname); try {/*w w w .j a v a2s. c om*/ // Find the right entry, trying for the requested filename by defaulting back on the default given ZipEntry e = zipFile.getEntry(contentsRequested); results.chosenFile = contentsRequested; if (e == null) { e = zipFile.getEntry(contentsDefault); results.chosenFile = contentsDefault; } if (e == null) { return null; } results.data = IOUtils.toByteArray(zipFile.getInputStream(e)); } finally { zipFile.close(); } results.etagSuggestion = results.chosenFile.hashCode() & 0xfffff; return results; }
From source file:org.openspaces.core.util.FileUtils.java
public static byte[] unzipFileToMemory(String applicationFile, File directoryOrZip, long maxFileSizeInBytes) { ZipFile zipFile = null; try {/* w w w . j a v a 2s . c o m*/ zipFile = new ZipFile(directoryOrZip); final ZipEntry zipEntry = zipFile.getEntry(applicationFile); if (zipEntry == null) { throw new RuntimeException("Failed to load " + applicationFile + " from " + directoryOrZip); } final int length = (int) zipEntry.getSize(); if (length > maxFileSizeInBytes) { throw new RuntimeException( applicationFile + " file size cannot be bigger than " + maxFileSizeInBytes + " bytes"); } byte[] buffer = new byte[length]; final InputStream in = zipFile.getInputStream(zipEntry); final DataInputStream din = new DataInputStream(in); try { din.readFully(buffer, 0, length); return buffer; } catch (final IOException e) { throw new RuntimeException("Failed to read application file input stream", e); } } catch (final IOException e) { throw new RuntimeException("Failed to read zip file " + directoryOrZip, e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (final IOException e) { logger.debug("Failed to close zip file " + directoryOrZip, e); } } } }
From source file:org.openoffice.maven.packager.OxtMojoTest.java
private static String getManifestContent() throws ZipException, IOException { ZipFile zip = new ZipFile(OXT_FILE); try {/*from ww w. java2 s . com*/ ZipEntry entry = zip.getEntry("META-INF/manifest.xml"); InputStream istream = zip.getInputStream(entry); return IOUtils.toString(istream); } finally { zip.close(); } }
From source file:net.fabricmc.installer.installer.MultiMCInstaller.java
public static void install(File mcDir, String version, IInstallerProgress progress) throws Exception { File instancesDir = new File(mcDir, "instances"); if (!instancesDir.exists()) { throw new FileNotFoundException(Translator.getString("install.multimc.notFound")); }//from w w w. j a v a 2 s .c o m progress.updateProgress(Translator.getString("install.multimc.findInstances"), 10); String mcVer = version.split("-")[0]; List<File> validInstances = new ArrayList<>(); for (File instanceDir : instancesDir.listFiles()) { if (instanceDir.isDirectory()) { if (isValidInstance(instanceDir, mcVer)) { validInstances.add(instanceDir); } } } if (validInstances.isEmpty()) { throw new Exception(Translator.getString("install.multimc.noInstances").replace("[MCVER]", mcVer)); } List<String> instanceNames = new ArrayList<>(); for (File instance : validInstances) { instanceNames.add(instance.getName()); } String instanceName = (String) JOptionPane.showInputDialog(null, Translator.getString("install.multimc.selectInstance"), Translator.getString("install.multimc.selectInstance"), JOptionPane.QUESTION_MESSAGE, null, instanceNames.toArray(), instanceNames.get(0)); if (instanceName == null) { progress.updateProgress(Translator.getString("install.multimc.canceled"), 100); return; } progress.updateProgress( Translator.getString("install.multimc.installingInto").replace("[NAME]", instanceName), 25); File instnaceDir = null; for (File instance : validInstances) { if (instance.getName().equals(instanceName)) { instnaceDir = instance; } } if (instnaceDir == null) { throw new FileNotFoundException("Could not find " + instanceName); } File patchesDir = new File(instnaceDir, "patches"); if (!patchesDir.exists()) { patchesDir.mkdir(); } File fabricJar = new File(patchesDir, "Fabric-" + version + ".jar"); if (!fabricJar.exists()) { progress.updateProgress(Translator.getString("install.client.downloadFabric"), 30); FileUtils.copyURLToFile(new URL("http://maven.modmuss50.me/net/fabricmc/fabric-base/" + version + "/fabric-base-" + version + ".jar"), fabricJar); } progress.updateProgress(Translator.getString("install.multimc.createJson"), 70); File fabricJson = new File(patchesDir, "fabric.json"); if (fabricJson.exists()) { fabricJson.delete(); } String json = readBaseJson(); json = json.replaceAll("%VERSION%", version); ZipFile fabricZip = new ZipFile(fabricJar); ZipEntry dependenciesEntry = fabricZip.getEntry("dependencies.json"); String fabricDeps = IOUtils.toString(fabricZip.getInputStream(dependenciesEntry), Charset.defaultCharset()); json = json.replace("%DEPS%", stripDepsJson(fabricDeps.replace("\n", ""))); FileUtils.writeStringToFile(fabricJson, json, Charset.defaultCharset()); fabricZip.close(); progress.updateProgress(Translator.getString("install.success"), 100); }
From source file:com.meltmedia.cadmium.deployer.JBossUtil.java
public static boolean isCadmiumWar(File file, Logger log) { if (file.isDirectory()) { return new File(file, "WEB-INF/cadmium.properties").exists(); } else if (file.exists()) { ZipFile zip = null; try {/*from w ww . j a va 2 s . c o m*/ zip = new ZipFile(file); ZipEntry cadmiumProps = zip.getEntry("WEB-INF/cadmium.properties"); return cadmiumProps != null; } catch (Exception e) { log.warn("Failed to read war file " + file, e); } finally { if (zip != null) { try { zip.close(); } catch (IOException e) { //Not much we can do about this. } } } } return false; }