Java Unzip to Folder unZip(String baseDir, InputStream is)

Here you can find the source of unZip(String baseDir, InputStream is)

Description

Extracts Zip file contents relative to baseDir

License

Open Source License

Return

An ArrayList containing the File instances for each unzipped file

Declaration

public static ArrayList unZip(String baseDir, InputStream is) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w  ww  .  j a  va 2 s .  com
 * ====================================================================
 *
 * This code is subject to the freebxml License, Version 1.1
 *
 * Copyright (c) 2001 - 2003 freebxml.org.  All rights reserved.
 *
 * $Header: /cvsroot/ebxmlrr/omar/src/java/org/freebxml/omar/common/Utility.java,v 1.40 2007/05/18 18:58:59 psterk Exp $
 * ====================================================================
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;

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

public class Main {
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    /**
     *
     * Extracts Zip file contents relative to baseDir
     * @return An ArrayList containing the File instances for each unzipped file
     */
    public static ArrayList unZip(String baseDir, InputStream is) throws IOException {
        ArrayList files = new ArrayList();
        ZipInputStream zis = new ZipInputStream(is);

        while (true) {
            // Get the next zip entry.  Break out of the loop if there are
            //   no more.
            ZipEntry zipEntry = zis.getNextEntry();
            if (zipEntry == null)
                break;

            String entryName = zipEntry.getName();
            if (FILE_SEPARATOR.equalsIgnoreCase("\\")) {
                // Convert '/' to Windows file separator
                entryName = entryName.replaceAll("/", "\\\\");
            }
            String fileName = baseDir + FILE_SEPARATOR + entryName;
            //Make sure that directory exists.
            String dirName = fileName.substring(0, fileName.lastIndexOf(FILE_SEPARATOR));
            File dir = new File(dirName);
            dir.mkdirs();

            //Entry could be a directory
            if (!(zipEntry.isDirectory())) {
                //Entry is a file not a directory.
                //Write out the content of of entry to file 
                File file = new File(fileName);
                files.add(file);
                FileOutputStream fos = new FileOutputStream(file);

                // Read data from the zip entry.  The read() method will return
                //   -1 when there is no more data to read.
                byte[] buffer = new byte[1000];

                int n;

                while ((n = zis.read(buffer)) > -1) {
                    // In real life, you'd probably write the data to a file.
                    fos.write(buffer, 0, n);
                }
                zis.closeEntry();
                fos.close();
            } else {
                zis.closeEntry();
            }
        }

        zis.close();

        return files;
    }
}

Related

  1. unzip(String archive, String out_dir)
  2. unzip(String compressedStr)
  3. unzip(String file, String destinationdir)
  4. unzip(String filePath, String unzipPath)
  5. unzip(String inFilePath)