1. 压缩指定目录下的文件/文件夹
/**
* 压缩工具类
*/
@Slf4j
public class ZipUtils {
private static final String DEFAULT_CHARSET = "UTF-8";
private static final int BUFFER_SIZE = 2 * 1024;
/**
* 压缩文件夹
*
* @param zipFileName 打包后文件的名称,含路径
* @param sourceFolder 需要打包的文件夹或者文件的路径
* @param zipPathName 打包目的文件夹名,为空则表示直接打包到根
*/
public static void zip(String zipFileName, String sourceFolder, String zipPathName) throws Exception {
ZipOutputStream out = null;
try {
File zipFile = new File(zipFileName);
FileUtil.mkdir(zipFile.getParent());
FileOutputStream outputStream = new FileOutputStream(zipFile);
out = new ZipOutputStream(outputStream);
if (StringUtils.isNotBlank(zipPathName)) {
zipPathName = FilenameUtils.normalizeNoEndSeparator(zipPathName, true) + "/";
} else {
zipPathName = "";
}
zip(out, sourceFolder, zipPathName);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* 压缩文件夹
*
* @param zipFile 打包后文件的名称,含路径
* @param source 需要打包的文件夹或者文件的路径
*/
public static void zip(String zipFile, String source) throws Exception {
File file = new File(source);
zip(zipFile, source, file.isFile() ? StringUtils.EMPTY : file.getName()