
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to un-compress a file in Java?
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.......
- Related Articles
- How to compress and un-compress the data of a file in Java?
- How to compress a file in Java?
- How to truncate a file in Java?
- How to create a pdf file in Java?
- How to delete a temporary file in Java?
- How to convert a Kotlin source file to a Java source file?
- How to append data to a file in Java?
- How do I un-escape a backslash-escaped string in Python?
- How to search a file in a directory in java
- How to Compress files with ZIPFILE module in Python.
- How to convert File into a Stream in Java?
- How to open a plain text file in Java?
- How to compress Python objects before saving to cache?
- How to use file class in Java?
- How to set file permissions in Java?

Advertisements