- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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 to Compress files with ZIPFILE module in Python.
- How to optimize and compress jpeg or png images in linux
- How to compress Python objects before saving to cache?
- How we can compress large Python files?
- Compress array to group consecutive elements JavaScript
- The best way to compress and extract files using the tar command on linux
- Liquids and gases can be compressed but it is difficult to compress solids. Why?
- Sponge is solid, but we can still compress it. Why?
- How to read the data from a file in Java?
- How to append data to a file in Java?

Advertisements