Example usage for java.util.zip GZIPInputStream GZIPInputStream

List of usage examples for java.util.zip GZIPInputStream GZIPInputStream

Introduction

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

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:ByteUtils.java

public static byte[] unpackRaw(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);

    GZIPInputStream zis = new GZIPInputStream(bais);
    byte[] tmpBuffer = new byte[256];
    int n;/*  w  w  w .  ja v  a 2s .  c o  m*/
    while ((n = zis.read(tmpBuffer)) >= 0)
        baos.write(tmpBuffer, 0, n);
    zis.close();

    return baos.toByteArray();
}

From source file:azkaban.utils.GZIPUtils.java

public static byte[] unGzipBytes(byte[] bytes) throws IOException {
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(gzipInputStream, byteOutputStream);

    return byteOutputStream.toByteArray();
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.Compression.java

public static byte[] decompress(byte[] plaintext) {
    try {//from  w w  w.  ja  v  a2 s  . c o m
        ByteArrayOutputStream writer = new ByteArrayOutputStream();
        ByteArrayInputStream reader = new ByteArrayInputStream(plaintext);
        GZIPInputStream gzipStream = new GZIPInputStream(reader);
        IOUtils.copy(gzipStream, writer);
        gzipStream.close();
        writer.flush();
        writer.close();
        return writer.toByteArray();
    } catch (IOException ex) {
        return null; // Invalid source data
    }
}

From source file:com.vincestyling.netroid.HttpUtils.java

/**
 * Reads the contents of HttpEntity into a byte[].
 *//* w w  w.j ava  2  s .  c  o m*/
public static byte[] responseToBytes(HttpResponse response) throws IOException, ServerError {
    HttpEntity entity = response.getEntity();
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(ByteArrayPool.get(),
            (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (isGzipContent(response) && !(in instanceof GZIPInputStream)) {
            in = new GZIPInputStream(in);
        }

        if (in == null) {
            throw new ServerError();
        }

        buffer = ByteArrayPool.get().getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            NetroidLog.v("Error occured when calling consumingContent");
        }
        ByteArrayPool.get().returnBuf(buffer);
        bytes.close();
    }
}

From source file:Zip.java

/**
 * Reads a GZIP file and dumps the contents to the console.
 */// w ww.j a  v  a2 s  . c  om
public static void readGZIPFile(String fileName) {
    // use BufferedReader to get one line at a time
    BufferedReader gzipReader = null;
    try {
        // simple loop to dump the contents to the console
        gzipReader = new BufferedReader(
                new InputStreamReader(new GZIPInputStream(new FileInputStream(fileName))));
        while (gzipReader.ready()) {
            System.out.println(gzipReader.readLine());
        }
        gzipReader.close();
    } catch (FileNotFoundException fnfe) {
        System.out.println("The file was not found: " + fnfe.getMessage());
    } catch (IOException ioe) {
        System.out.println("An IOException occurred: " + ioe.getMessage());
    } finally {
        if (gzipReader != null) {
            try {
                gzipReader.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.grosscommerce.ICEcat.utilities.Downloader.java

public static void download(String urlFrom, String login, String pwd, OutputStream destStream)
        throws Exception {
    try {/*from  w  w w. j av  a2s  . c  o  m*/
        HttpURLConnection uc = prepareConnection(urlFrom, login, pwd);

        uc.connect();

        if (uc.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Error, code: {0}, message {1}",
                    new Object[] { uc.getResponseCode(), uc.getResponseMessage() });

            return;
        }

        BufferedInputStream is = null;

        if ((uc.getContentEncoding() != null && uc.getContentEncoding().toLowerCase().equals("gzip"))
                || uc.getContentType() != null && uc.getContentType().toLowerCase().contains("gzip")) {
            is = new BufferedInputStream(new GZIPInputStream(uc.getInputStream()));

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download gzip data from: {0}",
                    urlFrom);
        } else {
            is = new BufferedInputStream(uc.getInputStream());

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO,
                    "Will download not compressed data from:{0}", urlFrom);
        }

        StreamsHelper.copy(is, destStream);
        destStream.flush();

    } catch (Exception ex) {
        Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, "URL: " + urlFrom, ex);

        throw ex;
    }
}

From source file:com.cenrise.test.azkaban.GZIPUtils.java

public static byte[] unGzipBytes(final byte[] bytes) throws IOException {
    final ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
    final GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

    final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(gzipInputStream, byteOutputStream);

    return byteOutputStream.toByteArray();
}

From source file:de.dentrassi.pm.utils.deb.Packages.java

public static Map<String, String> parseControlFile(final File packageFile) throws IOException, ParserException {
    try (final ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(packageFile))) {
        ArchiveEntry ar;/* ww  w  .  jav  a  2s. c om*/
        while ((ar = in.getNextEntry()) != null) {
            if (!ar.getName().equals("control.tar.gz")) {
                continue;
            }
            try (final TarArchiveInputStream inputStream = new TarArchiveInputStream(new GZIPInputStream(in))) {
                TarArchiveEntry te;
                while ((te = inputStream.getNextTarEntry()) != null) {
                    String name = te.getName();
                    if (name.startsWith("./")) {
                        name = name.substring(2);
                    }
                    if (!name.equals("control")) {
                        continue;
                    }
                    return parseControlFile(inputStream);
                }
            }
        }
    }
    return null;
}

From source file:halive.shootinoutside.common.core.game.map.GameMap.java

public static GameMap createFromByteArray(byte[] b) throws IOException, ParseException {
    ByteArrayInputStream in = new ByteArrayInputStream(b);
    GZIPInputStream input = new GZIPInputStream(in);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int j;// ww w. j  av a2  s  . c  o m
    while ((j = input.read()) != -1) {
        out.write(j);
    }
    String s = new String(out.toByteArray());
    return loadGameMapFromJSONString(s);
}

From source file:bencoding.securely.Converters.java

public static Object deserializeObjectFromString(String objectString) throws Exception {

    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
            Base64.decode(objectString, Base64.DEFAULT));
    GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
    ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);

    Object object = objectInputStream.readObject();

    objectInputStream.close();/*from   www  . j av  a 2 s. co  m*/
    gzipInputStream.close();
    arrayInputStream.close();

    return object;
}