Example usage for java.util.zip ZipFile ZipFile

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

Introduction

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

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

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(/* ww  w  .j a  v a2s.  co  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: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);//w w  w  .  j  av a2s  . co  m
        while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
        bis.close();
    }
}

From source file:ZipFileViewer.java

public static void main(String[] arg) throws Exception {
    String zipFileName = arg[0];// w  w  w  .  j  av  a2  s . c o  m
    List zipFileList = null;
    ZipFile zipFile = new ZipFile(zipFileName);
    Enumeration zipEntries = zipFile.entries();
    zipFileList = new ArrayList();
    while (zipEntries.hasMoreElements()) {
        zipFileList.add((ZipEntry) (zipEntries.nextElement()));
    }
    ZipFileViewer zipFileViewer = new ZipFileViewer(zipFileName, zipFileList);
}

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 a v a2 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:com.heliosdecompiler.appifier.Appifier.java

public static void main(String[] args) throws Throwable {
    if (args.length == 0) {
        System.out.println("An input JAR must be specified");
        return;/*from  w w  w.ja v a  2  s.  com*/
    }

    File in = new File(args[0]);

    if (!in.exists()) {
        System.out.println("Input not found");
        return;
    }

    String outName = args[0];
    outName = outName.substring(0, outName.length() - ".jar".length()) + "-appified.jar";

    File out = new File(outName);

    if (out.exists()) {
        if (!out.delete()) {
            System.out.println("Could not delete out file");
            return;
        }
    }

    try (ZipOutputStream outstream = new ZipOutputStream(new FileOutputStream(out));
            ZipFile zipFile = new ZipFile(in)) {

        {
            ZipEntry systemHook = new ZipEntry("com/heliosdecompiler/appifier/SystemHook.class");
            outstream.putNextEntry(systemHook);
            outstream.write(SystemHookDump.dump());
            outstream.closeEntry();
        }

        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();

        while (enumeration.hasMoreElements()) {
            ZipEntry next = enumeration.nextElement();

            if (!next.isDirectory()) {
                ZipEntry result = new ZipEntry(next.getName());
                outstream.putNextEntry(result);
                if (next.getName().endsWith(".class")) {
                    byte[] classBytes = IOUtils.toByteArray(zipFile.getInputStream(next));
                    outstream.write(transform(classBytes));
                } else {
                    IOUtils.copy(zipFile.getInputStream(next), outstream);
                }
                outstream.closeEntry();
            }
        }
    }
}

From source file:ZipCompress.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    // No corresponding getComment(), though.
    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;/*  w  ww.  j a  v  a  2s  . com*/
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();
    // Checksum valid only after the file has been closed!
    System.out.println("Checksum: " + csum.getChecksum().getValue());
    // Now extract the files:
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();
    // Alternative way to open and read zip files:
    ZipFile zf = new ZipFile("test.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze2 = (ZipEntry) e.nextElement();
        System.out.println("File: " + ze2);
        // ... and extract the data as before
    }
}

From source file:Main.java

public static ZipFile newZipFile(File file) {
    try {/*from w w w . j  ava2s . c  o  m*/
        return new ZipFile(file);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Enumeration<?> getEntriesEnumeration(File zipFile) throws IOException {
    ZipFile zf = new ZipFile(zipFile);
    Enumeration<?> entry = zf.entries();
    zf.close();/* www . j a v  a 2  s.  co m*/
    return entry;
}

From source file:Main.java

public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException, IOException {
    ZipFile zf = new ZipFile(zipFile);
    return zf.entries();

}

From source file:Main.java

public static byte[] readZipEntry(File zfile, ZipEntry entry) throws ZipException, IOException {
    Log.d("file3: ", zfile.toString());
    Log.d("zipEntry3: ", entry.toString());
    ZipFile zipFile = new ZipFile(zfile);
    if (entry != null && !entry.isDirectory()) {
        byte[] barr = new byte[(int) entry.getSize()];
        int read = 0;
        int len = 0;
        InputStream is = zipFile.getInputStream(entry);
        BufferedInputStream bis = new BufferedInputStream(is);
        int length = barr.length;
        while ((len = bis.read(barr, read, length - read)) != -1) {
            read += len;/*from  w w  w  .j  av  a 2  s . c  o m*/
        }
        bis.close();
        is.close();
        zipFile.close();
        return barr;
    } else {
        zipFile.close();
        return new byte[0];
    }
}