Class ZipEntry in Java

Class ZipEntry in Java

ZipEntry class describes a single entry (typically a compressed file) stored within a ZIP file. The various methods get and set various pieces of information about the entry. The ZipEntry class is used by the ZipFile and ZipInputStream, which read ZIP files, and by the ZipOutputStream, which writes ZIP files.

The ZipEntry class represents a single entry in a ZIP file, which is either a compressed file or an uncompressed file. ZipEntry provides methods that set and retrieve various pieces of information about an entry.

Ads code goes here

When we are reading a ZIP file, you use ZipInputStream.getNextEntry() to return the next
entry in the file. Then we can retrieve information about that particular entry. we can also read the entries in a ZIP file in a nonsequential order using the ZipFile class.

When reading a ZIP file, the ZipEntry objects returned by the ZipFile or ZipInputStream contain the name, size, modification time, and other information about each entry in the file.

When writing a ZIP file, on the other hand, we should create our own ZipEntry objects, and initialize them to contain the entry name and other appropriate information before writing the contents of the entry.

When we are writing a ZIP file, you use ZipOutputStream.putNextEntry() to write an entry,
and we must create your own ZipEntry objects. If you are storing compressed (deflated) files, we need only specify a name for the ZipEntry; the other fields are filled in automatically. If, however, we are placing uncompressed entries, we need to specify the size of each entry and provide a CRC-32 checksum value.

READ  What is Cucumber and How to Work With Cucumber?

The class definition for Class ZipEntry is given as

public class java.util.zip.ZipEntry extends java.lang.Object {
// Public Constructor
public ZipEntry(String name);
// Constants
public static final int DEFLATED;
public static final int STORED;
// Public Instance Methods
public String getComment();
public long getCompressedSize();
public long getCrc();
public byte[] getExtra();
public int getMethod();
public String getName();
public long getSize();
public long getTime();
public boolean isDirectory();
public void setComment(String comment);
public void setCrc(long crc);
public void setExtra(byte[] extra);
public void setMethod(int method);
public void setSize(long size);
public void setTime(long time);
public String toString(); // Overrides Object
}

The details of the class structure are given as follows:

public static final int DEFLATED;

public static final int DEFLATED represents a constant that represents an entry that is stored using the deflate algorithm.

public static final int STORED;

public static final int STORED represents a constant that represents an entry that is stored verbatim; in other words, with no compression applied.

public ZipEntry(String name);

public ZipEntry(String name) constructor creates a ZipEntry with the given name.

Parameter
name – The name of the entry.

public String getComment();

public String getComment() method returns the comment string for this ZipEntry.

This method returns the comment of this entry or null if one has not been specified.

public long getCompressedSize();

public long getCompressedSize() method returns the compressed size of this ZipEntry.

This method returns the compressed size of this entry or -1 is the compressed size is not known.

public long getCrc();

public long getCrc() method returns the CRC-32 checksum value for this ZipEntry.

This method returns the checksum value for this entry.

public byte[] getExtra();

public byte[] getExtra() method returns the bytes in the extra field data for this ZipEntry.

READ  Class ParsePosition in Java

This method returns the extra field data for this entry or null if there is none.

public int getMethod();

public int getMethod() method returns the compression method of this ZipEntry. Valid values are DEFLATED and
STORED.

This method returns the compression method of this entry or -1 if it has not been specified.

public String getName();

public String getName() method returns the string name of this ZipEntry.

This method returns the name of this entry.

public long getSize();

public long getSize() method returns the uncompressed size of this ZipEntry.

This method returns the uncompressed size of this entry or -1 if the uncompressed size is not known.

public long getTime();

public long getTime() method returns the modification date of this ZipEntry as the number of milliseconds since
midnight, January 1, 1970 GMT.

This method returns the modification date of this entry.

public boolean isDirectory();

public boolean isDirectory() method returns true if this ZipEntry represents a directory.

This method returns a boolean value that indicates whether or not this entry is a directory.

public void setComment(String comment);

public void setComment(String comment) method sets the comment string of this ZipEntry.

Parameter
comment – The new comment string.

public void setCrc(long crc);

public void setCrc(long crc) method sets the CRC-32 checksum value for this ZipEntry.

Parameter
crc – The new checksum value.

public void setExtra(byte[] extra);

public void setExtra(byte[] extra) method sets the extra field data for this ZipEntry.

Parameter
extra – The extra field data.

public void setMethod(int method);

public void setMethod(int method) method sets the compression method of this ZipEntry. This corresponds to the
compression level of Deflater.

READ  Class URL in Java

Parameter
method – The new compression method.

public void setSize(long size);

public void setSize(long size) method sets the uncompressed size of this ZipEntry.

Parameter
size – The new uncompressed entry size.

public void setTime(long time);

public void setTime(long time) method sets the modification date of this ZipEntry.

Parameter
time – The new modification date, expressed as the number of seconds since midnight, January 1,
1970 GMT.

public String toString();

public String toString() method returns the name of this ZipEntry.

This method returns a string representation of this object.
Apart from these ZipEntry class also has inherited methods from class- Object. They are as follows:

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

Share and Enjoy !

Leave a Comment

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