Java Unzip to Folder unZip(String pathDest, BufferedInputStream buffInputStream)

Here you can find the source of unZip(String pathDest, BufferedInputStream buffInputStream)

Description

Unzip a inputstream to a folder

License

Open Source License

Parameter

Parameter Description
pathDest a parameter
buffInputStream a parameter

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static void unZip(String pathDest, BufferedInputStream buffInputStream)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Bruno G. Braga.// w w w .  j  a  v a 2 s.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bruno G. Braga - initial API and implementation
 *******************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**
     * Unzip a inputstream to a folder
     * @param pathDest
     * @param buffInputStream
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void unZip(String pathDest, BufferedInputStream buffInputStream)
            throws FileNotFoundException, IOException {
        //create destination dir
        new File(pathDest).mkdirs();
        final int BUFFER = 2048;

        BufferedOutputStream dest = null;

        //unzip file
        ZipInputStream zis = new ZipInputStream(buffInputStream);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];

            // create dirs to file
            String pathFile = pathDest + File.separator + entry.getName();
            File file = new File(pathFile);
            if (entry.isDirectory()) { //unzip only files
                file.mkdir();
                continue;
            }

            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(pathFile);
            dest = new BufferedOutputStream(fos, BUFFER);

            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    }
}

Related

  1. unzip(String filePath, String unzipPath)
  2. unzip(String inFilePath)
  3. unzip(String inFilePath, String outFilePath)
  4. unzip(String inputZipPath, String destinationDirectory)
  5. unzip(String path, int buffer)
  6. unzip(String sourceFile, String destDir)
  7. unzip(String sourceFile, String destDir)
  8. unzip(String sourceFile, String toDir)
  9. unzip(String sourceFileName, String destPath)