欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java操作壓縮包解壓過程詳解

 更新時間:2024年10月03日 11:37:17   作者:Satan712  
這篇文章主要介紹了Java操作壓縮包解壓過程,項目開發(fā)中,總會遇到解壓縮文件的時候,比如用戶下載多個文件時,服務端可以將多個文件壓縮成一個文件,用戶上傳資料時,允許上傳壓縮文件,服務端進行解壓讀取每一個文件,使用場景是很多的,下面來詳細講解,需要的朋友可以參考下

在Java開發(fā)中,處理壓縮文件(如ZIP、RAR等)是一項常見的任務,特別是在需要處理大量數(shù)據(jù)、備份或分發(fā)應用程序時。Java標準庫(Java SE)提供了對ZIP格式的原生支持,通過java.util.zip包中的類來實現(xiàn)壓縮和解壓功能。本文將重點介紹如何使用Java來解壓ZIP或RAR壓縮包。

一、解壓壓縮包

解壓壓縮包,借助ZipInputStream類,可以讀取到壓縮包中的每一個文件,然后根據(jù)讀取到的文件屬性,寫入到相應路徑下即可。對于解壓壓縮包中是文件樹的結構,每讀取到一個文件后,如果是多層路徑下的文件,需要先創(chuàng)建父目錄,再寫入文件流。

1.zip解壓代碼實現(xiàn)

// 解壓zip格式
    public static void unzip(String path){
        // 根據(jù)原始路徑(字符串),創(chuàng)建源文件(File對象)
        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工具類進行刪除
            try {
                FileUtils.deleteDirectory(rootDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 創(chuàng)建根目錄
        rootDir.mkdirs();
        // System.out.println(rootDir);
        // ZipInputStream:用于進行zip格式的壓縮文件輸入流
        try (ZipInputStream in  = new ZipInputStream(new FileInputStream(sourceFile))) {
            // 遍歷壓縮包中的每個子目錄或子文件(ZipEntry類型的對象)
            ZipEntry zipEntry = null;
            while((zipEntry = in.getNextEntry()) != null){
                // System.out.println(zipEntry.getName());
                // 創(chuàng)建子目錄或子文件(File對象)
                // 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();
                    // 讀取當前壓縮包中的子文件,并通過輸出流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解壓代碼實現(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對象,用于讀取rar壓縮文件格式
        try(Archive archive = new Archive(new FileInputStream(path))) {
            // 讀取壓縮文件中的所有子目錄或子文件(FileHeader對象)
            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);
                    // 復制文件輸入流至新子文件
                    FileUtils.copyInputStreamToFile(in,f);
                }
            }
        } catch (RarException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.調(diào)用解壓方法

最后,在main方法或任何其他適當?shù)奈恢谜{(diào)用unzip方法,傳入ZIP或RAR文件的路徑和解壓到的目標。

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\\實驗案例.rar";
        if(path.endsWith(".zip")) {
            unzip(path);
        } else if(path.endsWith(".rar")){
            unrar(path);
        }
    }
 

二、注意事項

  • 文件路徑處理:在解壓時,注意正確處理ZIP文件中的文件路徑,以避免安全風險(如路徑遍歷攻擊)。
  • 異常處理:在解壓過程中,可能會遇到文件讀取錯誤、寫入錯誤或權限問題,應妥善處理這些異常。
  • 性能優(yōu)化:對于大型ZIP文件,考慮使用更高效的IO操作和流控制來優(yōu)化解壓速度。
  • 壓縮用到的JAR包:需要使用第三方庫,如commons-io包。

三、總結

  • 在解壓縮文件過程中,主要是對流的讀取操作,注意進行異常處理,以及關閉流。
  • 解壓縮文件時,注意空文件夾的處理。

到此這篇關于Java操作壓縮包解壓過程詳解的文章就介紹到這了,更多相關Java壓縮包解壓內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論