
- 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 compress a file in Java?
The DeflaterOutputStream class of Java is used to compress the given data and stream it out to the destination.
The write() method of this class accepts the data (in integer and byte format), compresses it and, writes it to the destination of the current DeflaterOutputStream object. To compress a file using this method &Minus;
- 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.......
- Related Articles
- How to un-compress a file in Java?
- How to compress and un-compress the data of 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 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?
- Compress String in C++

Advertisements