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.


import java.util.Objects;

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

        try (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())) {
            try (ZipOutputStream zos =
                         new ZipOutputStream(new BufferedOutputStream(checksum))) {

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

                // Get all text files on the working folder.
                File dir = new File(".");
                String[] files = dir.list((directory, name) -> name.endsWith(".txt"));

                for (String file : Objects.requireNonNull(files)) {
                    System.out.println("Compressing: " + file);

                    try (FileInputStream fis = new FileInputStream(file);
                         BufferedInputStream bis =
                                 new BufferedInputStream(fis, buffer.length)) {

                        // put a new ZipEntry in the ZipOutputStream
                        ZipEntry zipEntry = new ZipEntry(file);

                        // 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);


            // Print out checksum value
            long value = checksum.getChecksum().getValue();
            System.out.println("Checksum   : " + value);

Leave a Reply

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