Class ZipFile in Java

Class ZipFile in Java

ZipFile class reads the contents of ZIP files. It uses a random access file internally so that the entries of the ZIP file do not have to be read sequentially as they do with the ZipInputStream class.

A ZipFile object can be created by specifying the ZIP file to be read either as a String filename or as a File object. Once created, the getEntry() method returns a ZipEntry object for a named entry, and the entries() method returns an Enumeration object that allows you to loop through all the ZipEntry objects for the file. To read the contents of a specific ZipEntry within the ZIP file, pass the ZipEntry to getInputStream()–this returns an InputStream object from which we can read the entry’s contents.

The ZipFile class represents a ZIP file. Unlike with a ZipInputStream, we can read the entries in a ZipFile nonsequentially. Internally, the class uses a RandomAccessFile so that we can read the entries from the file in any order. we can obtain a list of the entries in this ZIP file by calling entries(). Given an entry, we can get an InputStream for that entry using

The structure of the class is given by

public class extends java.lang.Object {
// Public Constructors
public ZipFile(String name) throws IOException;
public ZipFile(File file) throws ZipException, IOException;
// Public Instance Methods
public void close() throws IOException;
public Enumeration entries();
public ZipEntry getEntry(String name);
public InputStream getInputStream(ZipEntry ze) throws IOException;
public String getName();

The details of the class structure are given as follows:

public ZipFile(File file);

public ZipFile(File file) constructor creates a ZipFile for reading from the given File object.

See also  ENUM Concept Simplified In Java

file – The File to read.

public ZipFile(String name);

public ZipFile(String name) constructor creates a ZipFile for reading from the file specified by the given path.

name – A string that contains the path name of the file.

public void close();

public void close() method closes the ZipFile and releases its system resources.

public Enumeration entries();

public Enumeration entries() method returns an enumeration of ZipEntry objects that represents the contents of this

This method returns an Enumeration of ZipEntry objects.

public ZipEntry getEntry(String name);

public ZipEntry getEntry(String name) method returns the ZipEntry object that corresponds to the given entry name.

This method returns the entry corresponding to the given name or null if there is no such entry.

name – The entry name.

public InputStream getInputStream(ZipEntry ze);

public InputStream getInputStream(ZipEntry ze) method returns an input stream that can read the entry described by the supplied ZipEntry.

This method returns an InputStream for the given entry.

ze – A ZipEntry in this file.

public String getName();

public String getName() method returns the path name of this ZipFile.

This method returns the path of this file.

Apart from these ZipFile class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *