44 lines
1.3 KiB
Java
44 lines
1.3 KiB
Java
package com.zcloud.util;
|
||
|
||
import java.io.File;
|
||
import java.io.*;
|
||
import java.util.List;
|
||
import java.util.zip.*;
|
||
|
||
public class CompressDownloadUtil {
|
||
|
||
/**
|
||
* 将多个文件压缩到指定输出流中
|
||
*
|
||
* @param files 需要压缩的文件列表
|
||
* @param outputStream 压缩到指定的输出流
|
||
* @author hongwei.lian
|
||
* @date 2018年9月7日 下午3:11:59
|
||
*/
|
||
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) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
|
||
}
|