Sample Programs

Sample java program to compress (Zip) a list of files using Checksum


 package org.jusfortechies.example; 

 import java.io.*;
 import java.util.zip.*;

 public class Zip {
   
   static final int BUFFER = 2048;

   public static void main(String argv[]) {
     try {
       BufferedInputStream origin = null;
       String dirPath = "C:\\SamplePrograms";

       FileOutputStream dest = new FileOutputStream(dirPath + "\\myZip.zip");
       CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
       ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));
       
       byte data[] = new byte[BUFFER];

	   // get a list of files from current directory
       File f = new File(dirPath);
       String files[] = f.list();

       for (int i = 0; i < files.length; i++) {
         System.out.println("Adding: " + files[i]);
         FileInputStream fi = new FileInputStream(dirPath + "\\" + files[i]);
         origin = new BufferedInputStream(fi, BUFFER);
         ZipEntry entry = new ZipEntry(files[i]);
         out.putNextEntry(entry);
         int count;
         while ((count = origin.read(data, 0, BUFFER)) != -1) {
           out.write(data, 0, count);
         }
         origin.close();
       }

       out.close();
       
	   System.out.println("Checksum: " + checksum.getChecksum().getValue());

     } catch (Exception e) {
       e.printStackTrace();
     }
  }

 }




blog comments powered by Disqus