java.util.zip.CheckedInputStream getChecksum()方法

java.util.zip.CheckedInputStream getChecksum()方法 介绍

java.util.zip.CheckedInputStream.getChecksum() 方法返回此输出流的校验和。

java.util.zip.CheckedInputStream getChecksum()方法 声明

public Checksum getChecksum()

java.util.zip.CheckedInputStream getChecksum()方法 示例

下面的例子展示了 java.util.zip.CheckedInputStream getChecksum() 方法的用法。

package com.yiidian;

import java.io.*;
import java.util.zip.*;

public class Main {
  public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");

    for (int i = 0; i < args.length; i++) {
      System.out.println("Writing file " + args[i]);
      BufferedReader in = new BufferedReader(new FileReader(args[i]));
      zos.putNextEntry(new ZipEntry(args[i]));
      int c;
      while ((c = in.read()) != -1)
        out.write(c);
      in.close();
    }
    out.close();


    System.out.println("Checksum: " + csum.getChecksum().getValue());

    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
      System.out.println("Reading file " + ze);
      int x;
      while ((x = bis.read()) != -1)
        System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();


  }
}

输出结果为:

热门文章

优秀文章