Java Unzip InputStream unzip(InputStream libInputStream, String path)

Here you can find the source of unzip(InputStream libInputStream, String path)

Description

unzip

License

Apache License

Declaration

public static void unzip(InputStream libInputStream, String path) throws IOException 

Method Source Code


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

import java.io.*;

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

public class Main {
    public static void unzip(InputStream libInputStream, String path) throws IOException {
        createFolder(path);/*from  w w w. j  av a  2 s .  co m*/
        ZipInputStream zis = new ZipInputStream(libInputStream);

        try {
            ZipEntry ze;
            byte[] buffer = new byte[4096];

            while ((ze = zis.getNextEntry()) != null) {
                if (ze.isDirectory())
                    continue;

                String fileName = ze.getName();

                int bytesRead;
                String filePath = path + File.separator + fileName;
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(filePath);
                    while ((bytesRead = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, bytesRead);
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } finally {
                    if (fos != null) {
                        fos.flush();
                        fos.close();
                    }
                    zis.closeEntry();
                }
            }
        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } finally {
            if (zis != null)
                zis.close();
        }
    }

    private static void createFolder(String path) {
        File file = new File(path);
        if (!file.exists())
            file.mkdirs();

    }

    public static void close(OutputStream os) throws IOException {
        if (os != null)
            os.close();
    }

    public static void close(Writer w) throws IOException {
        if (w != null)
            w.close();
    }

    public static void close(RandomAccessFile raf) throws IOException {
        if (raf != null)
            raf.close();
    }

    public static void close(BufferedReader br) throws IOException {
        br.close();
    }
}

Related

  1. unzip(InputStream input, File dest)
  2. unzip(InputStream input, String targetDir)
  3. unzip(InputStream inputStream, String destDirectory)
  4. unzip(InputStream inputStream, String outputFolder)
  5. unzip(InputStream is, File destDir)
  6. unzip(InputStream source, File target)
  7. unzip(InputStream zin, File file)