java 文件压缩及解压缩
2016-07-08 18:07
645 查看
java操作windows命令(Rar.exe)执行文件压缩
// String srcPath = "D:\\test";// 被压缩文件夹 String srcPath = "D:\\test.txt";// 被压缩文件 String destPath = "D:\\test.rar";// 压缩后文件 String rarexePath = "C:\\Program Files\\WinRAR\\Rar.exe"; // 电脑系统中WinRAR安装路径 未安装出错 String[] rarcmd = { rarexePath, "a", destPath, srcPath }; Runtime rt = Runtime.getRuntime(); rt.exec(rarcmd);
java操作windows命令(UnRAR.exe)执行文件解压缩
String srcPath = "D:\\test.rar";// 压缩文件 String destPath = "D:\\";// 解压缩后路径 String unrarexePath = "C:\\Program Files\\WinRAR\\UnRAR.exe"; // 电脑系统中WinRAR安装路径 未安装出错 String[] unrarcmd = { unrarexePath, "x", srcPath, destPath }; Runtime rt = Runtime.getRuntime(); rt.exec(unrarcmd);
java操作第三方jar包执行rar解压缩
// 引用java-unrar-0.3.jar和commons-logging-1.1.3.jar String rarFile = "d:/test.rar"; String destDir = "d:/"; // 保证文件夹路径最后是"/"或者"\" char lastChar = destDir.charAt(destDir.length() - 1); if (lastChar != '/' && lastChar != '\\') { destDir += File.separator; } // 根据类型,进行相应的解压缩 String type = rarFile.substring(rarFile.lastIndexOf(".") + 1); if ("rar".equals(type)) { // 压缩文件 Archive a = new Archive(new File(rarFile)); FileOutputStream fos = null; FileHeader fh = a.nextFileHeader(); while (fh != null) { if (!fh.isDirectory()) { // 1 根据不同的操作系统拿到相应的 destDirName 和 destFileName String compressFileName = fh.getFileNameString().trim(); String destFileName = ""; String destDirName = ""; if (File.separator.equals("/")) { // 非windows系统 destFileName = destDir + compressFileName.replaceAll("\\\\", "/"); destDirName = destFileName.substring(0, destFileName.lastIndexOf("/")); } else { // windows系统 destFileName = destDir + compressFileName.replaceAll("/", "\\\\"); destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\")); } // 2 创建文件夹 File dir = new File(destDirName); if (!dir.exists() || !dir.isDirectory()) { dir.mkdirs(); } // 3 解压缩文件 fos = new FileOutputStream(new File(destFileName)); a.extractFile(fh, fos); fos.close(); fos = null; } fh = a.nextFileHeader(); // 关闭流 } a.close(); a = null; } else { throw new Exception("不是rar格式的压缩包!"); }
相关文章推荐
- ModleAttribute相关文章(SpringMVC)
- 2016.6.20 eclipse中maven的配置
- 深入理解Java的接口和抽象类
- 【SpringMVC学习11】SpringMVC中的拦截器
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- 2016.6.20 eclipse中的jsp文件的字体大小在哪里修改
- 2016.6.20 eclipse安装完毕后打开失败,显示a JDK or a JRE must be avaliable in order to run Eclispe
- HW机试字符串压缩java(1)
- java常见机试题目(1)
- socket(Channel)协议编码
- java术语(PO/POJO/VO/BO/DAO/DTO)
- 以后坚持用java
- java序列化和反序列化
- Struts2常用标签总结
- 【SpringMVC学习09】SpringMVC与前台的json数据交互
- spring分析——
- Spring 注解详解
- [转]eclipse借助hibernate tool从数据库逆向生成Hibernate实体类
- LSC回文字符串
- Struts2 Action跳转到另一个Action