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
getInputStream().

The structure of the class is given by

public class java.util.zip.ZipFile 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.

Parameter
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.

Parameter
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
ZipFile.

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.

Parameter
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.

Parameter
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)
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment