Thursday, December 31, 2009

Writing a Zip file

This sample code shows the method to create a Zip file and add files to it. It uses ZipOutputStream to write zip file.


import java.io.*;
import java.util.zip.*;

public class ZipFileWrtExp {

public static void main(String[] args) {

try {
FileOutputStream fos = new FileOutputStream("C:\\MyZip.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("C:\\file1.txt");
zos.putNextEntry(ze);
zos.closeEntry();

ze= new ZipEntry("C:\\file2.txt");
zos.putNextEntry(ze);
zos.closeEntry();
zos.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Source: java-tips.org

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory