Java Unzip File unzip(File inputFile, File unzipDestFolder)

Here you can find the source of unzip(File inputFile, File unzipDestFolder)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File inputFile, File unzipDestFolder) 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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static void unzip(File inputFile, File unzipDestFolder) throws IOException {
        ZipFile zipFile = null;/*from  w  w  w  .  j av  a2 s .com*/
        InputStream inputStream = null;

        try {
            zipFile = new ZipFile(inputFile);

            Enumeration<? extends ZipEntry> oEnum = zipFile.entries();
            while (oEnum.hasMoreElements()) {
                ZipEntry zipEntry = oEnum.nextElement();

                File file = new File(unzipDestFolder, zipEntry.getName());
                if (zipEntry.isDirectory() && !file.exists()) {
                    file.mkdirs();
                } else {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    inputStream = zipFile.getInputStream(zipEntry);

                    BufferedInputStream buffInputStream = new BufferedInputStream(inputStream);
                    FileOutputStream fos = new FileOutputStream(file);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    int byteData;
                    while ((byteData = buffInputStream.read()) != -1) {
                        bos.write((byte) byteData);
                    }

                    bos.close();
                    fos.close();
                    buffInputStream.close();
                }
            }
        } catch (IOException ioException) {
            throw ioException;
        } finally {
            try {
                if (zipFile != null) {
                    zipFile.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ioException) {
            }
        }
    }
}

Related

  1. unZip(File inFile, File unzipDir)
  2. unzip(File input)
  3. unzip(File input, File outputDir)
  4. unzip(File input, File outputDirectory)
  5. unzip(File inputFile, File outputDir)
  6. unzip(File intoFolder, ZipFile zipFile)
  7. unzip(File jar, File target)
  8. unzip(File jarFile, File destDir)
  9. unzip(File sourceFile, File rootDir)