

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compress and un-compress the data of a file in Java?
Java provides a two classes namely, DeflaterOutputStream and InflaterInputStream for compressing and uncompressing data.
Compressing a Single file
To compress a single file −
- Create a FileInputStream object, by passing the path of the file to be compressed in String format, as a parameter to its constructor.
- Create a FileOutputStream object, by passing the path of the output file, in String format, as a parameter to its constructor.
- Create a DeflaterOutputStream object, by passing the above created FileOutputStream object, as a parameter to its constructor.
- Then, read the contents of the input file and write using the write() method of the DeflaterOutputStream class.
Example
import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.DeflaterOutputStream; public class CompressingFiles { public static void main(String args[]) throws IOException { //Instantiating the FileInputStream String inputPath = "D:\\ExampleDirectory\\logo.jpg"; FileInputStream inputStream = new FileInputStream(inputPath); //Instantiating the FileOutputStream String outputPath = "D:\\ExampleDirectory\\compressedLogo.txt"; FileOutputStream outputStream = new FileOutputStream(outputPath); //Instantiating the DeflaterOutputStream DeflaterOutputStream compresser = new DeflaterOutputStream(outputStream); int contents; while ((contents=inputStream.read())!=-1){ compresser.write(contents); } compresser.close(); System.out.println("File compressed......."); } }
Output
File compressed.......
Uncompressing the compressed file
To un-compress a compressed file −
- Create a FileInputStream object, by passing the path of the compressed file in String format, as a parameter to its constructor.
- Create a FileOutputStream object, by passing the path of the output file (uncompressed image file), in String format, as a parameter to its constructor.
- Create a InflaterInputStream object, by passing 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.......
- Related Questions & Answers
- How to un-compress a file in Java?
- How to compress a file in Java?
- Compress String in C++
- Best Way to compress mysqldump?
- Compress The Schedule to Hit The Target
- How we can compress large Python files?
- How to optimize and compress jpeg or png images in linux
- How to Compress files with ZIPFILE module in Python.
- Compress array to group consecutive elements JavaScript
- How to compress Python objects before saving to cache?
- The best way to compress and extract files using the tar command on linux
- How to read the data from a file in Java?
- How to append data to a file in Java?
- How to read the data from a properties file in Java?
- How to read the data from a CSV file in Java?
Advertisements