GZIPOutputStream
is just a subclass of DeflaterOutputStream
but both can be instantiated. When do I use one over the other? Is the compression the same?
The differences between DeflaterOutputStream, ZIPOutputStream, and GZIPOutputStream reflect the difference between their historic compression predecessors:
respectively. Deflate can be considered the reference implementation of the compression algorith, while ZIP and GZIP are 'extensions' to it. Both the latter support the concept of 'archives', which is much more evident in the Java API for ZipOutputStream.
If you are simply trying to compress a data stream I would recommend using the DeflaterOutputStream, but if you are creating an archive you should probably look at ZipOutputStream instead.
DeflaterOutputStream implements the raw deflate compression method. GZIPOutputStream adds additional logic required for GZIP: CRC-32 checking, the GZIP magic number, GZIP header, trailer, etc. See the source for GZIPOutputStream.java and DeflaterOutputStream.java.
The difference is in the specific compression format used. From the javadoc for DeflaterOutputStream:
public class DeflaterOutputStream
extends FilterOutputStream
This class implements an output stream filter for compressing data in the "deflate" compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream.
GZIPOutputStream says:
public class GZIPOutputStream
extends DeflaterOutputStream
This class implements a stream filter for writing compressed data in the GZIP file format.
The GZIP stream extends the Deflate implementation with additional details specific to the GZIP file format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With