Java Unzip InputStream unzip(InputStream inputStream, String outputFolder)

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

Description

Unzips a file all into one directory

License

Apache License

Parameter

Parameter Description
inputStream a parameter
outputFolder a parameter

Declaration

public static void unzip(InputStream inputStream, String outputFolder) 

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 final String SEPARATOR = "/";

    /**// w  ww . jav  a 2  s  .c o  m
     * Unzips a file all into one directory
     *
     * @param inputStream
     * @param outputFolder
     */
    public static void unzip(InputStream inputStream, String outputFolder) {
        byte[] buffer = new byte[1024];
        try {
            File folder = new File(outputFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }
            ZipInputStream zis = new ZipInputStream(inputStream);
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                if (ze.isDirectory()) {
                    ze = zis.getNextEntry();
                    continue;
                }
                fileName = new File(fileName).getName();
                File newFile = new File(outputFolder + SEPARATOR + fileName);
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        } catch (IOException ex) {
        }
    }
}

Related

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