Java.util.zip.ZipOutputStream class in Java Last Updated : 12 Sep, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report This class implements an output stream filter for writing files in the ZIP file format. Includes support for both compressed and uncompressed entries. Constructor : ZipOutputStream(OutputStream out) : Creates a new ZIP output stream. ZipOutputStream(OutputStream out, Charset charset) : Creates a new ZIP output stream. Methods: void close() : Closes the ZIP output stream as well as the stream being filtered. Syntax :public void close() throws IOException Overrides: close in class DeflaterOutputStream Throws: ZipException IOException void closeEntry() : Closes the current ZIP entry and positions the stream for writing the next entry. Syntax :public void closeEntry() throws IOException Throws: ZipException IOException void finish() : Finishes writing the contents of the ZIP output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream. Syntax :public void finish() throws IOException Overrides: finish in class DeflaterOutputStream Throws: ZipException IOException void putNextEntry(ZipEntry e) : Begins writing a new ZIP file entry and positions the stream to the start of the entry data. Closes the current entry if still active. The default compression method will be used if no compression method was specified for the entry, and the current time will be used if the entry has no set modification time. Syntax :public void putNextEntry(ZipEntry e) throws IOException Parameters: e - the ZIP entry to be written Throws: ZipException IOException void setComment(String comment) : Sets the ZIP file comment. Syntax :public void setComment(String comment) Parameters: comment - the comment string Throws: IllegalArgumentException void setLevel(int level) : Sets the compression level for subsequent entries which are DEFLATED.The default setting is DEFAULT_COMPRESSION. Syntax :public void setLevel(int level) Parameters: level - the compression level (0-9) Throws: IllegalArgumentException void setMethod(int method) : Sets the default compression method for subsequent entries. This default will be used whenever the compression method is not specified for an individual ZIP file entry, and is initially set to DEFLATED. Syntax :public void setMethod(int method) Parameters: method - the default compression method Throws: IllegalArgumentException void write(byte[] b, int off, int len) : Writes an array of bytes to the current ZIP entry data. This method will block until all the bytes are written. Syntax :public void write(byte[] b, int off, int len) Parameters: b - the data to be written off - the start offset in the data len - the number of bytes that are written Throws: ZipException IOException Program : Java //Java program demonstrating ZipOutputStream methods import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; class ZipOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("zipfile"); ZipOutputStream zos = new ZipOutputStream(fos); //illustrating setMethod() zos.setMethod(8); //illustrating setLevel method zos.setLevel(5); ZipEntry ze1 = new ZipEntry("ZipEntry1"); //illustrating putNextEntry method zos.putNextEntry(ze1); //illustrating setComment zos.setComment("This is my first comment"); //illustrating write() for(int i = 0; i < 10; i++) zos.write(i); //illustrating write(byte b[], int off, int len) byte b[] = { 11, 12, 13}; zos.write(b); //illustrating closeEntry() zos.closeEntry(); //Finishes writing the contents of the ZIP output stream // without closing the underlying stream zos.finish(); //closing the stream zos.close(); FileInputStream fin = new FileInputStream("zipfile"); ZipInputStream zin = new ZipInputStream(fin); //Reads the next ZIP file entry ZipEntry ze = zin.getNextEntry(); //the name of the entry. System.out.println(ze.getName()); //illustrating getMethod System.out.println(ze.getMethod()); //Reads up to byte.length bytes of data from this input stream // into an array of bytes. byte c[] = new byte[13]; if(zin.available() == 1) zin.read(c); System.out.print(Arrays.toString(c)); //closes the current ZIP entry zin.closeEntry(); //closing the stream zin.close(); } } Output : ZipEntry1 8 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13] Comment More infoAdvertise with us Next Article Java.util.zip.ZipEntry class in Java K kartik Improve Article Tags : Java Java-I/O Practice Tags : Java Similar Reads Java.util.zip.ZipInputStream class in Java This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries. Constructors: ZipInputStream(InputStream in) : Creates a new ZIP input stream. ZipInputStream(InputStream in, Charset charset) : Creates a new ZIP inp 3 min read Java.util.zip.ZipEntry class in Java This class is used to represent a ZIP file entry. Constructors ZipEntry(String name) : Creates a new zip entry with the specified name. ZipEntry(ZipEntry e) : Creates a new zip entry with fields taken from the specified zip entry. Methods: Object clone() : Returns a copy of this entry. Syntax :publi 4 min read Java.io.OutputStream class in Java This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo 2 min read Java.io.ObjectOutputStream Class in Java | Set 1 An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. Only objects that support the java.io.Serializable in 9 min read Java.io.ObjectInputStream Class in Java | Set 2 Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStrea 6 min read Java.io.OutputStreamWriter Class In Java, OutputStreamWriter class connects character streams to byte streams. It encodes Characters into bytes using a specified charset. Declaration of Java OutputStreamWriter Class public class OutputStreamWriter extends WriterConstructors of OutputStreamWriter Class in JavaConstructors in OutputS 5 min read Like