Java Unzip File unzip(File target)

Here you can find the source of unzip(File target)

Description

Unzips a file and return a list of the unziped files.

License

Open Source License

Parameter

Parameter Description
target File to be unzipped.

Exception

Parameter Description
IOException an exception

Return

A lista with the files that where unzipped.

Declaration

public static List<File> unzip(File target) throws IOException 

Method Source Code

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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

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

public class Main {
    private static final int BUFFER = 2048;

    /**//  w w  w.  j  ava  2  s . c o  m
     * Unzips a file and return a list of the unziped files.
     * 
     * @param target File to be unzipped.
     * @return A lista with the files that where unzipped.
     * @throws IOException 
     */
    public static List<File> unzip(File target) throws IOException {

        List<File> unzippedFiles = new ArrayList<>();
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(target);
        ZipEntry entry = null;

        try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {

            while ((entry = zis.getNextEntry()) != null) {

                int count;
                byte data[] = new byte[BUFFER];

                // write the files to the disk
                File unf = new File(target.getParentFile().getAbsolutePath() + "/" + entry.getName());
                unzippedFiles.add(unf);

                FileOutputStream fos = new FileOutputStream(unf);
                dest = new BufferedOutputStream(fos, BUFFER);

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

                dest.flush();
                dest.close();

            }

        }

        return unzippedFiles;

    }
}

Related

  1. unzip(File sourceZipfile, File directory)
  2. unzip(File src, File dest)
  3. unzip(File src, File target)
  4. unzip(File srcZipFile, File tgtDir)
  5. unzip(File tarFile, File destinationDir)
  6. unzip(File targetZip, File dirToExtract)
  7. unzip(File zip)
  8. unZip(File zip, File dest)
  9. unzip(File zip, File destination)