Open In App

Java.util.zip.ZipOutputStream class in Java

Improve
Improve
Like Article
Like
Save
Share
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 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]


Last Updated : 12 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads