It extract all files from a ZIP File to destiny path. - Java java.lang

Java examples for java.lang:byte Array Compress

Description

It extract all files from a ZIP File to destiny path.

Demo Code


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        File zippedFile = new File("Main.java");
        File fileDestiny = new File("Main.java");
        extractZipFile(zippedFile,fileDestiny);
    }/*from w  w  w  .  ja va  2s.  co m*/
    private static final Logger LOG = Logger.getLogger(CompressUtils.class);
    /**
     * It extract all files from a ZIP File to destiny path. <br/>
     * Prefer to use the method
     * 
     * @param zippedFile
     *          zipped file.
     * @param fileDestiny
     *          path where the files are saved.
     * @throws IOException
     *           when an error occurs.
     */
    public static void extractZipFile(File zippedFile, File fileDestiny)
            throws IOException {
        InputStream input = null;
        input = new FileInputStream(zippedFile);
        extractZipFile(input, fileDestiny);
    }
    /**
     * It extract all files from a ZIP File to destiny path.
     * 
     * @param input
     *          InputStream of the Zip file.
     * @param destiny
     *          path where the files are saved.
     * @throws IOException
     *           when an error occurs.
     */
    public static void extractZipFile(InputStream input, File destiny)
            throws IOException {
        ZipEntry entry = null;
        ZipInputStream zinputStream = null;

        zinputStream = new ZipInputStream(new BufferedInputStream(input));

        while ((entry = zinputStream.getNextEntry()) != null) {

            if (entry.isDirectory()) {
                String currentEntry = entry.getName();
                File destFile = new File(destiny, currentEntry);
                destFile.mkdirs();

            } else {
                String currentEntry = entry.getName();

                File destFile = new File(destiny, currentEntry);
                File destinationParent = destFile.getParentFile();

                // create the parent directory structure if needed
                destinationParent.mkdirs();

                LOG.debug("Extracting: " + entry);

                int count;
                byte data[] = new byte[StreamUtils.BUFFER_BYTES];
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                        StreamUtils.BUFFER_BYTES);
                while ((count = zinputStream.read(data, 0,
                        StreamUtils.BUFFER_BYTES)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                StreamUtils.closeSilently(dest);
            }
        }
        StreamUtils.closeSilently(zinputStream);
    }
}

Related Tutorials