• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Compressing and Uncompressing File Example in Java

January 22, 2014 //  by Krishna Srinivasan//  Leave a Comment

Compressing and Uncompressing of the files is one of the common utility we use in our daily life. If you are using windows, using WinZip is very common to compress the files and sent to others. The advantage of compressing the files is to save the memory occupied for the file in the disk. It can be Uncompressed when it is really meeded.In Java IO package, the DeflaterOutputStream and InflaterInputStream classes provide mechanism to compress and uncompress the data in the deflate compression format.

Look at the below example to Compress and Uncompress a file.

Compressing and Uncompressing File Example

CompressFileExample.java

package javabeat.net.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.DeflaterOutputStream;

public class CompressFileExample {
	public static void main(String args[]) {

		try {
			FileInputStream fin = new FileInputStream("TextFile.txt");

			FileOutputStream fout = new FileOutputStream("Compressed");
			DeflaterOutputStream out = new DeflaterOutputStream(fout);

			int i;
			while ((i = fin.read()) != -1) {
				out.write((byte) i);
				out.flush();
			}
			System.out.println("File is compressed!!");
			fin.close();
			out.close();

		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

UncompressFileExample.java

package javabeat.net.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.InflaterInputStream;

public class UncompressFileExample {
	public static void main(String args[]) {

		try {
			FileInputStream fin = new FileInputStream("Compressed");
			InflaterInputStream in = new InflaterInputStream(fin);

			FileOutputStream fout = new FileOutputStream("UnCompressed.txt");

			int i;
			while ((i = in.read()) != -1) {
				fout.write((byte) i);
				fout.flush();
			}
			System.out.println("File is Uncompressed!!");
			fin.close();
			fout.close();
			in.close();

		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Category: JavaTag: Java Basics, Java Util

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Auto Refresh a Web Page In JSP
Next Post: StringTokenizer Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact