23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

Normally, the name argument is the name of the file that's being placed in the archive. It<br />

should not be null, or a NullPointerException will be thrown. It is also required to be less<br />

than 65,536 bytes long (which is plenty long for a filename). <strong>Java</strong> 2 adds one more public<br />

constructor that copies the name, comment, modification time, CRC checksum, size,<br />

compressed size, method, comment, and indeed everything except the actual data of the file<br />

from an existing ZipEntry object. (It's unclear why you might need this.)<br />

public ZipEntry(ZipEntry e) // <strong>Java</strong> 2<br />

There are nine methods that return information about a specific entry in a zip file:<br />

public String getName()<br />

public long getTime()<br />

public long getSize()<br />

public long getCompressedSize()<br />

public long getCrc()<br />

public int getMethod()<br />

public byte[] getExtra()<br />

public String getComment()<br />

public boolean isDirectory()<br />

The name is simply the relative path and filename stored in the archive, like<br />

sun/net/www/protocol/systemresource/ParseSystemURL.class or java/awt/Dialog.class. The<br />

time is the last time this entry was modified. It is given as a long, counting the number of<br />

milliseconds since midnight, January 1, 1970, Greenwich Mean Time. (This is not how the<br />

time is stored in the zip file, but <strong>Java</strong> converts the time before returning it.) -1 indicates that<br />

the modification time is not specified. The CRC is a 32-bit cyclic redundancy code for the<br />

data that's used to determine whether or not the file is corrupt. If no CRC is included,<br />

getCRC() returns -1.<br />

The size is the original, uncompressed length of the data in bytes. The compressed size is the<br />

length of the compressed data in bytes. The getSize() and getCompressedSize() methods<br />

both return -1 to indicate that the size isn't known.<br />

getMethod() tells you whether or not the data is compressed; it returns if the data is<br />

uncompressed, 8 if it's compressed using the deflation format, and -1 if the compression<br />

format is unknown. and 8 are given as the mnemonic constants ZipEntry.STORED and<br />

ZipEntry.DEFLATED :<br />

public static final int STORED = 0;<br />

public static final int DEFLATED = 8;<br />

Each entry may contain an arbitrary amount of extra data. If so, this data is returned in a byte<br />

array by the getExtra() method. Similarly, each entry may contain an optional string<br />

comment. If it does, the getComment() method returns it; if it doesn't, getComment() returns<br />

null. Finally, the isDirectory() method returns true if the entry is a directory and false if<br />

it isn't.<br />

Example 9.9 is an improved ZipLister that prints information about the files in a zip archive.<br />

163

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!