Java Unzip File unzip(File zip, File extractTo)

Here you can find the source of unzip(File zip, File extractTo)

Description

Desenzippa un fitxer .zip en un directori

License

Open Source License

Parameter

Parameter Description
zip a parameter
extractTo a parameter

Declaration

public static void unzip(File zip, File extractTo) 

Method Source Code


//package com.java2s;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import java.io.InputStream;

public class Main {
    /**/* w ww  .j  a  va2  s  . c om*/
     * Desenzippa un fitxer .zip en un directori
     *
     * @param zip
     * @param extractTo
     */
    public static void unzip(File zip, File extractTo) {
        try {
            ZipFile archive = new ZipFile(zip);
            Enumeration<?> e = archive.entries();
            while (e.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                File file = new File(extractTo, entry.getName());
                if (entry.isDirectory() && !file.exists()) {
                    file.mkdirs();
                } else if (!entry.isDirectory()) {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }

                    InputStream in = archive.getInputStream(entry);
                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));

                    byte[] buffer = new byte[8192];
                    int read;
                    while (-1 != (read = in.read(buffer))) {
                        out.write(buffer, 0, read);
                    }
                    in.close();
                    out.close();
                }
            }
            archive.close();
        } catch (IOException e) {
            System.err.println("Error desenzippant el fitxer: " + zip.getName() + " al directori: " + extractTo
                    + ". Error: " + e.getMessage());
        }
    }
}

Related

  1. unZip(File zip, File dest)
  2. unzip(File zip, File destination)
  3. unzip(File zip, File dir)
  4. unzip(File zip, File directory)
  5. unzip(File zip, File extractTo)
  6. unzip(File zip, File location)
  7. unzip(File zip, File targetDir)
  8. unzip(File zip, File toDir)
  9. unzip(File zip, File toDir)