Java Reference
In-Depth Information
public static byte[] decompress(byte[] input, boolean GZIPFormat)
throws IOException, DataFormatException {
// Create an Inflater object to compress the data
Inflater decompressor = new Inflater(GZIPFormat);
// Set the input for the decompressor
decompressor.setInput(input);
// Decompress data
ByteArrayOutputStream bao = new ByteArrayOutputStream();
byte[] readBuffer = new byte[1024];
int readCount = 0 ;
while(!decompressor.finished()){
readCount = decompressor.inflate(readBuffer);
if (readCount > 0) {
// Write the data to the output stream
bao.write(readBuffer, 0, readCount);
}
}
// End the decompressor
decompressor.end();
// Return the written bytes from the output stream
return bao.toByteArray();
}
}
Input String: Hello world!
Uncompressed data length: 12
Compressed data length: 20
Decompressed data length: 12
Output String: Hello world!
You can use DeflaterInputStream and DeflaterOutputStream to compress data in the input and output
streams. There are also InflaterInputStream and InflaterOutputStream classes for decompressing data in the input
and output streams. The four classes are concrete decorators in the InputStream and OutputStream class families.
Please refer to Chapter 7 for more details on the decorator pattern and the concrete decorator classes.
Working with ZIP File Format
Java has direct support for the ZIP file format. Typically, you would be using the following four classes from the
java.util.zip package to work with the ZIP file format:
ZipEntry
ZipInputStream
ZipOutputStream
ZipFile
 
Search WWH ::




Custom Search