Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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());
    }
}