Java Unzip File unzip(File sourceZipfile, File directory)

Here you can find the source of unzip(File sourceZipfile, File directory)

Description

unzip

License

Apache License

Declaration

public static void unzip(File sourceZipfile, File directory) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
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;
import java.util.zip.ZipInputStream;

public class Main {
    public static void unzip(File sourceZipfile, File directory) throws IOException {
        try (ZipFile zfile = new ZipFile(sourceZipfile);) {
            Enumeration<? extends ZipEntry> entries = zfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File file = new File(directory, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();/*from  ww  w.ja  va 2  s  .c  om*/
                } else {
                    file.getParentFile().mkdirs();
                    try (InputStream in = zfile.getInputStream(entry)) {
                        copy(in, file);
                    }
                }
            }
        }
    }

    public static void unzip(String sourceZipFilePath, String destFilePath) throws IOException {
        unzip(new File(sourceZipFilePath), new File(destFilePath));
    }

    public static void unzip(InputStream sourceStream, String destFilePath) throws IOException {
        File destFile = new File(destFilePath);
        try (ZipInputStream zipStream = new ZipInputStream(sourceStream)) {
            ZipEntry entry = null;
            try (BufferedInputStream is = new BufferedInputStream(zipStream)) {
                while ((entry = zipStream.getNextEntry()) != null) {
                    File file = new File(destFile, entry.getName());
                    if (entry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        file.getParentFile().mkdirs();
                        copy(is, file);
                    }
                }
            }
        }
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int readCount = 0;
        while ((readCount = in.read(buffer)) >= 0) {
            out.write(buffer, 0, readCount);
        }
    }

    private static void copy(File file, OutputStream out) throws IOException {
        try (InputStream in = new FileInputStream(file)) {
            copy(in, out);
        }
    }

    private static void copy(InputStream in, File file) throws IOException {
        try (OutputStream out = new FileOutputStream(file)) {
            copy(in, out);
        }
    }
}

Related

  1. unzip(File inputFile, File unzipDestFolder)
  2. unzip(File intoFolder, ZipFile zipFile)
  3. unzip(File jar, File target)
  4. unzip(File jarFile, File destDir)
  5. unzip(File sourceFile, File rootDir)
  6. unzip(File src, File dest)
  7. unzip(File src, File target)
  8. unzip(File srcZipFile, File tgtDir)
  9. unzip(File tarFile, File destinationDir)