Java provides a class named InflaterInputStream this class is used to un-compress a compressed file.
The read() method of this class reads a single byte of compressed data from the input stream. To un-compress a compressed file using this method −
- Create a FileInputStream object, bypassing the path of the compressed file in String format, as a parameter to its constructor.
- Create a FileOutputStream object, bypassing the path of the output file (uncompressed image file), in String format, as a parameter to its constructor.
- Create an InflaterInputStream object, bypassing the above created FileOutputStream object, as a parameter to its constructor.
- Then, read the contents of the InflaterInputStream object and write using the write() method of the FileOutputStream class.
Example
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.InflaterInputStream; public class DeCompressingFiles { public static void main(String args[]) throws IOException { StringinputPath ="D:\\ExampleDirectory\\compressed.txt"; //Instantiating the FileInputStream FileInputStream inputStream = new FileInputStream(inputPath); String outputpath = "D:\\ExampleDirectory\\output.jpg"; FileOutputStream outputStream = new FileOutputStream(outputpath); InflaterInputStream decompresser = new InflaterInputStream(inputStream); int contents; while ((contents=decompresser.read())!=-1){ outputStream.write(contents); } //close the file outputStream.close(); decompresser.close(); System.out.println("File un-compressed......."); } }
Output
File un-compressed.......