📄 tij0115.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0114.html">Prev</a> | <a href="tij0116.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Compression<P><A NAME="Index1273"></A><A NAME="Index1274"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.1<A NAME="Index1275"></A>
has also added some classes to support reading and writing streams in a
compressed format. These are wrapped around existing IO classes to provide
compression functionality.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
aspect of these Java 1.1 classes stands out: They are not derived from the new
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes, but instead are part of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
hierarchies. So you might be forced to mix the two types of streams. (Remember
that you can use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStreamReader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStreamWriter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to provide easy conversion between one type and another.)
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
<COL width="126">
<COL width="211">
</COLGROUP>
<TR VALIGN="TOP">
<TH WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.1<A NAME="Index1276"></A>
Compression class
</FONT><P></DIV>
</TH>
<TH WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Function</FONT><P></DIV>
</TH>
</TR>
<A NAME="Index1277"></A><A NAME="Index1278"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CheckedInputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GetCheckSum( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
produces checksum for any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(not just decompression)
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1279"></A><A NAME="Index1280"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CheckedOutputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GetCheckSum( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
produces checksum for any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(not just compression)
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1281"></A><A NAME="Index1282"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DeflaterOutputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Base
class for compression classes
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1283"></A><A NAME="Index1284"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ZipOutputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DeflaterOutputStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">that
compresses data into the Zip file format
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1285"></A><A NAME="Index1286"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GZIPOutputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DeflaterOutputStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">that
compresses data into the GZIP file format
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1287"></A><A NAME="Index1288"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InflaterInputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Base
class for decompression classes
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1289"></A><A NAME="Index1290"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ZipInputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DeflaterInputStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">that
Decompresses data that has been stored in the Zip file format
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1291"></A><A NAME="Index1292"></A><TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GZIPInputStream</B></FONT><P></DIV>
</TD>
<TD WIDTH=211 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DeflaterInputStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">that
decompresses data that has been stored in the GZIP file format
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
there are many compression algorithms, Zip and GZIP are possibly the most
commonly used. Thus you can easily manipulate your compressed data with the
many tools available for reading and writing these formats.
</FONT><a name="_Toc408018635"></a><P></DIV>
<A NAME="Heading340"></A><H3 ALIGN=LEFT>
Simple
compression with GZIP
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
GZIP interface is simple and thus is probably more appropriate when you have a
single stream of data that you want to compress (rather than a collection of
dissimilar pieces of data). Here’s an example that compresses a single
file:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: GZIPcompress.java</font>
<font color="#009900">// Uses Java 1.1 GZIP compression to compress</font>
<font color="#009900">// a file whose name is passed on the command</font>
<font color="#009900">// line.</font>
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">import</font> java.util.zip.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> GZIPcompress {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">try</font> {
BufferedReader in =
<font color="#0000ff">new</font> BufferedReader(
<font color="#0000ff">new</font> FileReader(args[0]));
BufferedOutputStream out =
<font color="#0000ff">new</font> BufferedOutputStream(
<font color="#0000ff">new</font> GZIPOutputStream(
<font color="#0000ff">new</font> FileOutputStream("test.gz")));
System.out.println("Writing file");
<font color="#0000ff">int</font> c;
<font color="#0000ff">while</font>((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
System.out.println("Reading file");
BufferedReader in2 =
<font color="#0000ff">new</font> BufferedReader(
<font color="#0000ff">new</font> InputStreamReader(
<font color="#0000ff">new</font> GZIPInputStream(
<font color="#0000ff">new</font> FileInputStream("test.gz"))));
String s;
<font color="#0000ff">while</font>((s = in2.readLine()) != <font color="#0000ff">null</font>)
System.out.println(s);
} <font color="#0000ff">catch</font>(Exception e) {
e.printStackTrace();
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -