使用7z完成文件压缩与解压缩
来源:互联网 发布:网站特效可以什么源码 编辑:程序博客网 时间:2024/05/22 19:49
/** * <p> * 使用7z进行压缩解压的工具类 * </p> * <p><b>Copyright: All Rights Reserved (c) 2015-2016</b></p> * <p><b>Company:Travelsky</b></p> * @ClassName: Zip7ZUtil * @date 2015-5-14 下午2:06:25 * */public class Zip7zUtil { private static String exePath=SysPropUtil.getValue("7z.path"); private static Logger logger = LogManager.getLogger(Zip7zUtil.class); public static void zip(String srcRootPath,String zipFile) { String cmd = "7z a -tzip " + zipFile + " " + srcRootPath; try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 同步解压 * @param zipFilePath * @param unZipPath */ /*public static boolean syncUnzip(String zipFilePath,String unZipPath) { logger.info("unzip "+zipFilePath+" to "+unZipPath); boolean result; String cmd = exePath+"\\7z x -o" + unZipPath + " -y " + zipFilePath; logger.info("do syncUnzip exec : "+cmd); try { Process p = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while (true) { try { line = br.readLine(); if (line == null) { result = true; break; }else{ System.out.println(line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error(" syncUnzip exception : "+e); result = false; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error(" syncUnzip exception : "+e); result = false; } return result; }*/ public static boolean syncUnzip(String zipFilePath,String unZipPath) { logger.info("unzip "+zipFilePath+" to "+unZipPath); boolean result; String cmd = exePath+"\\7z x -o" + unZipPath + " -y " + zipFilePath; logger.info("do syncUnzip exec : "+cmd); try { File zipFile=new File(zipFilePath); long zipSize=zipFile.length(); long unzipSize=0; Process p = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while (true) { try { line = br.readLine(); logger.info("7z info : " + line); if (line != null) { String[] arr=line.split("="); if (arr.length==2) { if (arr[0].trim().equals("Physical Size")) { zipSize=Long.parseLong(arr[1].trim()); } } String[] arr2=line.split(":"); if (arr2[0].trim().equals("Compressed")) { unzipSize=Long.parseLong(arr2[1].trim()); } }else{ logger.info("unzipSize = "+unzipSize+" , zipSize = "+zipSize); if (unzipSize==zipSize) { result = true; }else{ result = false; logger.error(" syncUnzip "+zipFilePath+" to "+unZipPath+" failed ! "); } break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error(" syncUnzip exception : "+e); result = false; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error(" syncUnzip exception : "+e); result = false; } return result; } /** * 异步解压,会通过listener回调结果 * @param zipFilePath * @param unZipPath * @param listener */ public static void asyncUnzip(String zipFilePath,String unZipPath,final ICallbackListener listener) { if (zipFilePath == null || zipFilePath.equals("") || unZipPath == null || unZipPath.equals("")) { listener.callback("fail"); return; } String cmd = exePath+"\\7z x -o" + unZipPath + " -y -tzip " + zipFilePath; try { final Process p = Runtime.getRuntime().exec(cmd); if (listener != null) { new Thread() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while (true) { try { line = br.readLine().trim(); if (line == null) { listener.callback("OK"); break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); listener.callback("fail"); } } } }; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); listener.callback("fail"); } }}
0 0
- 使用7z完成文件压缩与解压缩
- C#:SevenZipSharp使用7z.dll来进行压缩与解压缩
- Java使用sevenzipjbind解压缩7z压缩包
- 文件压缩与解压缩
- 文件压缩与解压缩
- 文件压缩与解压缩
- unbuntu 解压缩.7z文件
- 命令行压缩解压缩一 7z
- 7、压缩与解压缩
- java 文件压缩与解压缩
- 文件的压缩与解压缩
- C#压缩与解压缩文件
- zip文件 压缩与解压缩
- Linux 压缩与解压缩文件
- Ubuntu 文件压缩与解压缩
- Python----压缩与解压缩文件
- linux 文件压缩与解压缩
- C# 文件压缩与解压缩
- 正则表达式RegExp
- 欢迎使用CSDN-markdown编辑器
- 《java并发编程实战》读书笔记
- Mybatis中SqlMapper配置的扩展与应用(3)
- 定义一个游乐园门票的类
- 使用7z完成文件压缩与解压缩
- Android源码编译之Nexus5真机编译
- [Wondgirl]ECMAScript6(ES6)(二)变量let、var和常量const的定义和区别
- 总结一下刷过的题
- UML整理-未完待续
- Linux编译时找不到命令解决方法
- ViewFlipper实践
- Android中图像和图像处理
- android使用retrofit框架示例