java安全解压文件

方法一

public class Decompression {private static final Logger LOGGER = Logger.getLogger(Decompression.class);private static final int BUFFER = 512;private static final int TOO_BIG = 0x6400000;private static final int TOO_MANY = 1024;private Decompression() {}public static void zipUncompress(String inputFile, String destDirPath) {FileOutputStream fileOutputStream = null;InputStream inputStream = null;ZipFile zipFile = null;try {File srcFile = new File(inputFile);if (!srcFile.exists()) {throw new IOException(srcFile.getPath());}zipFile = new ZipFile(srcFile);Enumeration entries = zipFile.entries();int total = 0;int entriesNumber = 0;while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();checkFileName(entry.getName(), destDirPath);int count = 0;if (entry.isDirectory()) {if (!srcFile.mkdir()) {throw new IOException();}} else {String fileName = Tools.delTimeStamp(entry.getName());File targetFile = new File(destDirPath + File.separator + fileName);if (!targetFile.getParentFile().exists()) {if (targetFile.getParentFile().mkdirs()) {throw new IOException();}}if (!targetFile.createNewFile()) {throw new IOException();}inputStream = zipFile.getInputStream(entry);fileOutputStream = new FileOutputStream(targetFile);int len;byte[] buf = new byte[BUFFER];while ((len = inputStream.read(buf)) != -1) {total += count;if (total > TOO_BIG) {LOGGER.error("Zip file is too big.");break;}fileOutputStream.write(buf, 0, len);}entriesNumber++;if (total > TOO_BIG) {LOGGER.error("Zip file is too big.");break;}if (entriesNumber > TOO_MANY) {LOGGER.error("Zip file is too many.");break;}}}} catch (IOException e) {LOGGER.error("IOException error.", e);} finally {closeProcess(inputFile, fileOutputStream, inputStream, zipFile);}}private static String checkFileName(String entryName, String intendedDir) throws IOException {File file = new File(intendedDir, entryName);String canonicalPath1 = file.getCanonicalPath();File intendedFile = new File(intendedDir);String canonicalPath2 = intendedFile.getCanonicalPath();if (canonicalPath1.startsWith(canonicalPath2)) {return canonicalPath1;} else {throw new IllegalStateException();}}private static void closeProcess(String inputFile, FileOutputStream fileOutputStream, InputStream inputStream,ZipFile zipFile) {try {if (zipFile != null) {zipFile.close();}if (fileOutputStream != null) {fileOutputStream.close();}if (inputFile != null) {inputStream.close();}} catch (IOException e) {LOGGER.error("Close error.", e);}}
}

方法二

public class Decompression {private static final Logger LOGGER = Logger.getLogger(Decompression.class);private static final int BUFFER = 512;private static final int TOO_BIG = 0x6400000;private static final int TOO_MANY = 1024;private Decompression() {}public static void zipUncompress(String inputFile, String destDirPath) {FileOutputStream fileOutputStream = null;InputStream inputStream = null;ZipFile zipFile = null;try {File srcFile = new File(inputFile);if (!srcFile.exists()) {throw new IOException(srcFile.getPath());}zipFile = new ZipFile(srcFile);Enumeration entries = zipFile.entries();int total = 0;int entriesNumber = 0;while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();checkFileName(entry.getName(), destDirPath);int count = 0;if (entry.isDirectory()) {if (!srcFile.mkdir()) {throw new IOException();}} else {String fileName = Tools.delTimeStamp(entry.getName());File targetFile = new File(destDirPath + File.separator + fileName);if (!targetFile.getParentFile().exists()) {if (targetFile.getParentFile().mkdirs()) {throw new IOException();}}if (!targetFile.createNewFile()) {throw new IOException();}inputStream = zipFile.getInputStream(entry);fileOutputStream = new FileOutputStream(targetFile);int len;byte[] buf = new byte[BUFFER];while ((len = inputStream.read(buf)) != -1) {total += count;if (total > TOO_BIG) {LOGGER.error("Zip file is too big.");break;}fileOutputStream.write(buf, 0, len);}entriesNumber++;if (total > TOO_BIG) {LOGGER.error("Zip file is too big.");break;}if (entriesNumber > TOO_MANY) {LOGGER.error("Zip file is too many.");break;}}}} catch (IOException e) {LOGGER.error("IOException error.", e);} finally {closeProcess(inputFile, fileOutputStream, inputStream, zipFile);}}private static String checkFileName(String entryName, String intendedDir) throws IOException {File file = new File(intendedDir, entryName);String canonicalPath1 = file.getCanonicalPath();File intendedFile = new File(intendedDir);String canonicalPath2 = intendedFile.getCanonicalPath();if (canonicalPath1.startsWith(canonicalPath2)) {return canonicalPath1;} else {throw new IllegalStateException();}}private static void closeProcess(String inputFile, FileOutputStream fileOutputStream, InputStream inputStream,ZipFile zipFile) {try {if (zipFile != null) {zipFile.close();}if (fileOutputStream != null) {fileOutputStream.close();}if (inputFile != null) {inputStream.close();}} catch (IOException e) {LOGGER.error("Close error.", e);}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/495672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汇编语言 test 和 cmp 区别

From:https://blog.csdn.net/zz709196484/article/details/77755243 看过破解教程,都知道 test,cmp 是比较关键,可是我一直不清楚它们究竟是怎么比较的,最后下决心找了很多资料,和大家一起把它们弄清楚. 首…

斯坦福大学、DARPA与硅谷公司共同分析前沿科技发展趋势

来源:科技日报 作者:张梦然日前,斯坦福大学研究团队、美国国防部高级研究计划局(DARPA)以及硅谷创投公司和米资本的专家们,共同研讨了技术将如何重塑行业和社会等问题。他们分析了现今全球前沿科技的未来…

利用cookie的跨域单点登录的简单实现

Configuration:1. Web.Config在两个站点的配置配置文件machine节点上相同的validationKey, decryptionKey and validation的值&#xff0c;如<machineKey validationKey"282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760ADF21B580D8587DB675FA02F791…

小甲鱼 OllyDbg 教程系列 (一) :二进制破解科普系列之 ReverseMe

小甲鱼 视频教程&#xff08; 4、5 集 &#xff09;&#xff1a;https://www.bilibili.com/video/av6889190?p4 实验程序 reverseME.exe 下载地址&#xff1a;https://pan.baidu.com/s/18NDV3rQ_yV_qzUrNRYmqjA 提取码&#xff1a;e91j http://www.360doc.com/content/1…

java将字符串生成图片

java将字符串保存为图片 因为最近接触到的需求是要将指定的字符串内容保存为图片&#xff0c;我知道肯定要用awt相关的东西&#xff0c;但是以前没有接触过awt&#xff0c;所以只能去网上学习相关的东西和找有关的资料&#xff0c;好在最后可以解决&#xff0c;直接上代码&…

开源的关系型数据持久化组件

Apache Empire-db 是一个开源的关系型数据持久化组件&#xff0c;能够实现数据库无关的动态查询定义&#xff0c;简便的数据读取和更新。与其它持久化组件相比 如&#xff1a;Hibernate、TopLink、iBATIS或JPA实现&#xff0c;Empire-db更注重编译期类型安全&#xff0c;减少冗…

AI 综述专栏 | 超长综述让你走近深度人脸识别

来源&#xff1a;人工智能前沿讲习班 作者&#xff1a; 葛政相信做机器学习或深度学习的同学们回家总会有这样一个烦恼&#xff1a;亲朋好友询问你从事什么工作的时候&#xff0c;如何通俗地解释能避免尴尬&#xff1f;我尝试过很多名词来形容自己的工作&#xff1a;机器学习&…

祝各位网友儿童节快乐!

儿童节就要到了&#xff0c;借此机会&#xff0c;给自己的童心放个假吧。 如果你不知道如何度过&#xff0c;可以参考以下建议&#xff1a; 建议一&#xff1a;在自己的房间开心的裸爬&#xff1b; 建议二&#xff1a;穿着鞋在自己的床上狂蹦&#xff1b; 建议三&#xff1a;披…

机器人也能拥有人类情感:“情感计算”让机器人学会“读心术”

来源&#xff1a;《情感计算与情感机器人系统》作者&#xff1a;吴敏&#xff0c;刘振焘&#xff0c;陈略峰著随着机器人进入日常生活中的各个方面&#xff0c;人们对其提出了更高的要求&#xff0c;希望它们具有感知人类情感、意图的能力&#xff0c;这类机器人称为情感机器人…

C 语言 函数调用栈

From&#xff1a;https://www.cnblogs.com/clover-toeic/p/3755401.html https://www.cnblogs.com/clover-toeic/p/3756668.html 程序的执行过程可看作连续的函数调用。当一个函数执行完毕时&#xff0c;程序要回到调用指令的下一条指令(紧接call指令)处继续执行。函数调用过…

SQL脚本:监控当前重做日志文件使用情况

这个脚本可以用来分析当前重做日志文件(redo logfile)已被用到了什么位置(position)、还剩余多少空间和已使用的百分比: set linesize 200 pagesize 1400; select le.leseq "Current log sequence No",100 * cp.cpodr_bno / le.lesiz "Percent Full",(cpod…

Apache HttpClient 详解

1、简介 HttpClient 是 Apache Jakarta Common 下的子项目&#xff0c;用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包&#xff0c;并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中&#xff0c;比如 Apache Jakarta 上很著名…

寒武纪创始人兼CEO陈天石博士的公开信

来源&#xff1a;来源&#xff1a;寒武纪科技摘要&#xff1a;2018年5月3日&#xff0c;寒武纪已在中国上海发布了首款云端智能芯片MLU100及相应的板卡产品。作为寒武纪的创始人和CEO&#xff0c;我非常自豪地与大家分享一个消息&#xff1a;2018年5月3日&#xff0c;寒武纪已在…

压栈, 跳转,执行,返回:从汇编看函数调用

From&#xff1a;https://www.jianshu.com/p/594357dff57e C函数调用过程原理及函数栈帧分析&#xff1a;https://blog.csdn.net/zsy2020314/article/details/9429707 从本篇开始&#xff0c;我们讨论一些高级语言中的基础设施&#xff1a;堆栈&#xff0c;函数调用&#xff0…

IBM AIX 5.3 系统管理 -- 系统启动过程详解

一. 启动过程 启动过程包含下面的一些步骤&#xff1a; 1.1启动一个系统的初始步骤是上电自检&#xff08;Power On Self Test&#xff0c;POST&#xff09;。其目的是验证基本硬件是否处于正常的工作状态。同时初始化内存、键盘、通信&#xff0c;以及音频设备。您可以看到在屏…

Collectors.toMap应用

Collectors.toMap介绍 在真实的业务场景中有很多集合转map的操作&#xff0c;例如 Data public class House {private Integer id; //idprivate Integer ownerid; //家主编号private String housename; //家庭名称private String address; //家庭地址}模拟数据 …

作弊阴影罩棋盘,人工智能咋避嫌?

来源&#xff1a;奇怪的科学家为什么要写这样一句话&#xff0c;就是为了避免剧情和现实生活中发生的事情万一差不多&#xff0c;会侵犯到别人的隐私&#xff0c;发生侵权&#xff0c;给双方带来不必要的麻烦。这位名为刘超的棋手把手机插在上衣兜里&#xff0c;摄像头正对棋盘…

小甲鱼视频教程系列

在学习 Ollydbg 时&#xff0c;发现小甲鱼讲的 Ollydbg 教程还可以&#xff0c;就把小甲鱼的视频整理了下。 小甲鱼主页&#xff1a;https://ilovefishc.com 小甲鱼论坛&#xff1a;https://fishc.com.cn 《零基础入门C语言》&#xff1a;https://www.bilibili.com/video/av277…

ubuntu server版本安装指南(1)

ubuntu是基于GNU/Linux 的操作系统&#xff0c;本身是在同样GNU/Linux 架构下的Debian的基础上的一个版本。由于它在桌面环境上的易用性和精细度是许多人认识了他。ubuntu的强大在一定程度上体现在apt包管理系 统。安装软件不必像以前那样幸苦找到下载地址。编译安装。还要非常…