4/30/09

File Zip using JAVA

pass your file names in an arry to the method.

---------------------------------------------------


public static void zip(String [] args) throws FileNotFoundException, IOException
{

String outputFile = "new.zip";

int level = 9;
int start = 1;

FileOutputStream fout = new FileOutputStream(outputFile);
ZipOutputStream zout = new ZipOutputStream(fout);

zout.setLevel(level);

for (int i = start; i < args.length; i++)
{

ZipEntry ze = new ZipEntry(args[i]);
FileInputStream fin = new FileInputStream(args[i]);
try {

System.out.println("Compressing " + args[i]);
zout.putNextEntry(ze);

for (int c = fin.read(); c != -1; c = fin.read())
{
zout.write(c);
}

}
finally{
fin.close();
}
}
zout.close();
}

No comments:

Post a Comment