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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

% java FancyZipLister temp.zip<br />

test.txt was deflated at Wed Jun 11 15:57:32 EDT 1997<br />

from 187 bytes to 98 bytes, a savings of 52.406417112299465%<br />

Its CRC is 1981281836<br />

ticktock.txt was deflated at Wed Jun 11 10:42:02 EDT 1997<br />

from 1480 bytes to 405 bytes, a savings of 27.364864864864863%<br />

Its CRC is 4103395328<br />

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

There are also six corresponding set methods, which are used to attach information to each<br />

entry you store in a zip archive. However, most of the time it's enough to let the ZipEntry<br />

class calculate these for you:<br />

public void setTime(long time)<br />

public void setSize(long size)<br />

public void setCrc(long crc)<br />

public void setMethod(int method)<br />

public void setExtra(byte[] extra)<br />

public void setComment(String comment)<br />

Finally, there's the toString() method, which was implicitly used in the ZipLister program<br />

to print the name of each entry:<br />

public String toString()<br />

<strong>Java</strong> 2 adds hashCode() and clone() methods:<br />

public int hashCode() // <strong>Java</strong> 2<br />

public Object clone() // <strong>Java</strong> 2<br />

In <strong>Java</strong> 1.1, ZipEntry simply inherits these methods from java.lang.Object. However,<br />

ZipEntry only implements the Cloneable interface in <strong>Java</strong> 2, not in <strong>Java</strong> 1.1.<br />

9.3.2 The ZipOutputStream Class<br />

The java.util.zip.ZipOutputStream class subclasses DeflaterOutputStream and writes<br />

compressed data in the zip format. ZipOutputStream implements the nonpublic<br />

java.util.zip.ZipConstants interface.<br />

public class ZipOutputStream extends DeflaterOutputStream<br />

implements ZipConstants<br />

<strong>Java</strong> supports two zip formats, uncompressed and compressed. These are slightly less<br />

obviously known as stored and deflated. They correspond to the mnemonic constants<br />

ZipOutputStream.STORED and ZipOutputStream.DEFLATED:<br />

public static final int STORED = ZipEntry.STORED;<br />

public static final int DEFLATED = ZipEntry.DEFLATED;<br />

Deflated files are compressed by a Deflater object using the deflation method. Stored files<br />

are copied byte for byte into the archive without any compression. This is the right format for<br />

files that are already compressed but still need to go into the archive, like a GIF image or an<br />

MPEG movie.<br />

165

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

Saved successfully!

Ooh no, something went wrong!