https://www.cnblogs.com/qingergege/p/5768376.html
public String compressFolder(String path) {
ZipOutputStream out = null;
BufferedOutputStream bos = null;
try {
out = new ZipOutputStream(new FileOutputStream(path + ".zip"));
bos = new BufferedOutputStream(out);
File sourceFile = new File(path);
String base = sourceFile.getName();
compress(out, bos, sourceFile, base);
} catch (Exception ex) {
} finally {
try {
bos.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "1";
}
private void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws Exception
{
if(sourceFile.isDirectory()) {
File[] flist = sourceFile.listFiles();
if(flist.length==0) {
out.putNextEntry(new ZipEntry(base+"/") );
} else {
for(int i=0;i<flist.length;i++) {
compress(out, bos, flist[i], base+"/"+flist[i].getName());
}
}
} else {
ZipEntry zipEntry = new ZipEntry(base);
zipEntry.setSize(sourceFile.length());
zipEntry.setTime(sourceFile.lastModified());
out.putNextEntry(zipEntry);
InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
int readLength = 0;
byte[] buffer = new byte[512];
while ((readLength = inputStream.read(buffer, 0, 512)) != -1) {
out.write(buffer, 0, readLength);
}
inputStream.close();
}
}