Java Unzip File unzip(File zipFileName, File targetDir)

Here you can find the source of unzip(File zipFileName, File targetDir)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File zipFileName, File targetDir) 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.FileFilter;

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 {
    public static void unzip(File zipFileName, File targetDir) throws IOException {
        unzip(zipFileName, targetDir, new FileFilter() {
            public boolean accept(File file) {
                return true;
            }//w  w  w  .  j av  a 2 s .  c  om
        });
    }

    public static void unzip(File zipFileName, File targetDir, FileFilter filter) throws IOException {
        ZipFile zipFile = new ZipFile(zipFileName);

        Enumeration enumeration = zipFile.entries();

        while (enumeration.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) enumeration.nextElement();

            String name = entry.getName();

            if (filter.accept(new File(targetDir, name))) {
                if (entry.isDirectory()) {
                    new File(targetDir, name).mkdirs();
                } else {
                    File targetFile = new File(targetDir, name);

                    targetFile.getParentFile().mkdirs();

                    InputStream source = zipFile.getInputStream(entry);
                    FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

                    copyAndClose(source, fileOutputStream, false);
                }
            }
        }
        zipFile.close();
    }

    public static void copyAndClose(InputStream source, OutputStream dest, boolean buffered) throws IOException {
        copy(source, dest, buffered);
        source.close();
        dest.close();
    }

    public static void copy(InputStream source, OutputStream dest, boolean buffered) throws IOException {
        //TODO: buffering is disabled until file truncation issue is resolved
        buffered = false;
        if (buffered) {
            source = new BufferedInputStream(source);
            dest = new BufferedOutputStream(dest);
        }

        int bytesRead;

        byte[] buffer = new byte[1024];

        while ((bytesRead = source.read(buffer)) >= 0) {
            dest.write(buffer, 0, bytesRead);
        }
    }
}

Related

  1. unzip(File zipfile, File directory)
  2. unzip(File zipFile, File outputDir)
  3. unzip(File zipFile, File outputFolder)
  4. unZip(File zipFile, String desdir)
  5. unZip(File zipFile, String extPlace, boolean reservZipFile)
  6. unzip(File zipName, File destDir)
  7. unZip(File zipPath, File destPath)
  8. unzip(File zippedFile)
  9. unzip(File zippedFile, File outputDirectory)