import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.*;
private static void ziper(File f, String s, ZipOutputStream zos) throws Exception {
if(f.isDirectory()) {
s += s.equals("") ? "" : "/";
for(File F : f.listFiles()) {
ziper(F, s + F.getName(), zos);
}
}
if(f.isFile()) {
zos.putNextEntry(new ZipEntry(s));
System.out.println(s);
Long l = f.length();
byte[] b = new byte[l.intValue()];
FileInputStream fis = new FileInputStream(f);
fis.read(b);
fis.close();
zos.write(b);
zos.flush();
zos.closeEntry();
}
}
public static void ziper(String[] sa) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
int[] i = {sa[0].lastIndexOf(File.separator), sa[0].lastIndexOf(".")};
StringBuffer sb = new StringBuffer(i[1] - i[0] > 0 ? sa[0].substring(i[0] + 1, i[1]) : sa[0]);
sb.append(" " + sdf.format(new Date()) + ((new Random()).nextInt(90) + 10) + ".zip");
FileOutputStream fos = new FileOutputStream(sb.toString());
CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
zos.setComment("m@20190326.yiyi");
zos.setLevel(Deflater.BEST_COMPRESSION);
zos.setMethod(ZipOutputStream.DEFLATED);
for(String s : sa) {
File f = new File(s);
ziper(f, f.getName(), zos);
}
zos.close();
cos.close();
fos.close();
}