List of usage examples for java.util.zip ZipEntry getMethod
public int getMethod()
From source file:com.taobao.android.utils.ZipUtils.java
/** * zip//www .j a v a 2s. c om * * @param zipFile * @param file * @param destPath ? * @param overwrite ? * @throws IOException */ public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath, boolean overwrite) throws IOException { byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile)); ZipEntry entry = zin.getNextEntry(); boolean addFile = true; while (entry != null) { boolean addEntry = true; String name = entry.getName(); if (StringUtils.equalsIgnoreCase(name, destPath)) { if (overwrite) { addEntry = false; } else { addFile = false; } } if (addEntry) { ZipEntry zipEntry = null; if (ZipEntry.STORED == entry.getMethod()) { zipEntry = new ZipEntry(entry); } else { zipEntry = new ZipEntry(name); } out.putNextEntry(zipEntry); int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } if (addFile) { InputStream in = new FileInputStream(file); // Add ZIP entry to output stream. ZipEntry zipEntry = new ZipEntry(destPath); out.putNextEntry(zipEntry); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Close the streams zin.close(); out.close(); }
From source file:JarResources.java
private String dumpZipEntry(ZipEntry ze) { StringBuffer sb = new StringBuffer(); if (ze.isDirectory()) { sb.append("d "); } else {/* w ww. jav a2s . co m*/ sb.append("f "); } if (ze.getMethod() == ZipEntry.STORED) { sb.append("stored "); } else { sb.append("defalted "); } sb.append(ze.getName()); sb.append("\t"); sb.append("" + ze.getSize()); if (ze.getMethod() == ZipEntry.DEFLATED) { sb.append("/" + ze.getCompressedSize()); } return (sb.toString()); }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * zip/* w w w .jav a 2 s . c o m*/ * * @param zipFile * @param file * @param destPath * @param overwrite ? * @throws java.io.IOException */ public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath, boolean overwrite) throws IOException { byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile)); ZipEntry entry = zin.getNextEntry(); boolean addFile = true; while (entry != null) { boolean addEntry = true; String name = entry.getName(); if (StringUtils.equalsIgnoreCase(name, destPath)) { if (overwrite) { addEntry = false; } else { addFile = false; } } if (addEntry) { ZipEntry zipEntry = null; if (STORED == entry.getMethod()) { zipEntry = new ZipEntry(entry); } else { zipEntry = new ZipEntry(name); } out.putNextEntry(zipEntry); int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } if (addFile) { InputStream in = new FileInputStream(file); // Add ZIP entry to output stream. ZipEntry zipEntry = new ZipEntry(destPath); out.putNextEntry(zipEntry); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Close the streams zin.close(); out.close(); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * 2jar?,??jar//from w w w . ja v a2 s. c o m * @param baseJar * @param diffJar * @param outJar */ public static void unionJar(File baseJar, File diffJar, File outJar) throws IOException { File outParentFolder = outJar.getParentFile(); if (!outParentFolder.exists()) { outParentFolder.mkdirs(); } List<String> diffEntries = getZipEntries(diffJar); FileOutputStream fos = new FileOutputStream(outJar); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(baseJar); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } String name = entry.getName(); if (diffEntries.contains(name)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException { if (outputFile.exists()) { FileUtils.deleteQuietly(outputFile); }/*from w w w.j a v a 2s .c om*/ if (null == includeEnties || includeEnties.size() < 1) { return; } FileOutputStream fos = new FileOutputStream(outputFile); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(inputFile); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } String name = entry.getName(); if (!includeEnties.contains(name)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * jarclass/* www . j a v a 2 s . co m*/ * @param inJar * @param removeClasses */ public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException { if (null == removeClasses || removeClasses.isEmpty()) { return; } File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp"); File outParentFolder = outJar.getParentFile(); if (!outParentFolder.exists()) { outParentFolder.mkdirs(); } FileOutputStream fos = new FileOutputStream(outJar); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(inJar); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory() || !entry.getName().endsWith(".class")) { continue; } String name = entry.getName(); String className = getClassName(name); if (removeClasses.contains(className)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); FileUtils.deleteQuietly(inJar); FileUtils.moveFile(outJar, inJar); }
From source file:com.jayway.maven.plugins.android.phase09package.ApkMojo.java
private void updateWithMetaInf(ZipOutputStream zos, File jarFile, Set<String> entries, boolean metaInfOnly) throws IOException { ZipFile zin = new ZipFile(jarFile); for (Enumeration<? extends ZipEntry> en = zin.entries(); en.hasMoreElements();) { ZipEntry ze = en.nextElement(); if (ze.isDirectory()) { continue; }/* w w w . j a va2s . c om*/ String zn = ze.getName(); if (metaInfOnly) { if (!zn.startsWith("META-INF/")) { continue; } if (this.extractDuplicates && !entries.add(zn)) { continue; } if (!this.apkMetaInf.isIncluded(zn)) { continue; } } final ZipEntry ne; if (ze.getMethod() == ZipEntry.STORED) { ne = new ZipEntry(ze); } else { ne = new ZipEntry(zn); } zos.putNextEntry(ne); InputStream is = zin.getInputStream(ze); copyStreamWithoutClosing(is, zos); is.close(); zos.closeEntry(); } zin.close(); }
From source file:com.simpligility.maven.plugins.android.phase09package.ApkMojo.java
private void updateWithMetaInf(ZipOutputStream zos, File jarFile, Set<String> entries, boolean metaInfOnly) throws IOException { ZipFile zin = new ZipFile(jarFile); for (Enumeration<? extends ZipEntry> en = zin.entries(); en.hasMoreElements();) { ZipEntry ze = en.nextElement(); if (ze.isDirectory()) { continue; }//from www .j av a 2s . c o m String zn = ze.getName(); if (metaInfOnly) { if (!zn.startsWith("META-INF/")) { continue; } if (!this.apkMetaInf.isIncluded(zn)) { continue; } } boolean resourceTransformed = false; if (transformers != null) { for (ResourceTransformer transformer : transformers) { if (transformer.canTransformResource(zn)) { getLog().info("Transforming " + zn + " using " + transformer.getClass().getName()); InputStream is = zin.getInputStream(ze); transformer.processResource(zn, is, null); is.close(); resourceTransformed = true; break; } } } if (!resourceTransformed) { // Avoid duplicates that aren't accounted for by the resource transformers if (metaInfOnly && this.extractDuplicates && !entries.add(zn)) { continue; } InputStream is = zin.getInputStream(ze); final ZipEntry ne; if (ze.getMethod() == ZipEntry.STORED) { ne = new ZipEntry(ze); } else { ne = new ZipEntry(zn); } zos.putNextEntry(ne); copyStreamWithoutClosing(is, zos); is.close(); zos.closeEntry(); } } zin.close(); }
From source file:com.taobao.android.builder.tools.sign.LocalSignedJarBuilder.java
/** * Copies the content of a Jar/Zip archive into the receiver archive. * <p/>An optional {@link IZipEntryFilter} allows to selectively choose which files * to copy over.// w w w .j a v a 2s .c o m * * @param input the {@link InputStream} for the Jar/Zip to copy. * @param filter the filter or <code>null</code> * @throws IOException * @throws SignedJarBuilder.IZipEntryFilter.ZipAbortException if the {@link IZipEntryFilter} filter indicated that the write * must be aborted. */ public void writeZip(InputStream input, IZipEntryFilter filter) throws IOException, IZipEntryFilter.ZipAbortException { ZipInputStream zis = new ZipInputStream(input); try { // loop on the entries of the intermediary package and put them in the final package. ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String name = entry.getName(); // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } // ignore some of the content in META-INF/ but not all if (name.startsWith("META-INF/")) { // ignore the manifest file. String subName = name.substring(9); if ("MANIFEST.MF".equals(subName)) { int count; ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((count = zis.read(buffer)) != -1) { out.write(buffer, 0, count); } ByteArrayInputStream swapStream = new ByteArrayInputStream(out.toByteArray()); Manifest manifest = new Manifest(swapStream); mManifest.getMainAttributes().putAll(manifest.getMainAttributes()); continue; } // special case for Maven meta-data because we really don't care about them in apks. if (name.startsWith("META-INF/maven/")) { continue; } // check for subfolder int index = subName.indexOf('/'); if (index == -1) { // no sub folder, ignores signature files. if (subName.endsWith(".SF") || name.endsWith(".RSA") || name.endsWith(".DSA")) { continue; } } } // if we have a filter, we check the entry against it if (filter != null && !filter.checkEntry(name)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } writeEntry(zis, newEntry); zis.closeEntry(); } } finally { zis.close(); } }
From source file:org.apache.taverna.robundle.TestBundles.java
@Ignore("Broken in OpenJDK8 zipfs") @Test/*from ww w . j a v a 2 s . co m*/ public void mimeTypePosition() throws Exception { Bundle bundle = Bundles.createBundle(); String mimetype = "application/x-test"; Bundles.setMimeType(bundle, mimetype); assertEquals(mimetype, Bundles.getMimeType(bundle)); Path zip = Bundles.closeBundle(bundle); assertTrue(Files.exists(zip)); try (ZipFile zipFile = new ZipFile(zip.toFile())) { // Must be first entry ZipEntry mimeEntry = zipFile.entries().nextElement(); assertEquals("First zip entry is not 'mimetype'", "mimetype", mimeEntry.getName()); assertEquals("mimetype should be uncompressed, but compressed size mismatch", mimeEntry.getCompressedSize(), mimeEntry.getSize()); assertEquals("mimetype should have STORED method", ZipEntry.STORED, mimeEntry.getMethod()); assertEquals("Wrong mimetype", mimetype, IOUtils.toString(zipFile.getInputStream(mimeEntry), "ASCII")); } // Check position 30++ according to // http://livedocs.adobe.com/navigator/9/Navigator_SDK9_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Navigator_SDK9_HTMLHelp&file=Appx_Packaging.6.1.html#1522568 byte[] expected = ("mimetype" + mimetype + "PK").getBytes("ASCII"); FileInputStream in = new FileInputStream(zip.toFile()); byte[] actual = new byte[expected.length]; try { assertEquals(MIME_OFFSET, in.skip(MIME_OFFSET)); assertEquals(expected.length, in.read(actual)); } finally { in.close(); } assertArrayEquals(expected, actual); }