import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
/**
* 工具类
* zip操作工具类
*/
public class UtilsZIP {
private static final Logger LOG = LoggerFactory.getLogger(UtilsZIP.class);
private UtilsZIP() {
}
/**
* 打包zip
* stream方式
*/
public static CallUtilsZIPResult<ByteArrayOutputStream> packToZip(List<ExportExcelListBean> fileList) {
CallUtilsZIPResult<ByteArrayOutputStream> callUtilsZIPResult = new CallUtilsZIPResult();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipArchiveOutputStream zip = new ZipArchiveOutputStream(out);
for (ExportExcelListBean file : fileList) {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getFileName());
zipEntry.setSize(file.getFile().length());
zip.putArchiveEntry(zipEntry);
InputStream in = file.getInputStream();
IOUtils.copy(in, zip);
IOUtils.closeQuietly(in);
}
zip.closeArchiveEntry();
IOUtils.closeQuietly(zip);
IOUtils.closeQuietly(out);
callUtilsZIPResult.setSuccess(true);
callUtilsZIPResult.setResult(out);
} catch (Exception e) {
LOG.info("[工具类异常]:exception:{}", UtilsException.getExceptionMsg(e));
callUtilsZIPResult.setSuccess(false);
callUtilsZIPResult.setMsg("生成文件EXCEL异常信息:" + e.getMessage());
}
return callUtilsZIPResult;
}
public static class ExportExcelListBean<E> {
private String fileName;
private File file;
private InputStream inputStream;
public E filesSource;
public E getFilesSource() {
return filesSource;
}
public void setFilesSource(E filesSource) {
this.filesSource = filesSource;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
public static class CallUtilsZIPResult<T> implements Serializable {
public boolean success = false;
public String msg;
public long code;
public T result;
public CallUtilsZIPResult() {
}
public CallUtilsZIPResult(long code, String msg) {
this.success = false;
this.code = code;
this.msg = msg;
this.result = null;
}
public CallUtilsZIPResult(T result) {
this.success = true;
this.code = 0L;
this.msg = "成功!";
this.result = result;
}
public boolean getSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
}
}
文件打包成zip流(stream方式)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...