uncompress Directory - Java java.lang

Java examples for java.lang:byte Array Compress

Description

uncompress Directory

Demo Code

/*//from  w w w.j  a va 2s. c  o  m
 * Copyright (C) 2011 Peransin Nicolas.
 * Use is subject to license terms.
 */
//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        File zipSource = new File("Main.java");
        File target = new File("Main.java");
        uncompressDirectory(zipSource, target);
    }

    public static final String ZIP_FILE_SEPARATOR = "/";
    public static final int BUFFER_SIZE = 2156;
    public static final boolean DEBUG = false;

    static public void uncompressDirectory(File zipSource, File target)
            throws IOException {
        // Target is the zip
        // Source is the directory

        //create a ZipInputStream to unzip the data from.

        ZipInputStream zis = new ZipInputStream(new FileInputStream(
                zipSource));

        //assuming that there is a directory named inFolder (If there 
        //isn't create one) in the same directory as the one the code 
        // runs from, call the zipDir method
        try {
            byte[] readBuffer = new byte[BUFFER_SIZE];

            for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis
                    .getNextEntry()) {
                String relativeName = entry.getName();
                if (DEBUG)
                    System.out.println("- entry : " + relativeName);

                int start = 0;
                File dir = target;
                for (int index = relativeName.indexOf(ZIP_FILE_SEPARATOR,
                        start); index != -1; index = relativeName.indexOf(
                        ZIP_FILE_SEPARATOR, start)) {
                    String pathName = relativeName.substring(start, index);
                    dir = new File(dir, pathName);
                    start = index + ZIP_FILE_SEPARATOR.length();
                }
                dir.mkdirs();

                if (entry.isDirectory()) {
                    // Do we create empty directory or not ?
                    // boolean c = new File(dir, name).mkdirs();
                    // if (DEBUG) {
                    //    System.out.println("read dir ["+dir.exists()+"] : " + dir.getPath());
                    // }
                    continue;
                }

                String name = relativeName.substring(start);

                FileOutputStream fos = new FileOutputStream(new File(dir,
                        name));
                try {
                    for (int bytesIn = zis.read(readBuffer); (bytesIn != -1); bytesIn = zis
                            .read(readBuffer)) {
                        fos.write(readBuffer, 0, bytesIn);
                    }
                } finally {
                    fos.close();
                }
                if (DEBUG) {
                    File f = new File(dir, name);
                    //                    System.out.println("read file (" + f.length() + " defined as "
                    //                                + entry.getSize() + " ) defined : " + f);
                }
            }
        } finally {
            zis.close();
        }
    }
}

Related Tutorials