java 解压rar文件
程序员文章站
2024-03-22 19:38:48
...
https://github.com/ShenZhenSogaaTech/sogaa-web/blob/b1ef0d8bd582bbf4e0e4fb7ad0e39657f2b307d0/src/main/java/com/sogaa/system/utils/ZipUtils.java
maven依赖关系
<!-- https://mvnrepository.com/artifact/com.github.junrar/junrar --> <dependency> <groupId>com.github.junrar</groupId> <artifactId>junrar</artifactId> <version>0.7</version> </dependency>
import com.github.junrar.Archive; import com.github.junrar.exception.RarException; import com.github.junrar.rarfile.FileHeader; import org.junit.Test; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.List; /** * Created by on 2017/2/28. */ public class RarTest { /** * @param rarFileName rar file name * @param outFilePath output file path * @return success Or Failed * @throws Exception * @author zhuss */ public static boolean unrar(String rarFileName, String outFilePath) throws Exception { boolean flag = false; try { Archive archive = new Archive(new File(rarFileName)); if (archive == null) { throw new FileNotFoundException(rarFileName + " NOT FOUND!"); } if (archive.isEncrypted()) { throw new Exception(rarFileName + " IS ENCRYPTED!"); } List<FileHeader> files = archive.getFileHeaders(); for (FileHeader fh : files) { if (fh.isEncrypted()) { throw new Exception(rarFileName + " IS ENCRYPTED!"); } String fileName = fh.getFileNameString(); if (fileName != null && fileName.trim().length() > 0) { String saveFileName = outFilePath + "\\" + fileName; File saveFile = new File(saveFileName); File parent = saveFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } if (!saveFile.exists()) { saveFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(saveFile); try { archive.extractFile(fh, fos); fos.flush(); fos.close(); } catch (RarException e) { if (e.getType().equals(RarException.RarExceptionType.notImplementedYet)) { } } finally { } } } flag = true; } catch (Exception e) { e.printStackTrace(); throw e; } return flag; } @Test public void unrar() throws Exception { RarTest.unrar("/install/putty.rar", "/test"); } }
上一篇: datawindow 技巧 ExcelSQLC#CC++
下一篇: JAVA面试6174问题。
推荐阅读
-
java 解压rar文件
-
java 利用TCP协议从客户端向服务器端发送文件.(网络编程)
-
文件下载中文文件名的处理 博客分类: java IEOpera浏览器.net
-
intelljIdea的ctrl+shift+N找不到文件 博客分类: Java_about
-
1.Java 压缩20M文件从30秒到1秒的优化过程
-
JAVA操作HDFS的文件系统
-
Java文件操作——简单文件搜索
-
Servlet中下载已上传的文件(已测试) 博客分类: Java JavaServletdownload
-
jar包中资源文件的读取 博客分类: java Jar
-
使用多线程往同一个文件写入数据的线程安全的例子(java实现)