Java Uncompress Byte Array unzip(byte[] zipData, File directory)

Here you can find the source of unzip(byte[] zipData, File directory)

Description

unzip

License

Open Source License

Declaration

public static File unzip(byte[] zipData, File directory) throws IOException 

Method Source Code


//package com.java2s;
/*//from  www  . j ava 2 s.  c om
 * ZipUtils.java
 *
 * Network Embedded Sensor Testbed (NESTbed)
 *
 * Copyright (C) 2006-2007
 * Dependable Systems Research Group
 * School of Computing
 * Clemson University
 * Andrew R. Dalton and Jason O. Hallstrom
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 *
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301, USA.
 */

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private final static int BUFFER_SIZE = 4096;

    public static File unzip(byte[] zipData, File directory) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
        ZipInputStream zis = new ZipInputStream(bais);
        ZipEntry entry = zis.getNextEntry();
        File root = null;

        while (entry != null) {
            if (entry.isDirectory()) {
                File f = new File(directory, entry.getName());
                f.mkdir();

                if (root == null) {
                    root = f;
                }
            } else {
                BufferedOutputStream out;
                out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                        BUFFER_SIZE);

                // ZipInputStream can only give us one byte at a time...
                for (int data = zis.read(); data != -1; data = zis.read()) {
                    out.write(data);
                }
                out.close();
            }

            zis.closeEntry();
            entry = zis.getNextEntry();
        }
        zis.close();

        return root;
    }
}

Related

  1. unzip(byte[] datas)
  2. unzip(byte[] in)
  3. unZip(byte[] input)
  4. unzip(byte[] output)
  5. unzip(byte[] src)
  6. unZip(byte[] zipFile)