How do I create a zip file? - JAVA Interview Questions

How do I create a zip file?


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZippingFileExample {
public static void main(String[] args) {
try {
String source = "text.txt";
String target = "example.zip";

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target));
FileInputStream fis = new FileInputStream(source);

// put a new ZipEntry in the ZipOutputStream
zos.putNextEntry(new ZipEntry(source));

int size = 0;
byte[] buffer = new byte[1024];

// read data to the end of the source file and write it to the zip
// output stream.
while ((size = fis.read(buffer)) > 0) {
zos.write(buffer);
}

zos.closeEntry();
fis.close();

// Finish zip process
zos.close();


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



Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

1 comments:

trustno1 said...

java programming with basic example codes
Trace message axis handler

Related JAVA Questions Posts