java用Zip进行多文件保存2007-05-29 yycnet.yeah.net yyc译提供了Zip支持的Java 1.1库显得更加全面。利用它可以方便地保存多个文件。甚至有一个独立的类来简化对Zip文件的读操作。这个库采采用的是标准Zip格式,所以能与当前因特网上使用的大量压缩、解压工具很好地协作。下面这个例子采取了与前例相同的形式,但能根据我们需要控制任意数量的命令行参数。除此之外,它展示了如何用Checksum类来计算和校验文件的“校验和”(Checksum)。可选用两种类型的Checksum:Adler32(速度要快一些)和CRC32(慢一些,但更准确)。
//: ZipCompress.java// Uses Java 1.1 Zip compression to compress// any number of files whose names are passed// on the command line.import java.io.*;import java.util.*;import java.util.zip.*;public class ZipCompress {public static void main(String[] args) {try {FileOutputStream f =new FileOutputStream("test.zip");CheckedOutputStream csum =new CheckedOutputStream(f, new Adler32());ZipOutputStream out =new ZipOutputStream(new BufferedOutputStream(csum));out.setComment("A test of Java Zipping");// Can"t read the above comment, thoughfor(int i = 0; i < args.length; i++) {System.out.println("Writing file " + args[i]);BufferedReader in =new BufferedReader(new FileReader(args[i]));out.putNextEntry(new ZipEntry(args[i]));int c;while((c = in.read()) != -1)out.write(c);in.close();}out.close();// Checksum valid only after the file// has been closed!System.out.println("Checksum: " +csum.getChecksum().getValue());// Now extract the files:System.out.println("Reading file");FileInputStream fi = new FileInputStream("test.zip");CheckedInputStream csumi =new CheckedInputStream(fi, new Adler32());ZipInputStream in2 =new ZipInputStream(new BufferedInputStream(csumi));ZipEntry ze;System.out.println("Checksum: " +csumi.getChecksum().getValue());while((ze = in2.getNextEntry()) != null) {System.out.println("Reading file " + ze);int x;while((x = in2.read()) != -1)System.out.write(x);}in2.close();// Alternative way to open and read// zip files:ZipFile zf = new ZipFile("test.zip");Enumeration e = zf.entries();while(e.hasMoreElements()) {ZipEntry ze2 = (ZipEntry)e.nextElement();System.out.println("File: " + ze2);// ... and extract the data as before}} catch(Exception e) {e.printStackTrace();}}} ///:~
对于要加入压缩档的每一个文件,都必须调用putNextEntry(),并将其传递给一个ZipEntry对象。ZipEntry对象包含了一个功能全面的接口,利用它可以获取和设置Zip文件内那个特定的Entry(入口)上能够接受的所有数据:名字、压缩后和压缩前的长度、日期、CRC校验和、额外字段的数据、注释、压缩方法以及它是否一个目录入口等等。然而,虽然Zip格式提供了设置密码的方法,但Java的Zip库没有提供这方面的支持。而且尽管CheckedInputStream和CheckedOutputStream同时提供了对Adler32和CRC32校验和的支持,但是ZipEntry只支持CRC的接口。这虽然属于基层Zip格式的限制,但却限制了我们使用速度更快的Adler32。
为解压文件,ZipInputStream提供了一个getNextEntry()方法,能在有的前提下返回下一个ZipEntry。作为一个更简洁的方法,可以用ZipFile对象读取文件。该对象有一个entries()方法,可以为ZipEntry返回一个Enumeration(枚举)。
为读取校验和,必须多少拥有对关联的Checksum对象的访问权限。在这里保留了指向CheckedOutputStream和CheckedInputStream对象的一个句柄。但是,也可以只占有指向Checksum对象的一个句柄。
Zip流中一个令人困惑的方法是setComment()。正如前面展示的那样,我们可在写一个文件时设置注释内容,但却没有办法取出ZipInputStream内的注释。看起来,似乎只能通过ZipEntry逐个入口地提供对注释的完全支持。
当然,使用GZIP或Zip库时并不仅仅限于文件——可以压缩任何东西,包括要通过网络连接发送的数据。