Java Unzip InputStream unzip(InputStream inputStream, String destDirectory)

Here you can find the source of unzip(InputStream inputStream, String destDirectory)

Description

Extracts an (Zip)InputStream to a directory specified by destDirectory (will be created if does not exists)

License

Apache License

Parameter

Parameter Description
inputStream a parameter
destDirectory a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(InputStream inputStream, String destDirectory)
        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 {
    /**//from   ww  w  .j ava2  s. c  o m
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;

    /**
     * Extracts an (Zip)InputStream to a directory specified by
     * destDirectory (will be created if does not exists)
     * @param inputStream
     * @param destDirectory
     * @throws IOException
     */
    public static void unzip(InputStream inputStream, String destDirectory)
            throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(inputStream);
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator
                    + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }

    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private static void extractFile(ZipInputStream zipIn, String filePath)
            throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
}

Related

  1. unzip(InputStream in)
  2. unzip(InputStream in, File destination)
  3. unzip(InputStream in, File toDir)
  4. unzip(InputStream input, File dest)
  5. unzip(InputStream input, String targetDir)
  6. unzip(InputStream inputStream, String outputFolder)
  7. unzip(InputStream is, File destDir)
  8. unzip(InputStream libInputStream, String path)
  9. unzip(InputStream source, File target)