Search in sources :

Example 26 with Inflater

use of java.util.zip.Inflater in project hazelcast by hazelcast.

the class IOUtil method decompress.

public static byte[] decompress(byte[] compressedData) throws IOException {
    if (compressedData.length == 0) {
        return compressedData;
    }
    Inflater inflater = new Inflater();
    inflater.setInput(compressedData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!inflater.finished()) {
        try {
            int count = inflater.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            Logger.getLogger(IOUtil.class).finest("Decompression failed", e);
        }
    }
    bos.close();
    inflater.end();
    return bos.toByteArray();
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 27 with Inflater

use of java.util.zip.Inflater in project j2objc by google.

the class DeflaterTest method testDeflate.

public void testDeflate() throws DataFormatException {
    deflater.setInput(new byte[] { 1, 2, 3 });
    deflateInflate(Deflater.NO_FLUSH);
    assertTrue(totalInflated < 3);
    // the 3rd byte shouldn't have been flushed yet
    assertEquals(0, decompressed[2]);
    deflater.setInput(new byte[] { 4, 5, 6 });
    deflateInflate(Deflater.SYNC_FLUSH);
    assertEquals(6, totalInflated);
    assertDecompressed(1, 2, 3, 4, 5, 6);
    assertEquals(0, inflater.inflate(decompressed));
    deflater.setInput(new byte[] { 7, 8, 9 });
    deflateInflate(Deflater.FULL_FLUSH);
    assertEquals(9, totalInflated);
    assertDecompressed(1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertEquals(0, inflater.inflate(decompressed));
    // safe because we did a FULL_FLUSH
    inflater = new Inflater(true);
    deflater.setInput(new byte[] { 10, 11, 12 });
    deflateInflate(Deflater.SYNC_FLUSH);
    assertEquals(12, totalInflated);
    assertDecompressed(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    assertEquals(0, inflater.inflate(decompressed));
}
Also used : Inflater(java.util.zip.Inflater)

Example 28 with Inflater

use of java.util.zip.Inflater in project android_frameworks_base by DirtyUnicorns.

the class StrictJarFile method getZipInputStream.

private InputStream getZipInputStream(ZipEntry ze) {
    if (ze.getMethod() == ZipEntry.STORED) {
        return new RAFStream(raf, ze.getDataOffset(), ze.getDataOffset() + ze.getSize());
    } else {
        final RAFStream wrapped = new RAFStream(raf, ze.getDataOffset(), ze.getDataOffset() + ze.getCompressedSize());
        int bufSize = Math.max(1024, (int) Math.min(ze.getSize(), 65535L));
        return new ZipInflaterInputStream(wrapped, new Inflater(true), bufSize, ze);
    }
}
Also used : Inflater(java.util.zip.Inflater)

Example 29 with Inflater

use of java.util.zip.Inflater in project android_frameworks_base by ResurrectionRemix.

the class StrictJarFile method getZipInputStream.

private InputStream getZipInputStream(ZipEntry ze) {
    if (ze.getMethod() == ZipEntry.STORED) {
        return new FDStream(fd, ze.getDataOffset(), ze.getDataOffset() + ze.getSize());
    } else {
        final FDStream wrapped = new FDStream(fd, ze.getDataOffset(), ze.getDataOffset() + ze.getCompressedSize());
        int bufSize = Math.max(1024, (int) Math.min(ze.getSize(), 65535L));
        return new ZipInflaterInputStream(wrapped, new Inflater(true), bufSize, ze);
    }
}
Also used : Inflater(java.util.zip.Inflater)

Example 30 with Inflater

use of java.util.zip.Inflater in project voltdb by VoltDB.

the class FileArchiver method unarchive.

public static void unarchive(String infilename, String outfilename, FileAccess storage, int compressionType) throws IOException {
    InputStream f = null;
    OutputStream outstream = null;
    boolean completed = false;
    try {
        if (!storage.isStreamElement(infilename)) {
            return;
        }
        storage.removeElement(outfilename);
        byte[] b = new byte[COPY_BLOCK_SIZE];
        f = storage.openInputStreamElement(infilename);
        switch(compressionType) {
            case COMPRESSION_ZIP:
                f = new InflaterInputStream(f, new Inflater());
                break;
            case COMPRESSION_GZIP:
                f = new GZIPInputStream(f, b.length);
                break;
            case COMPRESSION_NONE:
                break;
            default:
                throw new RuntimeException("FileArchiver: " + compressionType);
        }
        outstream = storage.openOutputStreamElement(outfilename);
        while (true) {
            int l = f.read(b, 0, b.length);
            if (l == -1) {
                break;
            }
            outstream.write(b, 0, l);
        }
        completed = true;
    } catch (Throwable e) {
        throw FileUtil.toIOException(e);
    } finally {
        try {
            if (f != null) {
                f.close();
            }
            if (outstream != null) {
                outstream.flush();
                outstream.close();
            }
            if (!completed && storage.isStreamElement(outfilename)) {
                storage.removeElement(outfilename);
            }
        } catch (Throwable e) {
            throw FileUtil.toIOException(e);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Inflater(java.util.zip.Inflater)

Aggregations

Inflater (java.util.zip.Inflater)99 InflaterInputStream (java.util.zip.InflaterInputStream)34 IOException (java.io.IOException)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 InputStream (java.io.InputStream)24 DataFormatException (java.util.zip.DataFormatException)24 GZIPInputStream (java.util.zip.GZIPInputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Deflater (java.util.zip.Deflater)9 BufferedInputStream (java.io.BufferedInputStream)6 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 Test (org.junit.Test)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4