Extract zipfile to outdir with complete directory structure - Java File Path IO

Java examples for File Path IO:Zip File

Description

Extract zipfile to outdir with complete directory structure

Demo Code


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

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

public class Main {
    /***/*from   w w w  . ja  v  a 2  s  .  c om*/
     * Extract zipfile to outdir with complete directory structure
     * @param zipfile Input .zip file
     * @param outputDirectory Output directory
     */
    public static boolean extract(File zipfile, File outputDirectory) {
        boolean isSuccessful = false;
        try {
            isSuccessful = extract(new FileInputStream(zipfile),
                    outputDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSuccessful;
    }

    /***
     * Extract zipfile to outdir with complete directory structure
     * @param zipfile Input .zip file
     * @param outputDirectory Output directory
     */

    public static boolean extract(InputStream zipfile, File outputDirectory) {
        boolean isSuccessful = false;
        try {
            ZipInputStream zin = new ZipInputStream(zipfile);
            ZipEntry entry;
            String name, dir;
            while ((entry = zin.getNextEntry()) != null) {
                name = entry.getName();
                if (entry.isDirectory()) {
                    mkdirs(outputDirectory, name);
                    continue;
                }
                /* this part is necessary because file entry can come before
                 * directory entry where is file located
                 * i.e.:
                 *   /foo/foo.txt
                 *   /foo/
                 */
                dir = dirpart(name);
                if (dir != null) {
                    mkdirs(outputDirectory, dir);
                }
                extractFile(zin, outputDirectory, name);
            }
            zin.close();
            // After the extraction is done - update the flag
            isSuccessful = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSuccessful;
    }

    private static void mkdirs(File outputDirectory, String path) {
        File d = new File(outputDirectory, path);
        if (!d.exists()) {
            d.mkdirs();
        }
    }

    private static String dirpart(String name) {
        int s = name.lastIndexOf(File.separatorChar);
        return s == -1 ? null : name.substring(0, s);
    }

    private static void extractFile(ZipInputStream in,
            File outputDirectory, String name) throws IOException {
        final int BUFFER_SIZE = 1024;
        byte[] buffer = new byte[BUFFER_SIZE];
        BufferedOutputStream out = new BufferedOutputStream(
                new FileOutputStream(new File(outputDirectory, name)));
        int count;
        while ((count = in.read(buffer)) != -1) {
            out.write(buffer, 0, count);
        }
        out.close();
    }
}

Related Tutorials