List of usage examples for java.util.zip ZipFile getEntry
public ZipEntry getEntry(String name)
From source file:Main.java
public static void main(String[] args) throws Exception { ZipFile zipFile = new ZipFile(new File("testfile.zip"), ZipFile.OPEN_READ); ZipEntry entry = zipFile.getEntry("fileName"); }
From source file:Main.java
public static void main(String[] args) throws Exception { ZipFile zipFile = new ZipFile(new File("testfile.zip"), ZipFile.OPEN_READ, Charset.defaultCharset()); ZipEntry entry = zipFile.getEntry("fileName"); }
From source file:org.hecl.androidbuilder.AndroidBuilder.java
public static void main(String[] args) throws IOException, ParseException { String androiddir = null;// w w w. j ava 2 s. c o m Options opts = new Options(); /* Define some command line options. */ opts.addOption("android", true, "android SDK location"); opts.addOption("class", true, "New class name"); opts.addOption("package", true, "New package name, like bee.bop.foo.bar"); opts.addOption("label", true, "Label"); opts.addOption("permissions", true, "Android Permissions"); opts.addOption("intentfilter", true, "Intent Filter File"); opts.addOption("extraclass", true, "Extra class"); opts.addOption("script", true, "Script file"); CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(opts, args); /* Get the android directory, or fail if it's not given. */ if (cmd.hasOption("android")) { androiddir = cmd.getOptionValue("android"); } else { usage(opts); } String aapt = androiddir + sep + "tools" + sep + "aapt"; String dx = androiddir + sep + "tools" + sep + "dx"; if (sep == "\\") { /* It's windows */ dx += ".bat"; } String androidjar = androiddir + sep + "android.jar"; /* Get the application's class name. */ String appclass = "Hackle"; if (cmd.hasOption("class")) { appclass = cmd.getOptionValue("class"); } /* Get the application's label. */ String appname = "Hecl Hackle"; if (cmd.hasOption("label")) { appname = cmd.getOptionValue("label"); } /* Get the fake package name. */ String packagename = "bee.bop.doo.wah"; if (cmd.hasOption("package")) { packagename = cmd.getOptionValue("package"); } String perms = ""; if (cmd.hasOption("permissions")) { for (String p : cmd.getOptionValue("permissions").split(",")) { perms += "<uses-permission android:name=\"android.permission." + p + "\" />\n"; } } boolean hasextraClass = false; String extraClass = ""; if (cmd.hasOption("extraclass")) { hasextraClass = true; extraClass = cmd.getOptionValue("extraclass"); } String intentfilterFile = ""; if (cmd.hasOption("intentfilter")) { intentfilterFile = cmd.getOptionValue("intentfilter"); } String scriptFilename = null; if (cmd.hasOption("script")) { scriptFilename = cmd.getOptionValue("script"); } /* Calculate some other stuff based on the informatin we have. */ String tmpdir = System.getProperty("java.io.tmpdir"); File dirnamefile = new File(tmpdir, appclass + "-" + System.currentTimeMillis()); String dirname = dirnamefile.toString(); String manifest = dirname + sep + "AndroidManifest.xml"; String tmppackage = dirname + sep + "Temp.apk"; String hecljar = dirname + sep + "Hecl.jar"; String heclapk = dirname + sep + "Hecl.apk"; String resdir = dirname + sep + "res"; String icondir = resdir + sep + "drawable"; String iconfile = (new File(icondir, "aicon.png")).toString(); String intentreceiver = ""; /* If we have an intent filter .xml file, read it and add its * contents. */ if (!intentfilterFile.equals("")) { StringBuffer sb = new StringBuffer(""); FileInputStream fis = new FileInputStream(intentfilterFile); int c = 0; while ((c = fis.read()) != -1) { sb.append((char) c); } fis.close(); intentreceiver = sb.toString(); } /* The AndroidManifest.xml template. */ String xmltemplate = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" \n" + "package=\"" + packagename + "\">\n" + perms + "<application android:icon=\"@drawable/aicon\">\n" + /* Main activity */ "<activity android:name=\"" + appclass + "\" android:label=\"" + appname + "\">\n" + "<intent-filter>\n" + "<action android:name=\"android.intent.action.MAIN\" />\n" + "<category android:name=\"android.intent.category.LAUNCHER\" />\n" + "</intent-filter>\n" + "</activity>\n" + /* SubHecl */ "<activity android:name=\"" + "Sub" + appclass + "\" android:label=\"SubHecl\">\n" + "<intent-filter>\n" + "<action android:name=\"android.intent.action.MAIN\" />\n" + "</intent-filter>\n" + "</activity>\n" + /* Intent Receiver. */ intentreceiver + "</application>\n" + "</manifest>\n"; /* Template for the main .java file. */ String mainClassTemplate = "package " + packagename + ";\n" + "import org.hecl.android.Hecl;\n" + "import org.hecl.HeclException;\n" + "import org.hecl.Interp;\n" + "import org.hecl.java.JavaCmd;\n" + "public class " + appclass + " extends Hecl {\n" + "protected void createCommands(Interp i) throws HeclException {\n" + "JavaCmd.load(interp, \"" + packagename + "." + appclass + "\", \"hecl\");\n" + "JavaCmd.load(interp, \"" + packagename + ".Sub" + appclass + "\", \"subhecl\");\n" + "}\n" + "}\n"; /* Template for the sub file. */ String subClassTemplate = "package " + packagename + ";\n" + "import org.hecl.android.SubHecl;\n" + "public class Sub" + appclass + " extends SubHecl {}\n"; /* First we write out the AndroidManifest.xml file. */ (new File(dirname)).mkdir(); FileWriter outputstream = null; try { outputstream = new FileWriter(manifest); outputstream.write(xmltemplate); } catch (IOException e) { System.err.println("Couldn't write to " + manifest + " : " + e.toString()); System.exit(1); } finally { if (outputstream != null) { outputstream.close(); } } InputStream is = null; FileOutputStream fos = null; /* Make a directory for the icon. */ (new File(icondir)).mkdirs(); copyFileStream(AndroidBuilder.class.getResourceAsStream("/android/res/drawable/aicon.png"), new FileOutputStream(iconfile)); /* Now, we run aapt to generate a new, compressed .xml file... */ runProcess(aapt, "package", "-f", "-M", manifest, "-S", resdir, "-I", androidjar, "-F", tmppackage); /* Then we extract it, overwriting AndroidManifest.xml*/ ZipFile zipfile = new ZipFile(tmppackage); ZipEntry newmanifest = zipfile.getEntry("AndroidManifest.xml"); System.out.println("newmanifest is " + newmanifest); is = zipfile.getInputStream(newmanifest); fos = new FileOutputStream(manifest); copyFileStream(is, fos); /* Now, we copy in Hecl.jar ... */ is = AndroidBuilder.class.getResourceAsStream("/android/Hecl.jar"); fos = new FileOutputStream(hecljar); copyFileStream(is, fos); /* ... and the Hecl.apk. */ is = AndroidBuilder.class.getResourceAsStream("/android/bin/Hecl.apk"); fos = new FileOutputStream(heclapk); copyFileStream(is, fos); /* Now, we can create some Java classes ... */ String packagedir = dirname; String jarpackagedir = ""; /* The name inside the jar file. */ for (String s : packagename.split("\\.")) { packagedir += sep + s; jarpackagedir += s + sep; } (new File(packagedir)).mkdirs(); String mainJava = packagedir + sep + appclass + ".java"; String subJava = packagedir + sep + "Sub" + appclass + ".java"; String mainClass = jarpackagedir + appclass + ".class"; String subClass = jarpackagedir + "Sub" + appclass + ".class"; /* Output a new 'main' class. */ fos = new FileOutputStream(mainJava); fos.write(mainClassTemplate.getBytes()); fos.close(); /* Output a new 'sub' class. */ fos = new FileOutputStream(subJava); fos.write(subClassTemplate.getBytes()); fos.close(); /* Compile the new classes. */ runProcess("javac", mainJava, subJava, "-cp", hecljar + pathsep + androidjar); /* Stash them in the .jar. */ runProcess("jar", "uf", hecljar, "-C", dirname, mainClass); runProcess("jar", "uf", hecljar, "-C", dirname, subClass); /* If there is an extra class, move it into the .jar */ if (hasextraClass) { File ec = new File(extraClass); is = new FileInputStream(ec); String outfile = dirname + sep + jarpackagedir + ec.getName(); System.out.println("Moving " + extraClass + " to " + outfile); fos = new FileOutputStream(outfile); copyFileStream(is, fos); runProcess("jar", "uf", hecljar, "-C", dirname, jarpackagedir + ec.getName()); } /* Run the dx program to turn them into Android dex stuff. */ String dexfile = dirname + sep + "classes.dex"; runProcess(dx, "-JXmx384M", "--dex", "--output=" + dexfile, "--positions=lines", hecljar); /* Finally, rename the whole business back to the calling * directory. We copy the whole thing across as a .zip * archive in order to replace the script.hcl file. */ String newfilename = System.getProperty("user.dir") + sep + appclass + ".apk"; if (scriptFilename == null) { /* Just move it over. */ (new File(heclapk)).renameTo(new File(newfilename)); } else { /* Copy it bit by bit, and replace the script.hcl file. */ ZipInputStream zif = new ZipInputStream(new FileInputStream(heclapk)); ZipOutputStream zof = new ZipOutputStream(new FileOutputStream(newfilename)); int read; byte[] buf = new byte[4096]; ZipEntry ze = zif.getNextEntry(); while (ze != null) { zof.putNextEntry(new ZipEntry(ze.getName())); if ("res/raw/script.hcl".equals(ze.getName())) { FileInputStream inf = new FileInputStream(scriptFilename); while ((read = inf.read(buf)) != -1) { zof.write(buf, 0, read); } inf.close(); /* Replace the apk's AndroidManifest.xml ... */ } else if ("AndroidManifest.xml".equals(ze.getName())) { FileInputStream inf = new FileInputStream(manifest); while ((read = inf.read(buf)) != -1) { zof.write(buf, 0, read); } inf.close(); /* ... and classes.dex */ } else if ("classes.dex".equals(ze.getName())) { FileInputStream inf = new FileInputStream(dexfile); while ((read = inf.read(buf)) != -1) { zof.write(buf, 0, read); } inf.close(); } else { while ((read = zif.read(buf)) != -1) { zof.write(buf, 0, read); } } ze = zif.getNextEntry(); } zif.close(); zof.close(); } /* FIXME - we should probably destroy the temporary directory, * but it's very useful for debugging purposes. */ }
From source file:ZipCompare.java
public static void main(String[] args) { if (args.length != 2) { System.out.println("Usage: zipcompare [file1] [file2]"); System.exit(1);//from ww w . j a va 2 s .c o m } ZipFile file1; try { file1 = new ZipFile(args[0]); } catch (IOException e) { System.out.println("Could not open zip file " + args[0] + ": " + e); System.exit(1); return; } ZipFile file2; try { file2 = new ZipFile(args[1]); } catch (IOException e) { System.out.println("Could not open zip file " + args[0] + ": " + e); System.exit(1); return; } System.out.println("Comparing " + args[0] + " with " + args[1] + ":"); Set set1 = new LinkedHashSet(); for (Enumeration e = file1.entries(); e.hasMoreElements();) set1.add(((ZipEntry) e.nextElement()).getName()); Set set2 = new LinkedHashSet(); for (Enumeration e = file2.entries(); e.hasMoreElements();) set2.add(((ZipEntry) e.nextElement()).getName()); int errcount = 0; int filecount = 0; for (Iterator i = set1.iterator(); i.hasNext();) { String name = (String) i.next(); if (!set2.contains(name)) { System.out.println(name + " not found in " + args[1]); errcount += 1; continue; } try { set2.remove(name); if (!streamsEqual(file1.getInputStream(file1.getEntry(name)), file2.getInputStream(file2.getEntry(name)))) { System.out.println(name + " does not match"); errcount += 1; continue; } } catch (Exception e) { System.out.println(name + ": IO Error " + e); e.printStackTrace(); errcount += 1; continue; } filecount += 1; } for (Iterator i = set2.iterator(); i.hasNext();) { String name = (String) i.next(); System.out.println(name + " not found in " + args[0]); errcount += 1; } System.out.println(filecount + " entries matched"); if (errcount > 0) { System.out.println(errcount + " entries did not match"); System.exit(1); } System.exit(0); }
From source file:Main.java
public static boolean searchJarFile(File file, String classFilePath) { try {//from w w w . j av a 2s .com if (!file.exists()) return false; ZipFile jarFile = new ZipFile(file); if (jarFile.getEntry(classFilePath) != null) { jarFile.close(); return true; } else { jarFile.close(); return false; } } catch (IOException ex) { System.out.println(ex.toString()); return false; } }
From source file:Main.java
public static String getBuildTimestamp(Activity activity) { String s = ""; try {//from w w w . j a v a 2 s. com ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0); ZipFile zf = new ZipFile(ai.sourceDir); ZipEntry ze = zf.getEntry("classes.dex"); long time = ze.getTime(); s = SimpleDateFormat.getInstance().format(new java.util.Date(time)); zf.close(); } catch (Exception e) { } return s; }
From source file:Main.java
/** * Determine whether a file is a JAR File. *//*from ww w.j a v a 2s . com*/ public static boolean isJarFile(File file) throws IOException { if (!isZipFile(file)) { return false; } ZipFile zip = new ZipFile(file); boolean manifest = zip.getEntry("META-INF/MANIFEST.MF") != null; zip.close(); return manifest; }
From source file:org.quickgeo.generate.Generate.java
License:asdf
private static ImmutableMap<String, InputStream> convertToStreams(ImmutableMap<String, File> map) { ImmutableMap.Builder<String, InputStream> builder = new ImmutableMap.Builder<String, InputStream>(); for (Entry<String, File> entry : map.entrySet()) { String cc = entry.getKey(); File zipFile = entry.getValue(); try {// w w w .j a v a 2 s . c o m ZipFile zf = new ZipFile(zipFile); ZipEntry ze = zf.getEntry(cc + ".txt"); if (ze != null) { builder.put(cc, zf.getInputStream(ze)); } } catch (Exception ex) { Settings.getSettings().getLogger().log(Level.WARNING, "Couldn't parse entry for " + cc, ex); } } return builder.build(); }
From source file:Main.java
public static java.io.InputStream upZip(String zipFilePath, String fileString) throws Exception { java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(zipFilePath); java.util.zip.ZipEntry zipEntry = zipFile.getEntry(fileString); return zipFile.getInputStream(zipEntry); }
From source file:Main.java
/** * Check is classes.dex file has modified * /*from w w w .j a v a 2s . co m*/ * @author : sWX293372 * @version: 1.0 * @return boolean if the classes.dex is modified then return true, else * return true * @createTime : 2016-5-9 */ public static boolean checkCRC(Context context, long crc) { boolean isModified = true; try { ZipFile zipFile = new ZipFile(context.getPackageCodePath()); ZipEntry entry = zipFile.getEntry("classes.dex"); if (crc == entry.getCrc()) { isModified = false; } } catch (IOException e) { isModified = true; e.printStackTrace(); } return isModified; }