Java Unzip File unzip(File file, String destination, String container)

Here you can find the source of unzip(File file, String destination, String container)

Description

unzip

License

Open Source License

Parameter

Parameter Description
container all extracted file will be stored in container, which will be created in the destination
file a parameter
destination where the file will be stored

Return

unzipped file

Declaration


public static File unzip(File file, String destination, String container) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    /**//from   ww  w  . jav  a2 s .c  o  m
     * @param container
     *            all extracted file will be stored in container, which will be
     *            created in the destination
     * @param file
     * @param destination
     *            where the file will be stored
     * @return unzipped file
     * 
     */

    public static File unzip(File file, String destination, String container) {
        File result = new File(destination + "/" + container);
        result.mkdir();
        destination = destination + "/" + container + "/";
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry ze = entries.nextElement();
                if (ze.isDirectory())
                    (new File(destination + ze.getName())).mkdir();
                else {
                    // make sure directories exist in case the client
                    // didn't provide directory entries!

                    File f = new File(destination + ze.getName());
                    (new File(f.getParent())).mkdirs();

                    FileOutputStream fos = new FileOutputStream(f);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    InputStream in = zipFile.getInputStream(ze);
                    copystream(in, bos);
                    bos.close();
                }
            }
            zipFile.close();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        return result;
    }

    public static void copystream(InputStream in, OutputStream out) throws IOException {

        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        // out.close();
    }
}

Related

  1. unzip(File file, File directory)
  2. unzip(File file, File targetDirectory)
  3. unzip(File file, File targetDirectory)
  4. unzip(File file, File toDir)
  5. unzip(File file, File toDirectory)
  6. unZip(File file, String outPath, String zipFileName)
  7. unzip(File file, String targetDirectory)
  8. unzip(File fileToUnzip, File destinationDirectory)
  9. unZip(File inFile, File unzipDir)