Java Unzip to Folder unzip(String sourceFile, String toDir)

Here you can find the source of unzip(String sourceFile, String toDir)

Description

unzip

License

Open Source License

Declaration

public static void unzip(String sourceFile, String toDir) throws Exception 

Method Source Code


//package com.java2s;
import java.io.*;

import java.util.*;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;

public class Main {
    public static void unzip(String sourceFile, String toDir) throws Exception {
        ZipFile zipFile = new ZipFile(sourceFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(toDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            if (entry.isDirectory())
                entryDestination.mkdirs();
            else {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = zipFile.getInputStream(entry);
                    out = new FileOutputStream(entryDestination);
                    ByteStreams.copy(in, out);
                } finally {
                    Closeables.close(in, true);
                    Closeables.close(out, true);
                }/*w  w w  .  j  a v a 2 s .  c  om*/

            }
        }
        Closeables.close(zipFile, true);
    }
}

Related

  1. unzip(String inputZipPath, String destinationDirectory)
  2. unzip(String path, int buffer)
  3. unZip(String pathDest, BufferedInputStream buffInputStream)
  4. unzip(String sourceFile, String destDir)
  5. unzip(String sourceFile, String destDir)
  6. unzip(String sourceFileName, String destPath)
  7. unzip(String sourceZip, String targetDirectory)
  8. unzip(String src, String dest, PrintStream stream)
  9. unzip(String sZip)