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.

}<br />

}<br />

}<br />

FileOutputStream fout = new FileOutputStream(<br />

args[i].substring(0, args[i].length()-3));<br />

StreamCopier.copy(gzin, fout);<br />

fout.close();<br />

}<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

}<br />

else {<br />

System.err.println(args[i] + " does not appear to be a gzipped<br />

file.");<br />

}<br />

9.2.5 Expanding Output Streams and Compressing Input Streams<br />

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

You may have noticed that the compression stream classes are not fully symmetrical. You can<br />

expand the data being read from an input stream, and you can compress data being written to<br />

an output stream, but there are no classes that compress data being read from an input stream<br />

or expand data being written to an output stream. Such classes aren't commonly needed. It's<br />

possible that you might want to read compressed data from a file and write uncompressed data<br />

onto the network, but as long as there are an input stream and an output stream, you can<br />

always put the compressor on the output stream or the decompressor on the input stream. In<br />

either case, the compressor and decompressor fall between the two underlying streams, so<br />

how they're chained doesn't really matter. Alternatively, you may have some reason to work<br />

with compressed data in memory; for example, your application might find it more efficient to<br />

store large chunks of text in compressed form. In this case, a byte array output stream chained<br />

to a deflater output stream will do the trick.<br />

9.3 Working with Zip Files<br />

Gzip and deflate are compression formats. Zip is both a compression and an archive format.<br />

This means that a single zip file may contain more than one uncompressed file, along with<br />

information about the names, permissions, creation and modification dates, and other<br />

information about each file in the archive. This makes reading and writing zip archives<br />

somewhat more complex and somewhat less amenable to a stream metaphor than reading and<br />

writing deflated or gzipped files.<br />

The java.util.zip.ZipFile class represents a file in the zip format. Such a file might be<br />

created by zip, PKZip, ZipIt, WinZip, or any of the many other zip programs. The<br />

java.util.zip.ZipEntry class represents a single file stored in such an archive.<br />

public class ZipFile extends Object implements ZipConstants<br />

public class ZipEntry extends Object implements ZipConstants<br />

The java.util.zip.ZipConstants interface that both these classes<br />

implement is a rare nonpublic interface that contains constants useful<br />

for reading and writing zip files. Most of these constants define the<br />

positions in a zip file where particular information, like the compression<br />

method used, is found. You don't need to concern yourself with it.<br />

159

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

Saved successfully!

Ooh no, something went wrong!