Java tutorial
//package com.java2s; /* * Copyright (c) 2007-2010 by The Broad Institute, Inc. and the Massachusetts Institute of Technology. * All Rights Reserved. * * This software is licensed under the terms of the GNU Lesser General Public License (LGPL), Version 2.1 which * is available at http://www.opensource.org/licenses/lgpl-2.1.php. * * THE SOFTWARE IS PROVIDED "AS IS." THE BROAD AND MIT MAKE NO REPRESENTATIONS OR WARRANTIES OF * ANY KIND CONCERNING THE SOFTWARE, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT * OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. IN NO EVENT SHALL THE BROAD OR MIT, OR THEIR * RESPECTIVE TRUSTEES, DIRECTORS, OFFICERS, EMPLOYEES, AND AFFILIATES BE LIABLE FOR ANY DAMAGES OF * ANY KIND, INCLUDING, WITHOUT LIMITATION, INCIDENTAL OR CONSEQUENTIAL DAMAGES, ECONOMIC * DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER THE BROAD OR MIT SHALL * BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE * FOREGOING. */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.Inflater; public class Main { public static byte[] decompress(byte[] data) { return decompress(data, data.length * 4); } /** * @param data -- the data to decompress * @param uncompressedChunkSize -- an estimate of the uncompressed chunk size. This need not be exact. * @return */ public static byte[] decompress(byte[] data, int uncompressedChunkSize) { // mpd: new code int rem = data.length; // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressedChunkSize); // Decompress the data byte[] outbuf = new byte[uncompressedChunkSize]; Inflater decompressor = new Inflater(); decompressor.setInput(data); while (rem > 0) { // If we are finished with the current chunk start a new one if (decompressor.finished()) { decompressor = new Inflater(); int offset = data.length - rem; decompressor.setInput(data, offset, rem); } try { int count = decompressor.inflate(outbuf, 0, outbuf.length); rem = decompressor.getRemaining(); bos.write(outbuf, 0, count); } catch (Exception e) { e.printStackTrace(); } } try { bos.close(); } catch (IOException e) { // Ignore -- no resources open } // Return the decompressed data return bos.toByteArray(); } }