How do I create checksum for a zip file?

This example demonstrate how to use CheckedOutputStream for creating a checksum of a zip file. Checksum can be used to detect whether a data was corrupted during a transmission process to a remote machine.

package org.kodejava.example.util;


public class ZipWithChecksum {
    public static void main(String[] args) {
        try {
            String target = "";

            FileOutputStream fos = new FileOutputStream(target);

            // An output stream that also maintains a checksum of the data being
            // written. The checksum can then be used to verify the integrity of
            // the output data.
            CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32());
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));

            int size = 0;
            byte[] buffer = new byte[1024];

            // Get all text files on the working folder.
            File dir = new File(".");
            String[] files = dir.list(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    if (name.endsWith(".txt")) {
                        return true;
                    } else {
                        return false;

            for (int i = 0; i < files.length; i++) {
                System.out.println("Compressing: " + files[i]);

                FileInputStream fis = new FileInputStream(files[i]);
                BufferedInputStream bis = new BufferedInputStream(fis, buffer.length);

                // put a new ZipEntry in the ZipOutputStream
                ZipEntry zipEntry = new ZipEntry(files[i]);

                // read data to the end of the source file and write it to the zip
                // output stream.
                while ((size =, 0, buffer.length)) > 0) {
                    zos.write(buffer, 0, size);


            // Finish zip process

            // Print out checksum value
            System.out.println("Checksum   : " + checksum.getChecksum().getValue());
        } catch (IOException e) {

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🙂.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.