DeflaterOutputStream and InflaterInputStream classes are provided in Java to compress and decompress the file contents. These classes provide useful methods that can be used for compressing the file content.
Compressing a File using DeflaterOutputStream
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.
- void close() : Writes remaining compressed data to the output stream and closes the underlying stream.
- protected void deflate() : Writes next block of compressed data to the output stream.
- void finish() : Finishes writing compressed data to the output stream without closing the underlying stream.
- void flush() : Flushes the compressed output stream.
- void write(byte b, int off, int len) : Writes an array of bytes to the compressed output stream where off is the start offset of data and len is total number of bytes.
- void write(int b) : Writes a byte to the compressed output stream.
Steps to compress a file(file 1)
- Take an input file ‘file 1’ to FileInputStream for reading data.
- Take the output file ‘file 2’ and assign it to FileOutputStream . This will help to write data into ‘file2’.
- Assign FileOutputStream to DeflaterOutputStream for Compressing the data.
- Now, read data from FileInputStream and write it into DeflaterOutputStream. It will compress the data and send it to FileOutputStream which stores the compressed data into the output file.
Decompressing a File using InflaterInputStream
This class implements a stream filter for uncompressing data in the “deflate” compression format. It is also used as the basis for other decompression filters, such as GZIPInputStream.
- int available() : Returns 0 after EOF has been reached, otherwise always return 1.
- void close() : Closes the input stream and releases any system resources associated with the stream.
- protected void fill() : Fills input buffer with more data to decompress.
- void mark(int readlimit) : Marks the current position in the input stream.
- boolean markSupported() : Tests if the input stream supports the mark and reset methods.
- int read() : Reads a byte of uncompressed data.
- int read(byte b, int off, int len) : Reads decompressed data into an array of bytes to the compressed output stream where off is the start offset of data and len is total number of bytes.
- void reset() : Re-positions this stream to the position at the time the mark method was last called on this input stream.
Steps to decompress a file
- File with the name ‘file2’ now contains compressed data and we need to obtain original decompressed data from this file.
- Assign the compressed file ‘file2’ to FileInputStream. This helps to read data from ‘file2’.
- Assign the output file ‘file3’ to FileOutputStream. This will help to write uncompressed data into ‘file3’.
- Now, read uncompressed data from InflaterInputStream and write it into FileOutputStream. This will write the uncompressed data to ‘file3’.
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.