Create checksum for a zip file in Java
Description
The following code shows how to create checksum for a zip file.
Example
/*from w ww. j a v a 2 s .co m*/
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
CheckedOutputStream checksum = new CheckedOutputStream(
new FileOutputStream("data.zip"), new Adler32());
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));
int size = 0;
byte[] buffer = new byte[1024];
File dir = new File(".");
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
System.out.println("Compressing: " + files[i]);
FileInputStream fis = new FileInputStream(files[i]);
ZipEntry zipEntry = new ZipEntry(files[i]);
zos.putNextEntry(zipEntry);
while ((size = fis.read(buffer, 0, buffer.length)) > 0) {
zos.write(buffer, 0, size);
}
zos.closeEntry();
fis.close();
}
zos.close();
System.out.println("Checksum : " + checksum.getChecksum().getValue());
}
}
The code above generates the following result.