package org.kodejava.example.util.zip;
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 = "dummy-1.txt";
String target = "data-1.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, buffer.length)) > 0) {
zos.write(buffer, 0, size);
}
zos.closeEntry();
fis.close();
// Finish zip process
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!