Example usage for java.util.zip ZipFile getInputStream

List of usage examples for java.util.zip ZipFile getInputStream

Introduction

In this page you can find the example usage for java.util.zip ZipFile getInputStream.

Prototype

public InputStream getInputStream(ZipEntry entry) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("ziptest.zip");

    Stream<? extends ZipEntry> entryStream = zf.stream();
    entryStream.forEach(entry -> {//from  w  ww. ja  v  a  2s  .c  o  m
        try {
            // Get the input stream for the current zip entry
            InputStream is = zf.getInputStream(entry);
            System.out.println(entry.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }

    });
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    ZipFile zf = new ZipFile("a.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        FileOutputStream fout = new FileOutputStream(ze.getName());
        InputStream in = zf.getInputStream(ze);
        for (int c = in.read(); c != -1; c = in.read()) {
            fout.write(c);/*from   w  ww  .ja v a  2  s .  c om*/
        }
        in.close();
        fout.close();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    ZipFile zf = new ZipFile(args[0]);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        System.out.println("Unzipping " + ze.getName());
        FileOutputStream fout = new FileOutputStream(ze.getName());
        InputStream in = zf.getInputStream(ze);
        for (int c = in.read(); c != -1; c = in.read()) {
            fout.write(c);/* w w w  .ja  v  a2 s . c  o m*/
        }
        in.close();
        fout.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    ZipFile zipFile = new ZipFile(zipname);
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
        System.out.println("Unzipping: " + zipEntry.getName());
        BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
        int size;
        byte[] buffer = new byte[2048];
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipEntry.getName()),
                buffer.length);/*  ww w . j  a  v  a 2s  . com*/
        while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
        bis.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zip = new ZipFile(new File("sample.zip"));

    for (Enumeration e = zip.entries(); e.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        System.out.println("File name: " + entry.getName() + "; size: " + entry.getSize()
                + "; compressed size: " + entry.getCompressedSize());
        InputStream is = zip.getInputStream(entry);
        InputStreamReader isr = new InputStreamReader(is);

        char[] buffer = new char[1024];
        while (isr.read(buffer, 0, buffer.length) != -1) {
            String s = new String(buffer);
            System.out.println(s.trim());
        }//from  w w  w.ja va 2s  .  c o m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("ziptest.zip");

    // Get the enumeration for all zip entries and loop through them
    Enumeration<? extends ZipEntry> e = zf.entries();
    ZipEntry entry = null;//from   w ww .  j  a va 2s  .  com

    while (e.hasMoreElements()) {
        entry = e.nextElement();

        // Get the input stream for the current zip entry
        InputStream is = zf.getInputStream(entry);

        /* Read data for the entry using the is object */

        // Print the name of the entry
        System.out.println(entry.getName());
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ZipFile zf = new ZipFile("a.zip");
    Enumeration<? extends ZipEntry> files = zf.entries();

    while (files.hasMoreElements()) {
        ZipEntry ze = files.nextElement();

        System.out.println("Decompressing " + ze.getName());
        System.out.println(//from w w  w  . ja  va2s. c  o m
                "  Compressed Size: " + ze.getCompressedSize() + "  Expanded Size: " + ze.getSize() + "\n");

        BufferedInputStream fin = new BufferedInputStream(zf.getInputStream(ze));
        BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(ze.getName()));

        int i;
        do {
            i = fin.read();
            if (i != -1)
                fout.write(i);
        } while (i != -1);

        fout.close();
        fin.close();
    }
    zf.close();
}

From source file:ReadZip.java

public static void main(String args[]) {
    try {/*w  w w  .  j  ava  2s .  com*/
        ZipFile zf = new ZipFile("ReadZip.zip");
        Enumeration entries = zf.entries();

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        while (entries.hasMoreElements()) {
            ZipEntry ze = (ZipEntry) entries.nextElement();
            System.out.println("Read " + ze.getName() + "?");
            String inputLine = input.readLine();
            if (inputLine.equalsIgnoreCase("yes")) {
                long size = ze.getSize();
                if (size > 0) {
                    System.out.println("Length is " + size);
                    BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        System.out.println(line);
                    }
                    br.close();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.hecl.androidbuilder.AndroidBuilder.java

public static void main(String[] args) throws IOException, ParseException {
    String androiddir = null;/*from  w  w  w.  j  av  a  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   w  w  w.  j  av  a 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);
}