TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Compressing File In Java
Alagunila Meganathan
Aug 10
2016
Code
603
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
CompressingFile.rar
import
java.io.*;
import
java.util.zip.*;
public
class
CompressingFile {
public
static
void
doCompressFile(String inFileName){
try
{
File file =
new
File(inFileName);
System.out.println(
" you are going to gzip the : "
+ file +
"file"
);
FileOutputStream fos =
new
FileOutputStream(file +
".gz"
);
System.out.println(
" Now the name of this gzip file is : "
+ file +
".gz"
);
GZIPOutputStream gzos =
new
GZIPOutputStream(fos);
System.out.println(
" opening the input stream"
);
FileInputStream fin =
new
FileInputStream(file);
BufferedInputStream in =
new
BufferedInputStream(fin);
System.out.println(
"Transferring file from"
+ inFileName +
" to "
+ file +
".gz"
);
byte
[] buffer =
new
byte
[
1024
];
int
i;
while
((i = in.read(buffer)) >=
0
){
gzos.write(buffer,
0
,i);
}
System.out.println(
" file is in now gzip format"
);
in.close();
gzos.close();
}
catch
(IOException e){
System.out.println(
"Exception is"
+ e);
}
}
public
static
void
main(String args[]){
if
(args.length!=
1
){
System.err.println(
"Please enter the file name which needs to be compressed "
);
}
else
{
doCompressFile(args[
0
]);
}
}
}
java
compressing file