Java實現(xiàn)解壓zip和rar包的示例代碼
zip的解壓提供了一種方法,
rar的解壓提供了兩種方法,第一種方法是調(diào)用命令調(diào)用主機安裝的解壓縮工具,
第二種方法,需要注意一下,需要導一個包
<dependency> <groupId>com.github.junrar</groupId> <artifactId>junrar</artifactId> <version>4.0.0</version> </dependency>
并且第二種方法,截至2023年,只支持rar4以下版本的解壓,rar5的版本不支持,以后會不會有更新,就不知道了。
try{ File file = new File(filePath); if(file.getName().endsWith(".zip")){ ZipInputStream zis = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK")); byte[] buffer = new byte[1024]; ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { String entryName = ze.getName(); File extractedFile = new File(ceShiFilePath + entryName); if (ze.isDirectory()) { extractedFile.mkdirs(); } else{ FileOutputStream fos = new FileOutputStream(extractedFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); fileList.add(extractedFile); } } zis.closeEntry(); }else if(file.getName().endsWith(".rar")){ String cmd = "D:\\tools\\zip\\UnRAR.exe"; //cmd命令 String unrarCmd = cmd + " x -r -p- -o+ " + file.getAbsolutePath() + " " + ceShiFilePath; Runtime rt = Runtime.getRuntime(); //調(diào)用程序UnRAR解壓程序(Windows) Process pre = rt.exec(unrarCmd); //防止文件亂碼 InputStreamReader isr = new InputStreamReader(pre.getInputStream(),"GBK"); BufferedReader bf = new BufferedReader(isr); String line = null; while ((line = bf.readLine()) != null) { line = line.trim(); if ("".equals(line)) { continue; } } bf.close(); pre.destroy(); }else if(file.getName().endsWith(".rar")){ Archive archive = new Archive(new FileInputStream(file)); List<FileHeader> fileHeaders = archive.getFileHeaders(); for (FileHeader fileHeader : fileHeaders) { if (fileHeader.isDirectory()) { File dir = new File(ceShiFilePath + fileHeader.getFileNameString()); if (!dir.exists()){ dir.mkdirs(); } } else { String fileName= fileHeader.getFileNameW().trim(); File subFile = new File(ceShiFilePath + fileName); if (!subFile.exists()) { if (!subFile.getParentFile().exists()) { subFile.getParentFile().mkdirs(); } subFile.createNewFile(); } FileOutputStream os = new FileOutputStream(subFile); archive.extractFile(fileHeader, os); os.close(); fileList.add(subFile); } } archive.close(); } for(File file111 : fileList){ System.out.println(file111.getAbsolutePath()); } }catch (Exception ex){ ex.printStackTrace(); log.error(ex.getMessage()); }
都是親測可用的。
到此這篇關于Java實現(xiàn)解壓zip和rar包的示例代碼的文章就介紹到這了,更多相關Java解壓zip和rar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java?Controller實現(xiàn)參數(shù)驗證與統(tǒng)一異常處理流程詳細講解
Controller是Spring接受并處理網(wǎng)頁請求的組件,是整個應用的入口,因此學會Controller的常用注解對理解一個應用是重中之重。SpringBoot的Controller中經(jīng)常會用到注解@Controller、@RestController、@RequestMapping、@RequestBody等2023-01-01Spring Boot+Shiro實現(xiàn)一個Http請求的Basic認證
本文向向大家仔細的介紹了如何使用Shiro實現(xiàn)一個Http請求的Basic認證,有此需求的朋友可以參考下本文2021-06-06SpringBoot封裝響應數(shù)據(jù)實現(xiàn)過程詳解
這篇文章主要介紹了SpringBoot封裝響應數(shù)據(jù)實現(xiàn)過程,SpringBoot響應數(shù)據(jù)封裝是指在SpringBoot應用程序中,將返回的數(shù)據(jù)進行封裝,以便于前端頁面或其他客戶端使用,感興趣想要詳細了解可以參考下文2023-05-05springboot2+es7使用RestHighLevelClient的示例代碼
本文主要介紹了springboot2+es7使用RestHighLevelClient的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07