Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Compressing File In Java
WhatsApp
Alagunila Meganathan
Aug 10
2016
656
0
0
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
Up Next
Compressing File In Java