import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zip {
public void ZipFiles(String file, String savepath) {
try {
File inFile = new File(file);
FileOutputStream fout = new FileOutputStream(savepath);
// 使用输出流检查
CheckedOutputStream cs = new CheckedOutputStream(fout,new Adler32());
// 声明输出zip流
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(cs));
// 写一个注释
zout.setComment("This is the comment");
zip(zout, inFile, "", cs);
zout.close();
} catch (Exception e) {
System.err.println(e);
}
}
private void zip(ZipOutputStream out, File inFile, String root,CheckedOutputStream cs) throws Exception {
if (inFile.isDirectory()) {
File[] files = inFile.listFiles();
out.putNextEntry(new ZipEntry(root + "/"));
root = root.length() == 0 ? "" : root + "/";
for (int i = 0; i < files.length; i++) {
zip(out, files[i], root + files[i].getName(), cs);
}
}else{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFile));
out.putNextEntry(new ZipEntry(root));
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
// System.out.println("Checksum::" + cs.getChecksum().getValue());
}
}
public static void main(String[] args){
new Zip().ZipFiles("E://BAK","E://BAK.ZIP");
String a = "2010";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
try {
System.out.println(sdf.parse(a));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}