Java Unzip to Folder unzip(String zipFileName, String unzipdir)

Here you can find the source of unzip(String zipFileName, String unzipdir)

Description

unzip

License

Open Source License

Declaration

public static void unzip(String zipFileName, String unzipdir) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.util.zip.ZipInputStream;

public class Main {
    public static void unzip(String zipFileName, String unzipdir) {
        try (ZipInputStream zis = new ZipInputStream(
                new BufferedInputStream(new FileInputStream(zipFileName)))) {

            // Read each entry from the ZIP file 
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                // Extract the entry's contents
                extractEntryContent(zis, entry, unzipdir);
            }//from w  w w.j  a  va  2 s. c  om

            System.out
                    .println("ZIP file's contents have been extracted to "
                            + (new File(unzipdir)).getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void extractEntryContent(ZipInputStream zis,
            ZipEntry entry, String unzipdir) throws IOException,
            FileNotFoundException {

        String entryFileName = entry.getName();
        String entryPath = unzipdir + File.separator + entryFileName;

        // Create the entry file by creating necessary directories
        createFile(entryPath);

        // Create an output stream to extract the content of the 
        // zip entry and write to the new file
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(entryPath));

        byte[] buffer = new byte[1024];
        int count = -1;
        while ((count = zis.read(buffer)) != -1) {
            bos.write(buffer, 0, count);
        }

        bos.close();
    }

    public static void createFile(String filePath) throws IOException {
        File file = new File(filePath);
        File parent = file.getParentFile();

        // Create all parent directories if they do not exist
        if (!parent.exists()) {
            parent.mkdirs();
        }

        file.createNewFile();
    }
}

Related

  1. unzip(String zipFile, String targetFolder, String... fileSuffixes)
  2. unzip(String zipFile, String targetPath)
  3. unzip(String zipFileName, String outputDirectory)
  4. unzip(String zipFileName, String targetFolderPath)
  5. unzip(String zipFileName, String targetPath)
  6. unzipFile(String zipFile, File outputFolder)
  7. unzipFile(String zipFile, String extDir)
  8. unZipFile(String zipFile, String outputFolder)
  9. unzipFile(String zipFile, String outputFolder)