Java Unzip File unzip(File intoFolder, ZipFile zipFile)

Here you can find the source of unzip(File intoFolder, ZipFile zipFile)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File intoFolder, ZipFile zipFile)
            throws FileNotFoundException, IOException 

Method Source Code

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

import java.io.File;

import java.io.FileNotFoundException;
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 intoFolder, ZipFile zipFile)
            throws FileNotFoundException, IOException {
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();

            String name = zipEntry.getName();
            String nameFixed = name.replaceAll("\\\\", "/");
            //long size = zipEntry.getSize();
            //long compressedSize = zipEntry.getCompressedSize();
            ///System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n",
            //        name, size, compressedSize);

            File file = new File(intoFolder, nameFixed);
            if (nameFixed.endsWith("/")) {
                file.mkdirs();//from   w  w w. j  a v a 2 s  .  co  m
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    }
}

Related

  1. unzip(File input)
  2. unzip(File input, File outputDir)
  3. unzip(File input, File outputDirectory)
  4. unzip(File inputFile, File outputDir)
  5. unzip(File inputFile, File unzipDestFolder)
  6. unzip(File jar, File target)
  7. unzip(File jarFile, File destDir)
  8. unzip(File sourceFile, File rootDir)
  9. unzip(File sourceZipfile, File directory)