Java Uncompress Byte Array unzip(byte[] compressedByte)

Here you can find the source of unzip(byte[] compressedByte)

Description

unzip

License

Open Source License

Declaration

public static final String unzip(byte[] compressedByte) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

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

public class Main {
    public static final String unzip(byte[] compressedByte) throws Exception {

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {/*from w  w  w . jav a  2s.  c o m*/

            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressedByte);
            zin = new ZipInputStream(in);
            ZipEntry entry = zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = new String(out.toByteArray(), "utf-8");
        } catch (Exception e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (Exception e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
        return decompressed;
    }

    public static final String unzip(byte[] compressedByte, String name) throws Exception {

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {

            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressedByte);
            zin = new ZipInputStream(in);
            ZipEntry entry = zin.getNextEntry();
            while (!name.equalsIgnoreCase(entry.getName())) {
                entry = zin.getNextEntry();
            }
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = new String(out.toByteArray(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (Exception e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
        return decompressed;
    }
}

Related

  1. unzip(byte[] blob)
  2. unzip(byte[] bytes)
  3. unzip(byte[] bytes)
  4. unzip(byte[] bytes)
  5. unzip(byte[] bytes, String encoding)
  6. unzip(byte[] compressedData)
  7. unzip(byte[] content)
  8. unzip(byte[] content)
  9. unZip(byte[] contents)