Java Gunzip Byte Array gunzip(byte[] contentBytes)

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

Description

gunzip

License

Open Source License

Declaration

public static byte[] gunzip(byte[] contentBytes) throws IOException 

Method Source Code

//package com.java2s;
/*//ww  w  .  j  a v a 2  s .  c  o  m
 * Syncany, www.syncany.org
 * Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com> 
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.util.zip.GZIPInputStream;

public class Main {
    public static byte[] gunzip(byte[] contentBytes) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copy(new GZIPInputStream(new ByteArrayInputStream(contentBytes)), out);

        byte[] result = out.toByteArray();
        out.close();

        return result;
    }

    public static void copy(File src, File dst) throws IOException {
        InputStream srcStream = new FileInputStream(src);
        OutputStream dstStream = new FileOutputStream(dst);
        copy(srcStream, dstStream);

        srcStream.close();
        dstStream.close();
    }

    public static void copy(InputStream in, OutputStream out) throws IOException {
        // Performance tests say 4K is the fastest (sschellh)
        byte[] buf = new byte[4096];

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }

    /**
     * Allows throttling local copy operations.
     *
     * @param src
     * @param dst
     * @param kbps
     * @throws IOException
     */
    public static void copy(File src, File dst, int kbps) throws IOException {
        if (kbps <= 0) {
            copy(src, dst);
            return;
        }

        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        int bytesPer100ms = Math.round(((float) kbps) * 1024 / 10);
        //System.out.println(new Date()+" -- bytes per 100ms:"+bytesPer100ms);
        byte[] buf = new byte[bytesPer100ms];

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
            //System.out.println(new Date()+" -- copy "+len);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
        }

        //System.out.println(new Date()+" -- fertig");

        in.close();
        out.close();
    }
}

Related

  1. gunzip(byte src[], byte default_value[])
  2. gunzip(byte[] bytes)
  3. gunzip(byte[] bytes)
  4. gunzip(byte[] bytes)
  5. gunzip(byte[] data)
  6. gunzip(byte[] data)
  7. gunzip(byte[] data)
  8. gunzip(byte[] in)