Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between GZIPOutputStream and DeflaterOutputStream?

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?

like image 558
Johnannes Münch Avatar asked Jul 10 '11 02:07

Johnannes Münch


3 Answers

The differences between DeflaterOutputStream, ZIPOutputStream, and GZIPOutputStream reflect the difference between their historic compression predecessors:

  • deflate
  • zip
  • gzip

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.

like image 182
Perception Avatar answered Oct 08 '22 19:10

Perception


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.

like image 32
g051051 Avatar answered Oct 08 '22 20:10

g051051


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.

like image 23
Femi Avatar answered Oct 08 '22 19:10

Femi