Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自助餐:解析與解壓 Zip 文件

編輯:關於 Android 程式設計

本文把解析 Zip 的過程拆成幾個小步驟:獲取所有節點游標、遍歷每個節點、處理單個節點、梳理解壓思路,最後給出可直接複用的完整代碼。

獲取文件的所有節點游標

Enumeration entries = zipFile.entries(); // zipFile 為 java.util.zip.ZipFile

遍歷每個節點

while (entries.hasMoreElements()) {
    // 在這裡逐一處理每個節點
}

處理單個節點

ZipEntry ze = (ZipEntry) entries.nextElement();
boolean b = ze.isDirectory();          // 判斷是否為目錄
String name = ze.getName();            // 獲取文件/文件夾名稱
name = new String(name.getBytes("8859_1"), "gb2312"); // 文件名轉碼,避免中文亂碼
InputStream is = new BufferedInputStream(zipFile.getInputStream(ze)); // 從該節點獲取輸入流(ze 必須不是目錄)
zipFile.close();                       // 用完關閉 zip 文件

解壓思路(把某個 ZipFile 解壓到指定目標路徑)

先獲取並遍歷每個節點,再逐個處理:

若節點是目錄:拼接出目標文件夾路徑並轉碼,調用 mkdir() 創建。

若節點是文件:用 / 分割節點名(除最後一段外均為路徑),若段數大於 1 則逐級拼接並用 mkdirs() 創建文件夾;最後一段轉碼拼到路徑上,用 createNewFile() 創建目標文件;隨後根據目標文件建輸出流、根據節點建輸入流,把數據從輸入流複製到輸出流即可。

直接可用的代碼

/** 解壓 zip 文件到指定路徑 */
public static void unpackZipFile(ZipFile zipFile, String folderPath) {
    Enumeration entries = zipFile.entries();
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    // 遍歷節點
    while (entries.hasMoreElements()) {
        ze = (ZipEntry) entries.nextElement();
        // 該節點是目錄
        if (ze.isDirectory()) {
            String dirstr = folderPath + ze.getName();
            try {
                dirstr = new String(dirstr.getBytes("8859_1"), "gb2312");
                File f = new File(dirstr);
                f.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
            }
            continue;
        }
        // 該節點是文件
        try {
            OutputStream os = new BufferedOutputStream(
                    new FileOutputStream(getRealFileName(folderPath, ze.getName())));
            InputStream is = new BufferedInputStream(zipFile.getInputStream(ze));
            int readLen = 0;
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                os.write(buf, 0, readLen);
            }
            is.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 關閉文件
    try {
        zipFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/** 根據相對路徑獲取並創建絕對路徑 */
private static File getRealFileName(String baseDir, String absFileName) {
    String[] dirs = absFileName.split("/");
    File ret = new File(baseDir);
    String substr = null;
    if (dirs.length > 1) {
        // 遍歷除最後一段外的各級路徑
        for (int i = 0; i < dirs.length - 1; i++) {
            substr = dirs[i];
            try {
                substr = new String(substr.getBytes("8859_1"), "gb2312");
            } catch (Exception e) {
                e.printStackTrace();
            }
            ret = new File(ret, substr);
        }
        if (!ret.exists()) {
            ret.mkdirs();
        }
    }
    // 處理最後一段
    substr = dirs[dirs.length - 1];
    try {
        substr = new String(substr.getBytes("8859_1"), "gb2312");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ret = new File(ret, substr);
    if (!ret.exists()) {
        try {
            ret.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return ret;
}

這段代碼的關鍵在於:目錄節點直接 mkdir 創建;文件節點則先通過 getRealFileName 逐級 mkdirs 建好父目錄、createNewFile 建好文件,再用輸入輸出流完成內容拷貝。整個過程還特別處理了 8859_1 到 gb2312 的轉碼,避免中文路徑亂碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved