java 通过7z 进行压缩加密
来源:互联网 发布:将乐教育网络办公系统 编辑:程序博客网 时间:2024/05/17 03:39
首先我们先来看一下效果图
要完成7z的压缩加密,我们首先先要下载7z工具,这个可以到官网自行下载,接下来我们只需将7z.dll,7z.exe,7z.sfx将这几个文件放在我们的工程目录,然后去引用即可。
关键代码示例:
private boolean zipByExternal(String zipFileName, String[] zipContentFileNameList, String compressionType, String password) throws Exception { Runtime runtime = null; Process process = null; String command = ""; StringBuffer sbFilePath = null; StringBuffer sbZipResult = null; String zipLibPath = ""; String result = ""; InputStream inputStream = null; int value = 0; boolean isSuccess = false; try{ if (!isContainData(zipFileName)){ throw new Exception(ERROR_MSG_NOT_OUTPUT_PATH); } if (zipContentFileNameList != null && zipContentFileNameList.length > 0){ for (int i=0; i<zipContentFileNameList.length; i++){ if (!FileUtil.fileIsExist(zipContentFileNameList[i])){ throw new Exception(ERROR_MSG_COMPRESSION_NO_ATTACHMENT_FILE); } } }else{ throw new Exception(ERROR_MSG_COMPRESSION_NO_ATTACHMENT_FILE); } if (!isContainData(compressionType)){ throw new Exception(ERROR_MSG_NOT_OUTPUT_PATH); } if (!isContainData(compressionType)){ throw new Exception(ERROR_MSG_COMPRESSION_INVALID_TYPE); } zipLibPath = Sevent_HOME_PATH + schoolId + File.separator + Sevent_COMMON_PATH + Sevent_ZIP_PATH; runtime = Runtime.getRuntime(); sbFilePath = new StringBuffer(100); sbZipResult = new StringBuffer(); for (int i=0; i<zipContentFileNameList.length; i++){ sbFilePath.append(zipContentFileNameList[i] + " "); } if (COMPRESSION_TYPE_EXE.equals(compressionType)){ if (zipFileName.toLowerCase().indexOf(COMPRESSION_TYPE_EXE.toLowerCase()) < 0){ throw new Exception(ERROR_MSG_COMPRESSION_TYPE_NOT_MATCH); } if (isContainData(password)){ //command = zipLibPath + Sevent_ZIP_EXE +" a -p" + password + " -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath; command = root_path + File.separator + Sevent_ZIP_EXE +" a -p" + password + " -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath; }else{ //command = zipLibPath + Sevent_ZIP_EXE +" a -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath; command = root_path + File.separator + Sevent_ZIP_EXE +" a -mhe -sfx7z.sfx " + zipFileName + " " + sbFilePath; } }else if (COMPRESSION_TYPE_ZIP.equals(compressionType)){ if (zipFileName.toLowerCase().indexOf(COMPRESSION_TYPE_ZIP.toLowerCase()) < 0){ throw new Exception(ERROR_MSG_COMPRESSION_TYPE_NOT_MATCH); } if (isContainData(password)){ //command = zipLibPath + Sevent_ZIP_EXE +" a -mem=aes -p" + password + " -tzip " + zipFileName + " " + sbFilePath; command = root_path + File.separator + Sevent_ZIP_EXE +" a -mem=aes -p" + password + " -tzip " + zipFileName + " " + sbFilePath; }else{ //command = zipLibPath + Sevent_ZIP_EXE +" a -mem=aes -tzip " + zipFileName + " " + sbFilePath; command = root_path + File.separator + Sevent_ZIP_EXE +" a -mem=aes -tzip " + zipFileName + " " + sbFilePath; } } process = runtime.exec(command); inputStream = process.getInputStream(); while ((value = inputStream.read()) != -1) { sbZipResult.append((char) value); } result = new String(sbZipResult.toString().getBytes("ISO-8859-1"), "GBK"); if (result.toLowerCase().indexOf(SUCCESS_KEYWORD.toLowerCase()) >= 0){ isSuccess = true; } inputStream.close(); inputStream = null; }catch (Exception e){ e.printStackTrace(); throw new Exception(ERROR_MSG_UNKNOWN_ERROR); }finally{ try{ if (inputStream != null){ inputStream.close(); inputStream = null; } }catch (Exception e){} return isSuccess; } }
由示例代码可知,我们这里会创建一个线程来执行我们的压缩动作,
由于7z支持exe压缩和zip压缩,所以这里我们做了类型判断,接下来只需要执行7z自带的命令即可进行打包压缩了,由于这里是调用shell命令,所以我们只能通过执行runtime.exec(command);来运行。
接着只需将运行好的文件以字节的形式输出即可
资源下载
0 0
- java 通过7z 进行压缩加密
- Java解压.7z格式压缩包
- 7z 命令行 进行单文件极限压缩与解压
- 【语言-C++】文件夹或文件加密 - 方案一使用7z-zip工具压缩加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- java压缩文件并对压缩包进行加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 7Z压缩
- 7Z压缩
- 对MemoryStream进行压缩、加密
- Java使用sevenzipjbind解压缩7z压缩包
- 7z压缩和解压的方法--java
- 压缩、解压缩.zip,.rar,.7z格式java工具类
- 如何高效学习大数据?
- grails学习之自定义标签
- java 施行cmd命令及mongodb脚本
- redis
- 1041. Be Unique (20)
- java 通过7z 进行压缩加密
- free -m 查看内存简介
- jogl入门
- 【Java进阶】——初识数据库连接池
- Solr教程
- 函数设计
- Quartz中时间表达式的设置-----corn表达式
- Redis 介绍
- free -m 查看内存简介