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 />

All high-order Unicode bytes are discarded before the comment is written onto the zip output<br />

stream. Attempts to attach a comment longer than 65,535 characters throw<br />

IllegalArgumentExceptions. Each zip output stream can have only one comment (though<br />

individual entries may have their own comments too). Resetting the comment erases the<br />

previous comment.<br />

9.3.2.3 Set the default compression level and method<br />

Next, you may wish to set the default compression method with setMethod():<br />

public void setMethod(int method)<br />

You can change the default compression method from stored to deflated or deflated to stored.<br />

This default method is used only when the zip entry itself does not specify a compression<br />

method. The initial value is ZipOutputStream.DEFLATED (compressed); the alternative is<br />

ZipOutputStream.STORED (uncompressed). An IllegalArgumentException is thrown if an<br />

unrecognized compression method is specified. You can call this method again at any time<br />

before the zip output stream is finished. This sets the default compression method for all<br />

subsequent entries in the zip output stream. For example:<br />

zout.setMethod(ZipOutputStream.STORED);<br />

You can change the default compression level with setLevel() at any time before the zip<br />

output stream is finished:<br />

public void setLevel(int level)<br />

For example:<br />

zout.setLevel(9);<br />

As with the default method, the zip output stream's default level is used only when the zip<br />

entry itself does not specify a compression level. The initial value is<br />

Deflater.DEFAULT_COMPRESS<strong>IO</strong>N. Valid levels range from (no compression) to 9 (high<br />

compression); an IllegalArgumentException is thrown if a compression level outside that<br />

range is specified. You can call setLevel() again at any time before the zip output stream is<br />

finished, which sets the default compression level for all subsequent entries in the zip output<br />

stream.<br />

9.3.2.4 Construct a ZipEntry object and put it in the archive<br />

Data is written into the zip output stream in separate groups called zip entries. These are<br />

represented by ZipEntry objects. A zip entry must be opened before data is written, and each<br />

zip entry must be closed before the next one is opened. The putNextEntry() method opens a<br />

new zip entry on the zip output stream:<br />

public void putNextEntry(ZipEntry ze) throws <strong>IO</strong>Exception<br />

If a previous zip entry is still open, it's closed automatically. The properties of the ZipEntry<br />

argument ze specify the compression level and method. If ze leaves those unspecified, then<br />

the defaults set by the last calls to setLevel() and setMethod() are used. The ZipEntry<br />

167

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

Saved successfully!

Ooh no, something went wrong!