Java Unzip ZipFile unzip(File srcFile, File toDir)

Here you can find the source of unzip(File srcFile, File toDir)

Description

unzip

License

Open Source License

Declaration

public static boolean unzip(File srcFile, File toDir) 

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.FileOutputStream;
import java.io.IOException;

import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static boolean unzip(File srcFile, File toDir) {
        return unzip(srcFile.toPath(), toDir.toPath());
    }//from   www . j a  v  a2 s  .c o  m

    public static boolean unzip(Path srcFile, Path toDir) {
        try (ZipFile zipFile = new ZipFile(srcFile.toFile())) {
            Enumeration<? extends ZipEntry> enumZip = zipFile.entries();
            while (enumZip.hasMoreElements()) {
                ZipEntry zipEntry = enumZip.nextElement();

                File ofile = new File(toDir.toFile(), new File(zipEntry.getName()).getName());

                if (zipEntry.isDirectory()) {
                    ofile.mkdir();
                    continue;
                }

                try (BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(zipEntry))) {

                    if (!ofile.getParentFile().exists()) {
                        ofile.getParentFile().mkdirs();
                    }

                    try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(ofile))) {
                        byte[] buffer = new byte[1024];
                        int readSize = 0;
                        while ((readSize = in.read(buffer)) != -1) {
                            out.write(buffer, 0, readSize);
                        }
                    }
                }
            }
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
        return true;
    }
}

Related

  1. unzip(byte[] zippedBytes)
  2. unzip(final File zip, final File patchDir)
  3. unzip(final File zipFile, final String suffix)
  4. unzip(ZipEntry entry, ZipFile zipfile, File explodedDir)
  5. unzip(ZipFile zip, File fileDir)