文件导出ZIP压缩

	@Override
public String downloadallfiles(HttpServletRequest request, String lsbpId, String bsTitle) {
JwtUserObject jwtUserObject = JwtUtil.getJwtUserObj(request.getHeader("Authorization"));
String userid = jwtUserObject.getUSERID();
String username = jwtUserObject.getUSERNAME();
String itemcode = jwtUserObject.getITEMCODE();
String orgcode = jwtUserObject.getORGCODE();
String useridentity = jwtUserObject.getUSERIDENTITY();
//判断是否已经压缩过该文件
List<FileObject> list = benbuMapper.findCzFileZipById(lsbpId);
if (list!=null && list.size() > 0) {
String path = sso.getPresignedObjectUrlByBucketNameAndPath(list.get(0).getBucketname(), list.get(0).getFilepath());
return path;
} File file1 = new File(tempfile);
if (!file1.exists()) {
file1.mkdirs();
} else {
deleteFile(file1);
if (flag == 1) {
System.out.println("文件删除成功!");
}
file1.mkdirs();
}
//根据本部主键查询厂站报送的文件
List<HashMap<String, String>> files = benbuMapper.findCzFileById(lsbpId);
if (files != null && files.size() > 0) {
for (HashMap map : files) {
//临时目录下新增文件
File file2 = new File(tempfile + "/" + map.get("itemCode"));
if (!file2.exists()) {
file2.mkdirs();
}
sso.downLoadFileByid((String) map.get("fileId"), tempfile + "/" + map.get("itemCode"));
}
}
//文件压缩
FileZipUtil.compressToZip(tempfile, zipDowload, bsTitle + ".zip"); FileItem fileItem = null;
MultipartFile file = getMultipartFile(bsTitle);
FileObject fileObject2 = sso.uploadFileCommondLs(file, userid, username, itemcode, orgcode, "10005", useridentity, lsbpId,
itemcode.toLowerCase(), FileTypeConstant.COMP_10 + "/10005/", "");
String path = sso.getPresignedObjectUrlByBucketNameAndPathLs(fileObject2.getBucketname(), fileObject2.getFilepath()); //删除本地文件
deleteFile(new File(zipDowload)); return path; }

文件压缩工具类

public class FileZipUtil {
/**
* 压缩文件
*
* @param sourceFilePath 源文件路径
* @param zipFilePath 压缩后文件存储路径
* @param zipFilename 压缩文件名
*/
public static String compressToZip(String sourceFilePath, String zipFilePath, String zipFilename) {
File sourceFile = new File(sourceFilePath);
File zipPath = new File(zipFilePath);
if (!zipPath.exists()) {
zipPath.mkdirs();
}
File zipFile = new File(zipPath + File.separator + zipFilename);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
writeZip(sourceFile, "", zos);
return "压缩完成";
//文件压缩完成后,删除被压缩文件
// boolean flag = deleteDir(sourceFile);
// System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
} /**
* 遍历所有文件,压缩
*
* @param file 源文件目录
* @param parentPath 压缩文件目录
* @param zos 文件流
*/
public static void writeZip(File file, String parentPath, ZipOutputStream zos) throws IOException {
if (file.isDirectory()) {
//目录
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else {
//文件
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
//指定zip文件夹
ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
zos.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[1024 * 10];
while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, len);
zos.flush();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
} /**
* 删除文件夹
*
* @param dir
* @return
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
//删除空文件夹
return dir.delete();
}
}

优化

@NotNull
private static String zipPersonFace(List<PersonVo> personList) {
String compressedPath = FilePathConfig.gettempfilepath() + "人脸照片.zip";
log.info("使用Buffer流压缩。。。。。。。。。。");
long beginTime = System.currentTimeMillis();
try {
FileOutputStream fos = new FileOutputStream(compressedPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zipOut = new ZipOutputStream(bos);
zipOut.setLevel(Deflater.BEST_SPEED);
for (PersonVo personVo : personList) {
zipFaceByUrl(personVo, zipOut);
}
zipOut.close();
bos.close();
fos.close();
long endTime = System.currentTimeMillis();
log.info("导出用时:" + (endTime - beginTime));
} catch (IOException e) {
log.error("导出失败..." + e);
throw new BusinessException(ResultCode.OPERATION_FAILURE, "导出失败..." + e);
}
return FilePathConfig.DOWNLOAD_URL + "人脸照片.zip";
}
private static void zipFaceByUrl(PersonVo personVo, ZipOutputStream zipOut) throws IOException {
String faceUrl = personVo.getFaceUrl();
if (StrUtil.isEmpty(faceUrl) || !faceUrl.startsWith("http")) {
return;
}
URL url = new URL(faceUrl);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
String suffix;
if (faceUrl.contains("?")) {
suffix = faceUrl.substring(faceUrl.lastIndexOf("."), faceUrl.indexOf("?"));
} else {
suffix = faceUrl.substring(faceUrl.lastIndexOf("."));
}
//照片名称
String fileName = personVo.getDepartmentName() + "_" + personVo.getName() + "(" + personVo.getCode() + ")" + suffix;
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
zipOut.write(buffer, 0, bytesRead);
}
zipOut.closeEntry();
bis.close();
}

文件导出ZIP压缩的更多相关文章

  1. C# 对多个文件进行zip压缩

    本文使用的ICSharpCode.SharpZipLib.dll类库来实现文件压缩,你可以通过Nuget来安装此类库,或者到搜索引擎去搜索一下遍地都是.类库下载下来之后,添加到项目引用就可以了.下面这 ...

  2. C#使用SharpZipLib解压多文件的zip压缩文件数据流,保存到本地

    代码: public async Task<ReturnModel<List<string>>> UploadModel() { var task = new Ta ...

  3. PHP生成ZIP压缩文件

    PHP生成ZIP压缩文件 /* * 生成zip压缩文件 * $sourceDir:被压缩的文件夹或文件 * $outFileName:输出的压缩文件名称 * */ function createZip ...

  4. Spark- Spark从SFTP中读取zip压缩文件数据做计算

    我们遇到个特别的需求,一个数据接入的流程跑的太慢,需要升级为用大数据方式去处理,提高效率. 数据: 数据csv文件用Zip 压缩后放置在SFTP中 数据来源: SFTP 数据操作: 文件和它的压缩包一 ...

  5. Java Zip压缩实现

    最近在自学javaWeb,先复习一下java,把还给老师的东西再找回来(知识如果不用很快就会忘记啊).. 今天看到了zip压缩,决定要整理一下. java将有关zip压缩的内容都封装在java.uti ...

  6. linux的文件打包与压缩

    简介 Linux 上常用的压缩/解压工具,介绍了zip.rar.tar的使用. 文件打包和压缩 Linux 上的压缩包文件格式,除了 Windows 最常见的*.zip.*.rar..7z 后缀的压缩 ...

  7. NPOI,导出Execl,压缩文件zip,发送Email

    private void SendEmail(string emailAddress, string companyName,string proxy, string officer, DataTab ...

  8. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  9. Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)

    1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...

  10. linux专题一之文件归档和压缩(tar、file、zip)

     本文主要从以下几个方便来说明文件的归档和压缩,同时比较几种不同压缩方法的压缩比率及特点. 文件归档命令tar,tar.gz源码包的安装管理 创建tar包-解压-查询tar包内容 zip命令的用法 为 ...

随机推荐

  1. DeepSeek 全面指南,95% 的人都不知道的9个技巧(建议收藏)

    大家好,我是汤师爷~ 最近,DeepSeek这款AI工具爆火国内外. 虽然许多人都开始尝试使用它,但有人吐槽说,没想象中那么牛. 其实问题不在工具,很多人的使用姿势就搞错了,用大炮打蚊子,白白浪费De ...

  2. GPU虚拟化技术简介:实现高性能图形处理的灵活部署

    本文分享自天翼云开发者社区<GPU虚拟化技术简介:实现高性能图形处理的灵活部署>,作者:z****n GPU虚拟化技术是一项重要的创新,通过将物理GPU划分为多个虚拟GPU,实现多用户共享 ...

  3. C# 深度学习框架 TorchSharp 原生训练模型和图像识别-自定义网络模型和识别手写数字

    目录 使用 Torch 训练模型 定义神经网络 加载数据集 创建网络模型 定义损失函数 训练 识别手写图像 教程名称:使用 C# 入门深度学习 作者:痴者工良 教程地址:https://torch.w ...

  4. ESP8266+AT指令,使用MQTT协议连接阿里云

    AT指令集 AT+CWMODE=1 AT+CWJAP="热点名","热点密码" AT+CIPSNTPCFG=1,8,"ntp1.aliyun.com& ...

  5. [CF576E] Painting Edges 题解

    模版题的升级了. 使用二分图经典判定方法(一个点拆成两个点 \(x,x+n\),连边 \((x,y)\) 就是连接 \((x,y+n),(x+n,y)\),那么是否是二分图就等价于判断 \(x,x+n ...

  6. Android:如何在后台启动Activity

    通常我用这段代码开始一个活动: Intent i = new Intent(context, MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NE ...

  7. div剩余高度自动填充满

    这边采用弹性布局来处理 在需要被填充满的div上设置display:flex;,然后根据你所需要填充宽度(flex-direction:column;)高度(flex-direction:row;)设 ...

  8. Joe主题 更换评论框样式,填写QQ自动获取昵称邮箱

    前言: 由于为了美观感去除了画图模式,至于怎么加上画图模式会在文章最后给出详细教程. 同时也去除了填写网址选项,这个暂时无添加教程,关键没有合适的添加位子. 话不多说,直接开始教程! 下载文件包,解压 ...

  9. 微信小程序获取环境变量

    微信小程序获取环境变量 在微信小程序中,无法直接获取环境变量.但是,我们可以通过其他方式来模拟环境变量的功能. 参考用法 通过 wx.getAccountInfoSync() 获取小程序信息,包含小程 ...

  10. 一文速通Python并行计算:00 并行计算的基本概念

    一文速通 Python 并行计算:00 并行计算的基本概念 摘要: 该文介绍了 Python 并行计算的核心概念.编程模型及其应用,并介绍了了并行程序的性能分析与优化方法,如并行效率.加速比及 Amd ...