Java Unzip File unzip(File aFile)

Here you can find the source of unzip(File aFile)

Description

Unzips the given file into the destination file.

License

Open Source License

Declaration

public static void unzip(File aFile) 

Method Source Code


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

import java.util.zip.*;

public class Main {
    /**/*from w  ww.java  2  s . c  om*/
     * Unzips the given file into the destination file.
     */
    public static void unzip(File aFile) {
        // Catch exceptions
        try {

            // Get new file input stream for file
            FileInputStream fis = new FileInputStream(aFile);

            // Get zip input stream
            ZipInputStream zis = new ZipInputStream(fis);

            // Create stream buffer
            byte buffer[] = new byte[8192];

            // Iterate over zip entries
            for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {

                // Get entry output file
                File outputFile = new File(aFile.getParent(), entry.getName());

                // If directory, just create and skip
                if (entry.isDirectory()) {
                    outputFile.mkdir();
                    continue;
                }

                // Write the files to the disk
                FileOutputStream fos = new FileOutputStream(outputFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos, 8192);
                for (int count = zis.read(buffer, 0, 8192); count != -1; count = zis.read(buffer, 0, 8192))
                    dest.write(buffer, 0, count);

                // Flush and close output stream
                dest.flush();
                dest.close();
            }

            // Close zip
            zis.close();

            // Catch exceptions
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. uncompress(ZipInputStream zipIn)
  2. uncompressDirectory(File zipSource, File target)
  3. uncompressEveryFileFromDirectory(File srcPath, File dstPath)
  4. unCompressGzipFile(String path)
  5. uncompressZipEntry(ZipInputStream zis, ZipEntry zipEntry, String dest)
  6. unzip(File archive)
  7. unzip(File archive, File output)
  8. unzip(File archiveFile, File destination)
  9. unzip(File archiveFile, File destination)