Create Checksum For Zip File In Java

Chapter: ZIP Last Updated: 07-08-2017 15:50:08 UTC

Program:

            /* ............... START ............... */
                
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 JavaCheckSumZipFile {
	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());
	}
}

                /* ............... END ............... */
        

Notes:

  • A checksum is a small-size datum from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage.
  • Checksum is not computed from the complete zip file, but from the user's data only.

Tags

Checksum For A Zip, Java

Similar Programs Chapter Last Updated
Zip Entry Is Directory Example In Java ZIP 23-04-2016
Create Zip File With CRC32 Checksum In Java ZIP 23-04-2016
Create Zip File With Adler32 Checksum In Java ZIP 23-04-2016
Extract Zip File With Adler32 Checksum In Java ZIP 23-04-2016
Extract File With CRC32 Checksum In Java ZIP 23-04-2016
CRC32 Checksum In Java ZIP 10-08-2016
Adler32 Checksum In Java ZIP 23-04-2016
Read Zip File Checksum Value In Java Example ZIP 20-07-2016
Decompress Zip File Using ZipInputStream In Java ZIP 20-07-2016
Read Entries In Zip File In Java Example ZIP 20-07-2016
List The Contents Of Zip File In Java Example ZIP 07-08-2017
Extract Contents Of Zip File In Java Example ZIP 07-08-2017
Zip File Creation In Java Example ZIP 23-06-2016

1