Example usage for java.util.zip ZipInputStream close

List of usage examples for java.util.zip ZipInputStream close

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream("C:/MyZip.zip");
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze;/*from  ww w  .  java2 s . co m*/
    while ((ze = zis.getNextEntry()) != null) {
        System.out.println(ze.getName());
        zis.closeEntry();
    }

    zis.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ZipInputStream in = new ZipInputStream(new FileInputStream("source.zip"));
    OutputStream out = new FileOutputStream("target");
    byte[] buf = new byte[1024];
    int len;/*from w  ww  .  j  ava 2 s. c  o  m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    out.close();
    in.close();
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {
  String inFilename = "infile.zip";
  ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));

  ZipEntry entry = in.getNextEntry();

  String outFilename = "o";
  OutputStream out = new FileOutputStream(outFilename);

  byte[] buf = new byte[1024];
  int len;//from  w w w .  j  av a  2 s . co m
  while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
  }

  out.close();
  in.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipInputStream inStream = new ZipInputStream(new FileInputStream("compressed.zip"));
    OutputStream outStream = new FileOutputStream("extracted.txt");

    byte[] buffer = new byte[1024];
    int read;/*from  ww  w . ja  va  2 s  .  c  om*/
    ZipEntry entry;
    if ((entry = inStream.getNextEntry()) != null) {
        while ((read = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, read);
        }
    }
    outStream.close();
    inStream.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    for (int i = 0; i < args.length; i++) {
        FileInputStream fin = new FileInputStream(args[i]);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;/*from w  w  w.j a v  a  2s. co m*/
        while ((ze = zin.getNextEntry()) != null) {
            System.out.println("Unzipping " + ze.getName());
            FileOutputStream fout = new FileOutputStream(ze.getName());
            for (int c = zin.read(); c != -1; c = zin.read()) {
                fout.write(c);
            }
            zin.closeEntry();
            fout.close();
        }
        zin.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "d.zip";
    CheckedInputStream checksum = new CheckedInputStream(new FileInputStream(zipname), new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;/*from  w w w  .j  a v  a 2 s.c  o m*/

    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        int size;
        byte[] buffer = new byte[2048];

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entry.getName()),
                buffer.length);
        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }

    zis.close();
    System.out.println("Checksum = " + checksum.getChecksum().getValue());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;/*from   w w w. j a  v a2  s. com*/

    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename"));
    ZipEntry zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        File newFile = new File(entryName);
        String directory = newFile.getParent();
        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }/*from   w ww  .jav a2 s. c o m*/
        RandomAccessFile rf = new RandomAccessFile(entryName, "r");
        String line;
        if ((line = rf.readLine()) != null) {
            System.out.println(line);
        }
        rf.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String destinationname = "d:\\";
    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;/*w w  w  . j a v a2  s  .  c  om*/
    zipinputstream = new ZipInputStream(new FileInputStream("fileName"));
    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);
        String directory = newFile.getParent();

        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }
        fileoutputstream = new FileOutputStream(destinationname + entryName);
        int n;
        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }
        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

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

public static void main(String[] args) throws IOException, ParseException {
    String androiddir = null;/* ww  w  .ja  v a 2  s  .c om*/

    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.  */
}