Java Unzip File uncompressDirectory(File zipSource, File target)

Here you can find the source of uncompressDirectory(File zipSource, File target)

Description

uncompress Directory

License

Open Source License

Declaration

static public void uncompressDirectory(File zipSource, File target) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  w  w. ja va 2 s. c  o m
 * Copyright (C) 2011 Peransin Nicolas.
 * Use is subject to license terms.
 */

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 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

  1. uncompress(String compressedValue)
  2. uncompress(String inputCompressedFile, File outputFile)
  3. uncompress(String inputFile, String toDir)
  4. uncompress(String str)
  5. uncompress(ZipInputStream zipIn)
  6. uncompressEveryFileFromDirectory(File srcPath, File dstPath)
  7. unCompressGzipFile(String path)
  8. uncompressZipEntry(ZipInputStream zis, ZipEntry zipEntry, String dest)
  9. unzip(File aFile)