The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.zip  [9 examples]

e450. Decompressing a Byte Array

This example decompresses a byte array that was compressed using the Deflater class (see e449 Compressing a Byte Array).
    // Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    
    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    
    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();

 Related Examples
e449. Compressing a Byte Array

See also: Checksums    GZIP    ZIP   


© 2002 Addison-Wesley.