import java.io.*;
import java.util.zip.*;
public class ZipCreateExample{
/**
* @param args the command line arguments
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception {
// input file
FileInputStream in = new FileInputStream("F:/Documents and Settings/nishanth1125/Desktop/ZipCreateExample.java");;
// out put file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/Documents and Settings/nishanth1125/Desktop/tmp.zip"));
// name of file
out.putNextEntry(new ZipEntry("zippedjava.java"));
byte[] b = new byte[1024];
int count;
while ((count = in.read(b)) > 0) {
System.out.println();
// write file in zip
out.write(b, 0, count);
}
out.close();
in.close();
}
}
Simplified code for zip file in javaNishanth Thomas December 1, 2011 at 11:52 AM
import java.io.*; import java.util.zip.*; public class ZipCreateExample{ /** * @param args the command line arguments * @throws FileNotFoundException */ public static void main(String[] args) throws Exception { // input file FileInputStream in = new FileInputStream("F:/Documents and Settings/nishanth1125/Desktop/ZipCreateExample.java");; // out put file ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/Documents and Settings/nishanth1125/Desktop/tmp.zip")); // name of file out.putNextEntry(new ZipEntry("zippedjava.java")); byte[] b = new byte[1024]; int count; while ((count = in.read(b)) > 0) { System.out.println(); // write file in zip out.write(b, 0, count); } out.close(); in.close(); } }
Post your Comment