Java Unzip to Folder unzip(String src, String dest, PrintStream stream)

Here you can find the source of unzip(String src, String dest, PrintStream stream)

Description

unzip

License

Apache License

Declaration

public static void unzip(String src, String dest, PrintStream stream)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private static final int BYTE_ARRAY_SIZE = 65536;

    public static void unzip(String src, String dest, PrintStream stream)
            throws IOException {
        src = src.replace("~", System.getProperty("user.home"));
        dest = dest.replace("~", System.getProperty("user.home"));
        if (!new File(src).exists()) {
            throw new RuntimeException(src + " does not exist.");
        }//from  w ww .  ja v  a 2 s.  co m
        ZipInputStream zis = new ZipInputStream(new CheckedInputStream(
                new FileInputStream(src), new CRC32()));
        innerUnzip(zis, dest, stream);
        zis.close();
    }

    private static void innerUnzip(ZipInputStream zis, String destPath,
            PrintStream stream) throws IOException {
        if (destPath == null) {
            destPath = new String();
        }
        ZipEntry entry;
        int read;
        byte data[] = new byte[BYTE_ARRAY_SIZE];
        while ((entry = zis.getNextEntry()) != null) {
            String currentPath = destPath + File.separator
                    + entry.getName();
            if (stream != null) {
                stream.println(currentPath);
            }
            File currentFile = new File(currentPath);
            File parentPath = currentFile.getParentFile();
            if (!parentPath.exists()) {
                parentPath.mkdirs();
            }
            if (entry.isDirectory()) {
                currentFile.mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(currentFile);
                while ((read = zis.read(data)) != -1) {
                    fos.write(data, 0, read);
                }
                fos.flush();
                fos.close();
            }
            zis.closeEntry();
        }
    }
}

Related

  1. unzip(String sourceFile, String destDir)
  2. unzip(String sourceFile, String destDir)
  3. unzip(String sourceFile, String toDir)
  4. unzip(String sourceFileName, String destPath)
  5. unzip(String sourceZip, String targetDirectory)
  6. unzip(String sZip)
  7. unzip(String toUnpackName)
  8. unzip(String zip, String dest)
  9. unzip(String zip, String folder)