Java Decompress Byte Array decompress(byte[] source)

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

Description

decompress

License

Open Source License

Declaration

public static byte[] decompress(byte[] source) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w ww  . ja  v a 2  s  .c o m*/
 * Copyright (c) 2011, 2014 Eurotech and/or its affiliates
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Eurotech
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class Main {
    public static byte[] decompress(byte[] source) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = new ByteArrayInputStream(source);
        GZIPInputStream gzipis = null;

        try {
            gzipis = new GZIPInputStream(bais);

            int n;
            final int MAX_BUF = 1024;
            byte[] buf = new byte[MAX_BUF];
            while ((n = gzipis.read(buf, 0, MAX_BUF)) != -1) {
                baos.write(buf, 0, n);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (gzipis != null) {
                try {
                    gzipis.close();
                } catch (IOException e) {
                    // Ignore
                }
            }

            try {
                baos.close();
            } catch (IOException e) {
                // Ignore
            }
        }

        return baos.toByteArray();
    }
}

Related

  1. decompress(byte[] data, int off, int len)
  2. decompress(byte[] data, int offset, int length)
  3. decompress(byte[] gzipped)
  4. decompress(byte[] in)
  5. decompress(byte[] input)
  6. decompress(byte[] source)
  7. decompress(byte[] src, Inflater decompresser, int compressCycleSize)
  8. decompress(byte[] str)
  9. decompress(byte[] zipByte)