Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read(byte[] b, int off, int len) throws IOException 

Source Link

Document

Reads from the current ZIP entry into an array of bytes.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "d.zip";
    CheckedInputStream checksum = new CheckedInputStream(new FileInputStream(zipname), new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;//from   w w  w .j av  a  2 s .c om

    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        int size;
        byte[] buffer = new byte[2048];

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entry.getName()),
                buffer.length);
        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }

    zis.close();
    System.out.println("Checksum = " + checksum.getChecksum().getValue());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;//from   w  w  w .j ava2  s  .  c o  m

    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String destinationname = "d:\\";
    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;//from  w ww. ja v  a2s.  co m
    zipinputstream = new ZipInputStream(new FileInputStream("fileName"));
    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);
        String directory = newFile.getParent();

        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }
        fileoutputstream = new FileOutputStream(destinationname + entryName);
        int n;
        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }
        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) {
    byte[] b = null;
    try {/*from  w w w. j  a v a 2  s.com*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] unZip(byte[] data) {
    byte[] b = null;
    try {/*  w w  w .ja  v  a  2s.co m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) throws IOException {
    byte[] b = null;
    try {//  w  ww . j  a va 2  s.  co m
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);

        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:ch.rgw.compress.CompEx.java

public static byte[] expand(InputStream in) {
    ByteArrayOutputStream baos;//from ww w  .j  av a 2 s  . c o m
    byte[] siz = new byte[4];
    try {
        in.read(siz);
        long size = BinConverter.byteArrayToInt(siz, 0);
        long typ = size & ~0x1fffffff;
        size &= 0x1fffffff;
        byte[] ret = new byte[(int) size];

        switch ((int) typ) {
        case BZIP2:
            CBZip2InputStream bzi = new CBZip2InputStream(in);
            int off = 0;
            int l = 0;
            while ((l = bzi.read(ret, off, ret.length - off)) > 0) {
                off += l;
            }

            bzi.close();
            in.close();
            return ret;
        case GLZ:
            GLZ glz = new GLZ();
            baos = new ByteArrayOutputStream();
            glz.expand(in, baos);
            return baos.toByteArray();
        case HUFF:
            HuffmanInputStream hin = new HuffmanInputStream(in);
            off = 0;
            l = 0;
            while ((l = hin.read(ret, off, ret.length - off)) > 0) {
                off += l;
            }
            hin.close();
            return ret;
        case ZIP:
            ZipInputStream zi = new ZipInputStream(in);
            zi.getNextEntry();
            off = 0;
            l = 0;
            while ((l = zi.read(ret, off, ret.length - off)) > 0) {
                off += l;
            }

            zi.close();
            return ret;
        default:
            throw new Exception("Invalid compress format");
        }
    } catch (Exception ex) {
        ExHandler.handle(ex);
        return null;
    }

}

From source file:nl.clockwork.common.util.Utils.java

public static byte[] unzip(byte[] content) throws IOException {
    int BUFFER_SIZE = 2048;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry entry = zin.getNextEntry();
    if (entry != null) {
        int count;
        byte buffer[] = new byte[BUFFER_SIZE];
        BufferedOutputStream bout = new BufferedOutputStream(out, BUFFER_SIZE);
        while ((count = zin.read(buffer, 0, BUFFER_SIZE)) != -1)
            bout.write(buffer, 0, count);
        bout.flush();/*from w  ww  . j  a va  2  s  . c o m*/
        bout.close();
        zin.close();
    }
    return out.toByteArray();
}

From source file:org.openecomp.sdc.common.util.ZipUtil.java

public static byte[] unzip(byte[] zipped) {
    ZipInputStream zipinputstream = null;
    ByteArrayOutputStream outputStream = null;
    try {/* w  w  w  .j  av  a 2  s .co m*/
        byte[] buf = new byte[1024];

        zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped));
        ZipEntry zipentry = zipinputstream.getNextEntry();
        outputStream = new ByteArrayOutputStream();
        int n;
        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            outputStream.write(buf, 0, n);
        }

        return outputStream.toByteArray();
    } catch (Exception e) {
        throw new IllegalStateException("Can't unzip input stream", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                log.debug("Failed to close output stream", e);
            }
        }
        if (zipinputstream != null) {
            try {
                zipinputstream.closeEntry();
                zipinputstream.close();
            } catch (IOException e) {
                log.debug("Failed to close zip input stream", e);
            }
        }
    }
}

From source file:net.grinder.util.LogCompressUtils.java

/**
 * Decompress the given the {@link InputStream} into the given {@link OutputStream}.
 *
 * @param inputStream  input stream of the compressed file
 * @param outputStream file to be written
 * @param limit        the limit of the output
 *//*  www .j  a va  2s. c om*/
public static void decompress(InputStream inputStream, OutputStream outputStream, long limit) {
    ZipInputStream zipInputStream = null;
    try {
        zipInputStream = new ZipInputStream(inputStream);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count;
        long total = 0;
        checkNotNull(zipInputStream.getNextEntry(), "In zip, it should have at least one entry");
        do {
            while ((count = zipInputStream.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                total += count;
                if (total >= limit) {
                    break;
                }
                outputStream.write(buffer, 0, count);
            }
        } while (zipInputStream.getNextEntry() != null);
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error("Error occurs while decompressing {}", e.getMessage());
        LOGGER.debug("Details : ", e);
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}