Monday, July 19, 2010

Zip a File Using JAVA

import java.io.*;
import java.util.zip.*;
public class zipFile
{
public static void main(String a[])
{
try
{
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(new FileOutputStream("zippedFile.zip")));
byte[] data = new byte[1000];
BufferedInputStream in = new BufferedInputStream
(new FileInputStream("ToBeZipped.txt"));
int count;
out.putNextEntry(new ZipEntry("zippedFile.zip"));
while((count = in.read(data,0,1000)) != -1)
{
out.write(data, 0, count);
}
in.close();
out.flush();
out.close();
System.out.println("Input file as ToBeZipped.txt");
System.out.println("File Zipped as zippedFile.zip");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

No comments:

Post a Comment