代码留存,zip压缩。
public static void zip(File file, File zipFile) throws Exception {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(new FileOutputStream(zipFile));
//如出现编码混乱请加入编码要求,一般出现在英文系统中
output.setEncoding("UTF-8");
createdir = false;
zipFile(output, file, "");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (output != null) {
output.flush();
output.close();
}
}
}
public static void zipFile(ZipOutputStream output, File file,
String basePath) throws IOException {
FileInputStream input = null;
try {
if (file.isDirectory()) {
File list[] = file.listFiles();
basePath = basePath + (basePath.length() == 0 ? "" : "/")
+ file.getName();
if (!createdir) {
basePath = "";
createdir = true;
}
for (File f : list)
zipFile(output, f, basePath);
} else {
basePath = (basePath.length() == 0 ? "" : basePath + "/")
+ file.getName();
output.putNextEntry(new ZipEntry(basePath));
input = new FileInputStream(file);
int readLen = 0;
byte[] buffer = new byte[1024 * 8];
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null)
input.close();
}
}