Java ZipFile

ZIP is a file format for compressed files or folders. It enables data compression. Using Java programming language, we can create ZIP files or folders. For this Java provides the classes In Java, ZipFile is a class that belongs to the java.util.zip package. The package provides the java.util.zip package. The package contains the number of classes that deal with the ZIP file or folder. The classes can be to read and write the standard ZIP and GZIP file formats. In this section, we will discuss the Java ZipFile class.

Java ZipFile Class

It provides fields and methods that are required to deal with the ZIP file. The class is useful if we want to read entries from a ZIP file. It implements the Closeable and AutoCloseable interfaces.

ZipFile Class Fields

The class provides the constant field's values that are public static and final.

Modifier and TypeField
static intCENATT
static intCENATX
static intCENCOM
static intCENCRC
static intCENDSK
static intCENEXT
static intCENFLG
static intCENHDR
static intCENHOW
static intCENLEN
static intCENNAM
static intCENOFF
static longCENSIG
static intCENSIZ
static intCENTIM
static intCENVEM
static intCENVER
static intENDCOM
static intENDHDR
static intENDOFF
static longENDSIG
static intENDSIZ
static intENDSUB
static intENDTOT
static intEXTCRC
static intEXTHDR
static intEXTLEN
static longEXTSIG
static intEXTSIZ
static intLOCCRC
static intLOCEXT
static intLOCFLG
static intLOCHDR
static intLOCHOW
static intLOCLEN
static intLOCNAM
static longLOCSIG
static intLOCSIZ
static intLOCTIM
static intLOCVER
static intOPEN_DELETE
Mode flag to open a zip file and mark it for deletion.
static intOPEN_READ
Mode flag to open a zip file for reading.

ZipFile Class Constructors

Passing null values to the constructors may lead to the NullPointerException.

ConstructorDescription
ZipFile(File file)It is used to open a specified file object file.
ZipFile(File file, Charset charset)It is used to open a ZIP file for reading.
ZipFile(File file, int mode)It opens a new ZipFile to read from the specified File object in the specified mode.
ZipFile(File file, int mode, Charset charset)It opens a new ZipFile to read from the specified File object in the specified mode.
ZipFile(String name)It also opens a ZIP file for reading.
ZipFile(String name, Charset charset)It is also used to open a ZIP file for reading.

ZipFile Class Methods

The class has the following methods.

MethodDescription
close()The method closes the ZIP file.
entries()It returns the enumeration of the ZIP file entries.
finalize()It ensures that the system resources held by this ZipFile object are released when there are no more references to it. It overrides the finalize method of the Object class.
getComment()It returns the ZIP file comment and returns null if the file has no comment.
getEntry()It returns the ZIP file entry for the specified name and returns null if the entry is null.
getInputStream()It is used to read the contents of the specified ZIP file entry and returns an input stream for the same.
getName()It is used to get the path of the ZIP file.
size()It is used to get the number of entries a ZIP file has.