<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
public class ZipUtils {
/**
* 将多个文件压缩到指定输出流中
*
* @param files 需要压缩的文件列表
* @param outputStream 压缩到指定的输出流
*/
public static void compressZip(List<File> files, OutputStream outputStream) {
ZipOutputStream zipOutStream = null;
try {
// 包装成ZIP格式输出流
zipOutStream = new ZipOutputStream(new BufferedOutputStream(outputStream));
// 设置压缩方法
zipOutStream.setMethod(ZipOutputStream.DEFLATED);
// 将多文件循环写入压缩包
for (int i = 0; i < files.size(); i++) {
File file = files.get(i);
FileInputStream filenputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
filenputStream.read(data);
//-- 添加ZipEntry,并ZipEntry中写入文件流,这里,加上i是防止要下载的文件有重名的导致下载失败
zipOutStream.putNextEntry(new ZipEntry(i + file.getName()));
zipOutStream.write(data);
filenputStream.close();
zipOutStream.closeEntry();
}
} catch (IOException e) {
log.error("error,msg:{}", e.getMessage());
} finally {
try {
if (Objects.nonNull(zipOutStream)) {
zipOutStream.flush();
zipOutStream.close();
}
if (Objects.nonNull(outputStream)) {
outputStream.close();
}
} catch (IOException e) {
log.error("error,msg:{}", e.getMessage());
}
}
}
/**
* 打包单个文件
*
* @param localFileName 本地文件名称
* @param is 源文件-输入流
* @param fileName 源文件-文件名
*/
public static void zipOneFile(String localFileName, InputStream is, String fileName) {
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new FileOutputStream(localFileName));
zipOut.putNextEntry(new ZipEntry(fileName));
// 输出文件
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) > 0) {
zipOut.write(buffer, 0, len);
}
} catch (IOException e) {
log.error("获取文件失败:{}", e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
log.error("关闭流失败:{}", e.getMessage());
}
}
}
}
/**
* 压缩多个文件到本地
*
* @param zipFileDir 文件目录
* @param filePaths 要打包的文件的路径
*/
public static void zipFiles(String zipFileDir, String fileName, List<String> filePaths) {
File zipPathFile = new File(zipFileDir);
if (!zipPathFile.exists()) {
zipPathFile.mkdirs();
}
String localFileName = zipFileDir + File.separator + fileName + ".zip";
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new FileOutputStream(localFileName));
for (String filePath : filePaths) {
File file = new File(filePath);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
// 单个文件名称
zipOut.putNextEntry(new ZipEntry(file.getName()));
// 输出文件
int len = 0;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) > 0) {
zipOut.write(buffer, 0, len);
}
} catch (Exception e) {
log.error("压缩文件失败:{}", e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
log.error("关闭流失败:{}", e.getMessage());
}
}
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (zipOut != null) {
try {
zipOut.close();
} catch (IOException e) {
log.error("关闭流失败:{}", e.getMessage());
}
}
}
}
public static void main(String[] args) {
List<String> fileList = Arrays.asList(
"/Users/mengday/Temp/springboot-rdffile/src/main/resources/data/de_all1.txt",
"/Users/mengday/Temp/springboot-rdffile/src/main/resources/data/de_all2.txt"
);
zipFiles("/Users/mengday/Temp", "打包文件", fileList);
}
}
public class TempTest {
public static void main(String[] args) throws Exception {
List<String> fileList = Arrays.asList("/Users/mengday/Temp/springboot-rdffile/src/main/resources/data/de_all1.txt",
"/Users/mengday/Temp/springboot-rdffile/src/main/resources/data/de_all2.txt"
);
String zipPath = zip(fileList, "data1", "/Users/mengday/Temp");
System.out.println(zipPath);
add(zipPath);
}
public static String zip(List<String> fileList, String zipName, String path) throws Exception {
String zipPath = path + File.separator + zipName + ".zip";
File targetFile = new File(zipPath);
if (targetFile.exists()) {
targetFile.delete();
}
try (FileOutputStream out = new FileOutputStream(new File(zipPath));
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out))) {
byte[] buffer = new byte[1024];
for (int i = 0; i < fileList.size(); i++) {
String fileTempName = fileList.get(i);
File file = new File(fileTempName);
if (file.exists()) {
zos.putNextEntry(new ZipEntry(file.getName()));
try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream fif = new BufferedInputStream(fis);) {
int len = 0;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fif.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
} catch (Exception e) {
log.error("zip error", e);
}
} else {
zos.write(("文件未找到 " + file.getName()).getBytes());
zos.closeEntry();
}
}
} catch (Exception e) {
throw e;
}
return zipPath;
}
public static void add(String zipFilePath) throws Exception {
File zipFile = new File(zipFilePath);
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
FileInputStream fis = new FileInputStream(zipFilePath);
ByteArrayInputStream bis = new ByteArrayInputStream("aaa".getBytes());
ByteArrayInputStream bis2 = new ByteArrayInputStream("bbb".getBytes())
){
zos.putNextEntry(new ZipEntry("data.zip"));
IOUtils.copy(fis, zos);
zos.putNextEntry(new ZipEntry("signature.txt"));
IOUtils.copy(bis, zos);
zos.putNextEntry(new ZipEntry("encryptKey.txt"));
IOUtils.copy(bis2, zos);
zos.closeEntry();
}
}
}