使用新版 net.lingala.zip4j.ZipFile 对文件打包加密


使用新版 net.lingala.zip4j.ZipFile 对文件打包加密

<dependency>
  <groupId>net.lingala.zip4j</groupId>
  <artifactId>zip4j</artifactId>
  <version>2.9.1</version>
</dependency>

注意低版本的lingala打包语法和参数略有不同

参考代码

/**
* 获取文件接口.
 * @param request 请求信息.
 * @return
 */
@SuppressWarnings("resource")
public void getFile(@RequestBody FileDownRequest reqVo, final HttpServletResponse response) {
	final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	String logcode = "";
	OutputStream out = null;
	InputStream in = null;
	File zipfile = null;
	try {
		final String interfaceCode = reqVo.getHead().getInterfaceCode();
		if("FILE_DOWNLOAD".equals(interfaceCode)) {
			final String zipName = sdf.format(new Date()) + ".zip";
			final FileDowntBody body = reqVo.getBody();
			final String encryptiontype = body.getEncryptionType();
			final List<String> fileList = body.getFileList();
			logcode = body.getLogCode();
			LOG.info(logcode + " IN getFile " + interfaceCode);
			final ArrayList<File> list = new ArrayList<File>();
			int goodFile = 0;
			if (fileList!=null && fileList.size()>0) {
				for (String filePathOne : fileList) {
					final File fO = new File(filePathOne );
					if (fO!=null && fO.exists() && fO.isFile() && fO.length()>0) {
						goodFile ++;
						list.add(fO);
					}
				}
				LOG.info(logcode + " 实际有效文件数/总文件数: " + goodFile + "|" + fileList.size());
			}
			if (list.size()==0) {
				return;
			}
			//创建1个ZIP
			//获取平台可用的缓存目录
			final String tempPath = "D://DEMO//";
			FileUtils.createDirectory(tempPath);
			final String zipPath = tempPath + zipName;
			ZipFile zipOne = null;
			final ZipParameters pram = new ZipParameters();
			//压缩方式
			pram.setCompressionMethod(CompressionMethod.DEFLATE);
			//压缩级别
			pram.setCompressionLevel(CompressionLevel.FASTEST);
			//设置备注
			pram.setFileComment("我是备注");
			//需要加密
			if (!"0".equals(encryptiontype)) {
				pram.setEncryptFiles(true);//开启加密
				pram.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
				zipOne = new ZipFile(zipPath, "我是压缩密码".toCharArray());
			} else {
				zipOne = new ZipFile(zipPath);
			}
			zipOne.addFiles(list, pram);
			//ZIP
			LOG.info(logcode + " ZIP文件已生成 " + interfaceCode);
			zipfile = new File(zipPath);
			if (zipfile!=null && zipfile.exists() && zipfile.isFile()) {//开始进行网络发送.
				response.addHeader("Content-Disposition", "attachment;filename=" + zipName);
				out = response.getOutputStream();
				in = new FileInputStream(zipfile);
				int len = 0;
				byte[] buffer = new byte[1024 * 10];
				while ((len = in.read(buffer)) != -1) {
					out.write(buffer, 0, len);
				}
				out.flush();
			}
		}
	} catch (Exception e) {
		LOG.error(logcode + " getFile,异常 ", e);
	} finally {
		try {
			if (in!=null) {
				in.close();
			} 
			if(out!=null) {
				out.close();
			}
			if (zipfile!=null && zipfile.exists() && zipfile.isFile()) {
				LOG.info(logcode + " ZIP文件缓存已删除,标示 " + zipfile.delete());
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}	

官方地址

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值