Java Unzip File unzip(File archive, File output)

Here you can find the source of unzip(File archive, File output)

Description

Unzip a ZIP archive into the specified output directory

License

Open Source License

Parameter

Parameter Description
archive a ZIP archive file
output the directory where the ZIP archive file has to be unzipped

Declaration

public static void unzip(File archive, File output) 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**//w  w w .j  av  a 2  s .co m
     * Unzip a ZIP archive into the specified output directory
     * @param archive a ZIP archive file
     * @param output the directory where the ZIP archive file has to be unzipped
     */
    public static void unzip(File archive, File output) {
        try {
            if (!output.exists()) {
                output.mkdir();

            }

            ZipInputStream zis = new ZipInputStream(new FileInputStream(archive));
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(output, fileName);

                // create all non-existing folders to avoid FileNotFoundException
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                byte[] buffer = new byte[1024];
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);

                }

                fos.close();
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();

        }
    }
}

Related

  1. uncompressEveryFileFromDirectory(File srcPath, File dstPath)
  2. unCompressGzipFile(String path)
  3. uncompressZipEntry(ZipInputStream zis, ZipEntry zipEntry, String dest)
  4. unzip(File aFile)
  5. unzip(File archive)
  6. unzip(File archiveFile, File destination)
  7. unzip(File archiveFile, File destination)
  8. unzip(File archiveFile, File targetDir, boolean skipRoot)
  9. unzip(File dest, String jar)