Example of ZipOutputStream in Java
February 06, 2013
In java, ZipOutputStream class is used to zip the file. Source file can be any file and that will be written in zip file.
ZipOutputStream has a method
public void putNextEntry(ZipEntry e) throws IOException
That takes input ZipEntry class. ZipEntry represents a zip file entry. putNextEntry begins writing new zip file and sets the start point.
package com.concretepage; 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 ZipOutputStreamTest { public static void main(String... args) throws IOException { String source="D:/page/file.txt"; File sfile= new File(source); String dest="D:/page/file.zip"; File dfile= new File(dest); FileInputStream fis= new FileInputStream(sfile); FileOutputStream fos= new FileOutputStream(dfile); ZipOutputStream zos= new ZipOutputStream(fos); ZipEntry ze= new ZipEntry(source); //begins writing a new zip file and sets the the position to the start of data zos.putNextEntry(ze); byte[] buf = new byte[1024]; int len; while((len=fis.read(buf))>0){ zos.write(buf, 0, len); } System.out.println("File created:"+dest); fis.close(); zos.close(); } }