Sunday, January 13, 2013
Today I am going to post a program that will be able to compress any file in GZIP format. Java language can be used very efficiently for this purpose. There is a special package java.util.zip which contains several classes that can be used for compressing files and folders. Today I am going to deal with GZIPOutputStream . This particular class is used to produce with a file in compressed GZIP format with extension .gz . Here at first the file to be compressed is taken as input from user. Then FileInputStream is used to open a stream connecting to that file. Next the data from file is read in a buffer of capacity 4K. Then the buffer is written to the output stream. Here output stream is nothing but a FileOutputStream wrapped over with a GZIPOutputStream . This process of reading and writing goes on until end of file is reached.
--------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class CompressFile {
public void gzipFile(String from, String to) throws IOException {
FileInputStream in = new FileInputStream(from); //stream to input file
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to)); //stream to output file
byte[] buffer = new byte[4096]; //defining buffer capacity
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) //reading into buffer
out.write(buffer, 0, bytesRead); //writing buffer contents to output stream
in.close(); //closing both streams
out.close();
}
public static void main(String args[]) throws IOException {
if(args.length>0){
String from=args[0]; //getting source file as command line input
new CompressFile().gzipFile(from,from+".gz");
}
}
}
NOTE : Very soon I will be posting a GUI version of this ptogram.
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class CompressFile {
public void gzipFile(String from, String to) throws IOException {
FileInputStream in = new FileInputStream(from); //stream to input file
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to)); //stream to output file
byte[] buffer = new byte[4096]; //defining buffer capacity
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) //reading into buffer
out.write(buffer, 0, bytesRead); //writing buffer contents to output stream
in.close(); //closing both streams
out.close();
}
public static void main(String args[]) throws IOException {
if(args.length>0){
String from=args[0]; //getting source file as command line input
new CompressFile().gzipFile(from,from+".gz");
}
}
}
NOTE : Very soon I will be posting a GUI version of this ptogram.
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
I really appreciate your efforts in java programming and the codes in your blog. You are great.
ReplyDeletePlease I need your help on DCT Steganography code. (i.e hinding and extracting files in a jpeg image.).
Thank you.
Raphael
amb_raph@yahoo.com.