zipTest.java
package com.test.file;
import java.io.*;
import java.util.zip.*;
public class ZipTest {
static final int BUFFER = 2048;
public static void main(String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream("e://zip//myfigs.zip");
CheckedOutputStream checksum = new CheckedOutputStream(dest, new CRC32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));
// out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File("E://zip//file");
String files[] = f.list();
for (int i = 0; i < files.length; i++) {
System.out.println("Adding: " + files[i]);
FileInputStream fi = new FileInputStream("E://zip//file//"+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();
}
}
}
unziptest.java
package com.test.file;
import java.io.*;
import java.util.zip.*;
public class UnzipTest {
public static void main(String argv[]) {
try {
final int BUFFER = 2048;
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream("e://zip//myfigs.zip");
CheckedInputStream checksum = new CheckedInputStream(fis, new CRC32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " + entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
//System.out.println(entry.getName());
FileOutputStream fos = new FileOutputStream("E://zip//new//"+entry.getName());
dest = new BufferedOutputStream(fos,BUFFER);
while ((count = zis.read(data, 0,BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
System.out.println("Checksum:" + checksum.getChecksum().getValue());
} catch (Exception e) {
e.printStackTrace();
}
}
}