Java 壓縮包解壓實(shí)現(xiàn)代碼
在Java開發(fā)中,處理壓縮文件(如ZIP、RAR等)是一項(xiàng)常見的任務(wù),特別是在需要處理大量數(shù)據(jù)、備份或分發(fā)應(yīng)用程序時(shí)。Java標(biāo)準(zhǔn)庫(kù)(Java SE)提供了對(duì)ZIP格式的原生支持,通過java.util.zip
包中的類來實(shí)現(xiàn)壓縮和解壓功能。本文將重點(diǎn)介紹如何使用Java來解壓ZIP或RAR壓縮包。
一、解壓壓縮包
解壓壓縮包,借助ZipInputStream類,可以讀取到壓縮包中的每一個(gè)文件,然后根據(jù)讀取到的文件屬性,寫入到相應(yīng)路徑下即可。對(duì)于解壓壓縮包中是文件樹的結(jié)構(gòu),每讀取到一個(gè)文件后,如果是多層路徑下的文件,需要先創(chuàng)建父目錄,再寫入文件流。
1.zip解壓代碼實(shí)現(xiàn):
// 解壓zip格式 public static void unzip(String path){ // 根據(jù)原始路徑(字符串),創(chuàng)建源文件(File對(duì)象) File sourceFile = new File(path); // 根目錄 String sourceFileName = sourceFile.getName(); File rootDir = new File(sourceFile.getParent()+"\\"+sourceFileName.substring(0,sourceFileName.lastIndexOf("."))); // 判斷根目錄是否已經(jīng)存在 if(rootDir.exists()){ // 如果存在,則刪除 // rootDir.delete(); // 僅能刪除空目錄 // 使用commons-io包提供的FileUtils工具類進(jìn)行刪除 try { FileUtils.deleteDirectory(rootDir); } catch (IOException e) { e.printStackTrace(); } } // 創(chuàng)建根目錄 rootDir.mkdirs(); // System.out.println(rootDir); // ZipInputStream:用于進(jìn)行zip格式的壓縮文件輸入流 try (ZipInputStream in = new ZipInputStream(new FileInputStream(sourceFile))) { // 遍歷壓縮包中的每個(gè)子目錄或子文件(ZipEntry類型的對(duì)象) ZipEntry zipEntry = null; while((zipEntry = in.getNextEntry()) != null){ // System.out.println(zipEntry.getName()); // 創(chuàng)建子目錄或子文件(File對(duì)象) // F:\Software\IDEA\Projects\test\easyftp-server-1.7.0.10-cn File file = new File(rootDir.getPath()+"\\"+zipEntry.getName()); if(zipEntry.isDirectory()){ // 物理磁盤創(chuàng)建子目錄 file.mkdirs(); }else{ // 物理磁盤創(chuàng)建子文件 file.createNewFile(); // 讀取當(dāng)前壓縮包中的子文件,并通過輸出流out寫入新子文件中 try(FileOutputStream out = new FileOutputStream(file)) { byte[] buff = new byte[1024]; int len = -1; while((len = in.read(buff)) != -1){ out.write(buff,0,len); } } } } } catch (IOException e) { e.printStackTrace(); } }
2.rar解壓代碼實(shí)現(xiàn):
// 解壓縮rar格式 private static void unrar(String path) { // 1.創(chuàng)建解壓縮的根目錄 File rarFile = new File(path); // File rootDir = new File(rarFile.getParent()+"\\"+rarFile.getName().substring(0,rarFile.getName().lastIndexOf("."))); String rarFileName = rarFile.getName(); File rootDir = new File(rarFile.getParent()+"\\"+rarFileName.substring(0,rarFileName.lastIndexOf("."))); if(rootDir.exists()){ try { FileUtils.deleteDirectory(rootDir); } catch (IOException e) { e.printStackTrace(); } } rootDir.mkdirs(); // 創(chuàng)建Archive對(duì)象,用于讀取rar壓縮文件格式 try(Archive archive = new Archive(new FileInputStream(path))) { // 讀取壓縮文件中的所有子目錄或子文件(FileHeader對(duì)象) List<FileHeader> fileHeaderList = archive.getFileHeaders(); // 按照子目錄(子文件)名來排序 fileHeaderList.sort(new Comparator<FileHeader>() { @Override public int compare(FileHeader o1, FileHeader o2) { return o1.getFileName().compareTo(o2.getFileName()); } }); // 遍歷子目錄和子文件 for (FileHeader fd:fileHeaderList) { System.out.println(fd.getFileName()); File f = new File(rootDir.getPath()+"\\"+fd.getFileName()); if(fd.isDirectory()){ // 創(chuàng)建新子目錄 f.mkdirs(); }else{ // 創(chuàng)建新子文件 f.createNewFile(); // 獲取壓縮包中的子文件輸出流 InputStream in = archive.getInputStream(fd); // 復(fù)制文件輸入流至新子文件 FileUtils.copyInputStreamToFile(in,f); } } } catch (RarException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
3.調(diào)用解壓方法:
最后,在main
方法或任何其他適當(dāng)?shù)奈恢谜{(diào)用unzip
方法,傳入ZIP或RAR文件的路徑和解壓到的目標(biāo)。
import com.github.junrar.Archive; import com.github.junrar.exception.RarException; import com.github.junrar.rarfile.FileHeader; import org.apache.commons.io.FileUtils; import java.io.*; import java.util.Comparator; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public static void main(String[] args) { // String path = "F:\\Software\\IDEA\\Projects\\test\\easyftp-server-1.7.0.10-cn.zip"; String path = "F:\\Software\\IDEA\\Projects\\test\\實(shí)驗(yàn)案例.rar"; if(path.endsWith(".zip")) { unzip(path); } else if(path.endsWith(".rar")){ unrar(path); } }
二、注意事項(xiàng)
- 文件路徑處理:在解壓時(shí),注意正確處理ZIP文件中的文件路徑,以避免安全風(fēng)險(xiǎn)(如路徑遍歷攻擊)。
- 異常處理:在解壓過程中,可能會(huì)遇到文件讀取錯(cuò)誤、寫入錯(cuò)誤或權(quán)限問題,應(yīng)妥善處理這些異常。
- 性能優(yōu)化:對(duì)于大型ZIP文件,考慮使用更高效的IO操作和流控制來優(yōu)化解壓速度。
- 壓縮用到的JAR包:需要使用第三方庫(kù),如commons-io包。
三、總結(jié)
- 在解壓縮文件過程中,主要是對(duì)流的讀取操作,注意進(jìn)行異常處理,以及關(guān)閉流。
- 解壓縮文件時(shí),注意空文件夾的處理。
到此這篇關(guān)于Java 壓縮包解壓實(shí)現(xiàn)過程的文章就介紹到這了,更多相關(guān)Java 壓縮包解壓內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java StringBuffer類與StringBuilder類用法實(shí)例小結(jié)
這篇文章主要介紹了Java StringBuffer類與StringBuilder類用法,結(jié)合實(shí)例形式總結(jié)分析了Java StringBuffer類與StringBuilder類的功能、原理及添加、刪除、替換、截取等操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03request.getParameter()方法的簡(jiǎn)單理解與運(yùn)用方式
在JavaWeb開發(fā)中,request對(duì)象扮演著至關(guān)重要的角色,它是HTTP請(qǐng)求的封裝,request.getParameter()用于獲取客戶端通過GET或POST方式發(fā)送的參數(shù),與之相對(duì),request.setAttribute()用于在服務(wù)器端設(shè)置屬性,這些屬性只在一次請(qǐng)求中有效2024-10-10mybatis plus自動(dòng)生成器解析(及遇到的坑)
這篇文章主要介紹了mybatis-plus自動(dòng)生成器及遇到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03