java.util.zip.CheckedInputStream.getChecksum()
方法返回此输入流的校验和。
参考以下示例代码 -
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class Adler32Demo {
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, though
for (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));
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();
}
}
} ///
执行上面示例代码,得到以下结果 -
Checksum: 1923287158
Reading file
Checksum: 1