Java ZipEntry and ZipFile: Describing, Reading, and Manipulating Zip Files in Java

Java ZipEntry and ZipFile Classes

The ZipEntry class describes an entry in a zip file. It contains various methods that get and set information about the entry. It is used by ZipFile and ZipInputStream which read zip files and by ZipOutputStream which writes zip files.

The last modification time is set by this method and encoded in standard MS-DOS date and time format. It is also converted to epoch time by the default TimeZone.

ReadZipEntry

Reading a zip file can be slow, especially if you have numerous entries. Using an input stream to read each entry in sequence can help speed up the process. The ZipInputStream class is one such stream.

It also recognizes Unix symbolic link entries, which are created by InfoZIP’s zip utility. It also allows you to pass in an extra field parsing behavior, allowing for finer-grained control of how the archive is read.

It can also define a function to append new member files sequentially into an existing ZIP file. This function opens and closes the target ZIP file each time it is called. It also implements the finish() and close() methods of the ZipInputStream. Both of these are useful for reducing memory use.

GetZipEntry

The GetZipEntry method gets a ZipEntry object for the current entry in the zip file. This object provides a set of methods to get and set various information about the entry, including the location within the archive and an offset into the archive where to find the actual data for that entry.

The ExcludeDir method allows the name of one or more directories to be excluded from the list of entries included when AppendFiles or AppendFileEx is called. This ensures that any new directories added will be at the end of the directory tree, or that they will not include any files that are already in the Zip file.

The HeartbeatMs parameter controls the frequency of AbortCheck event callbacks. This callback fires whenever the zip is about to write its next entry, and can be used to cancel a write operation.

SetZipEntry

Instances of this class store information about a single entry in a ZIP file. Various methods allow you to access this information. This class is used by ZipFile and ZipInputStream to read ZIP files, and by ZipOutputStream to write them.

A path to a file or directory on the filesystem that you want to add to the archive. This method will create a new entry in the ZIP file with the given name. The entry can either be stored or deflated. Deflated entries are compressed, stored entries are not.

If set, this entry’s last access time and/or creation time will be recorded in the extended timestamp fields (NTFS tag 0x0001 or Info-ZIP Application Note 970311). This allows for archived files to be reextracted at a later date.

GetZipFileEntry

The c:zipfile component wraps the JSZip library and leverages JavaScript Remoting to request each Zip File entry in turn from a Page. Once it receives the entire set of incoming Zip files (with inline Base64 data) it executes an action method on the controller and redirects the user to the resulting Document page.

Unlike the classic ZIP format, which only uses 4-byte “signatures” to denote various structures in a ZIP file, the new ZIP64 specification allows some entries that do not fit into the classic local file header or central directory records to be moved into extra fields. This allows tools that correctly read ZIP archives to skip reading any file data between the end of the central directory and the start of the first file chunk.

SetZipFileEntry

Using the ZipFile class you can read (“r”), write (“w”) and append (“a”) new member files to existing archives. It also allows you to set relevant metadata.

The ZipFile class has several other useful features and functionality. For example it supports a “fullpath + volume name” (MacOS format) in addition to the full path as stored in the central directory and it tries to avoid storing prepended data such as the drive letter and leading path separators.

Another feature is that it displays progress dots when scanning for input files, displaying one every 2 seconds or a fixed number of entries, whichever is longer. This can be useful if the scan is taking too long and it may help diagnose problems such as a slow network connection.

Hustle back to the home screen

Leave a Reply

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