1. 程式人生 > 實用技巧 >Java解壓和壓縮帶密碼的zip檔案過程詳解

Java解壓和壓縮帶密碼的zip檔案過程詳解

https://www.jb51.net/article/164137.htm

前言

JDK自帶的ZIP操作介面(java.util.zip包,請參看文章末尾的部落格連結)並不支援密碼,甚至也不支援中文檔名。

為了解決ZIP壓縮檔案的密碼問題,在網上搜索良久,終於找到了winzipaes開源專案。

該專案在google code下託管,僅支援AES壓縮和解壓zip檔案(This library only supports Win-Zip's 256-Bit AES mode.)。網站上下載的檔案是原始碼,最新版本為winzipaes_src_20120416.zip,本示例就是在此基礎上編寫。

詳述

專案使用很簡單,利用原始碼自己匯出一個jar檔案,在專案中引用即可。

這裡有一個需要注意的問題,就是如果給定ZIP檔案沒有密碼,那麼就不能使用該專案解壓,如果壓縮檔案沒有密碼卻使用該專案解壓在這裡會報一個異常,所以使用中需要注意:加密ZIP檔案可以使用它解壓,沒有加密的就需要採取其它方式了。

此文就是採用修改後的winzipaes編寫,並記錄詳細修改步驟。

winzipaes專案依賴bcprov的jar包

Windows命令列:

 1   try {
 2 
 3    String cmd = "unzip -o -P" + passWord + nssDecomFilePath + "\\"
 4      + zipFileName;
 5 
 6    Runtime.getRuntime().exec(cmd);
7 } catch (Exception ex) { 8 return false; 9 }

示例

在研究該專案時寫了一個工具類,本來準備用在專案中,最後找到了更好的解決方案zip4j來代替,所以最終沒有采用。

  1 package com.ninemax.demo.zip.decrypt;
  2 import java.io.File;
  3 import java.io.IOException;
  4 import java.util.List;
  5 import java.util.zip.DataFormatException;
  6 import
org.apache.commons.io.FileUtils; 7 import de.idyl.winzipaes.AesZipFileDecrypter; 8 import de.idyl.winzipaes.AesZipFileEncrypter; 9 import de.idyl.winzipaes.impl.AESDecrypter; 10 import de.idyl.winzipaes.impl.AESDecrypterBC; 11 import de.idyl.winzipaes.impl.AESEncrypter; 12 import de.idyl.winzipaes.impl.AESEncrypterBC; 13 import de.idyl.winzipaes.impl.ExtZipEntry; 14 /** 15 * 壓縮指定檔案或目錄為ZIP格式壓縮檔案 16 * 支援中文(修改原始碼後) 17 * 支援密碼(僅支援256bit的AES加密解密) 18 * 依賴bcprov專案(bcprov-jdk16-140.jar) 19 * 20 * @author zyh 21 */ 22 public class DecryptionZipUtil { 23 /** 24 * 使用指定密碼將給定檔案或資料夾壓縮成指定的輸出ZIP檔案 25 * @param srcFile 需要壓縮的檔案或資料夾 26 * @param destPath 輸出路徑 27 * @param passwd 壓縮檔案使用的密碼 28 */ 29 public static void zip(String srcFile,String destPath,String passwd) { 30 AESEncrypter encrypter = new AESEncrypterBC(); 31 AesZipFileEncrypter zipFileEncrypter = null; 32 try { 33 zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter); 34 /** 35 * 此方法是修改原始碼後新增,用以支援中文檔名 36 */ 37 zipFileEncrypter.setEncoding("utf8"); 38 File sFile = new File(srcFile); 39 /** 40 * AesZipFileEncrypter提供了過載的新增Entry的方法,其中: 41 * add(File f, String passwd) 42 * 方法是將檔案直接新增進壓縮檔案 43 * 44 * add(File f, String pathForEntry, String passwd) 45 * 方法是按指定路徑將檔案新增進壓縮檔案 46 * pathForEntry - to be used for addition of the file (path within zip file) 47 */ 48 doZip(sFile, zipFileEncrypter, "", passwd); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } finally { 52 try { 53 zipFileEncrypter.close(); 54 } catch (IOException e) { 55 e.printStackTrace(); 56 } 57 } 58 } 59 60 /** 61 * 具體壓縮方法,將給定檔案新增進壓縮檔案中,並處理壓縮檔案中的路徑 62 * @param file 給定磁碟檔案(是檔案直接新增,是目錄遞迴呼叫新增) 63 * @param encrypter AesZipFileEncrypter例項,用於輸出加密ZIP檔案 64 * @param pathForEntry ZIP檔案中的路徑 65 * @param passwd 壓縮密碼 66 * @throws IOException 67 */ 68 private static void doZip(File file, AesZipFileEncrypter encrypter, 69 String pathForEntry, String passwd) throws IOException { 70 if (file.isFile()) { 71 pathForEntry += file.getName(); 72 encrypter.add(file, pathForEntry, passwd); 73 return; 74 } 75 pathForEntry += file.getName() + File.separator; 76 for(File subFile : file.listFiles()) { 77 doZip(subFile, encrypter, pathForEntry, passwd); 78 } 79 } 80 81 /** 82 * 使用給定密碼解壓指定壓縮檔案到指定目錄 83 * @param inFile 指定Zip檔案 84 * @param outDir 解壓目錄 85 * @param passwd 解壓密碼 86 */ 87 public static void unzip(String inFile, String outDir, String passwd) { 88 File outDirectory = new File(outDir); 89 if (!outDirectory.exists()) { 90 outDirectory.mkdir(); 91 } 92 AESDecrypter decrypter = new AESDecrypterBC(); 93 AesZipFileDecrypter zipDecrypter = null; 94 try { 95 zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter); 96 AesZipFileDecrypter.charset = "utf-8"; 97 /** 98 * 得到ZIP檔案中所有Entry,但此處好像與JDK裡不同,目錄不視為Entry 99 * 需要建立資料夾,entry.isDirectory()方法同樣不適用,不知道是不是自己使用錯誤 100 * 處理資料夾問題處理可能不太好 101 */ 102 List<ExtZipEntry> entryList = zipDecrypter.getEntryList(); 103 for(ExtZipEntry entry : entryList) { 104 String eName = entry.getName(); 105 String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1); 106 File extractDir = new File(outDir, dir); 107 if (!extractDir.exists()) { 108 FileUtils.forceMkdir(extractDir); 109 } 110 /** 111 * 抽出檔案 112 */ 113 File extractFile = new File(outDir + File.separator + eName); 114 zipDecrypter.extractEntry(entry, extractFile, passwd); 115 } 116 } catch (IOException e) { 117 e.printStackTrace(); 118 } catch (DataFormatException e) { 119 e.printStackTrace(); 120 } finally { 121 try { 122 zipDecrypter.close(); 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } 126 } 127 } 128 /** 129 * 測試 130 * @param args 131 */ 132 public static void main(String[] args) { 133 /** 134 * 壓縮測試 135 * 可以傳檔案或者目錄 136 */ 137 // zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh"); 138 // zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh"); 139 unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh"); 140 } 141 }

壓縮多個檔案時,有兩個方法(第一種沒試):

(1) 預先把多個檔案壓縮成zip,然後呼叫enc.addAll(inZipFile, password);方法將多個zip檔案加進來。

(2)針對需要壓縮的檔案迴圈呼叫enc.add(inFile, password);,每次都用相同的密碼。

修改原始碼後的專案可到上面提到的部落格去下載,或者參照部落格自己修改,其實也很容易,畢竟只有幾處改動。

另外我的CSDN下載頻道也上傳了修改後的原始碼和jar包,也可以去那裡下載。

修改記錄

需要修改的檔案有:

  • ExtZipOutputStream
  • ExtZipEntry
  • AesZipFileEncrypter

在ExtZipOutputStream裡增加一成員變數並新增兩個方法:

 1 protected String encoding = "iso-8859-1";    
 2 public boolean utf8Flg = false;
 3     public void setEncoding(String encoding) {
 4         this.encoding = encoding;
 5         utf8Flg |= isUTF8(encoding);
 6     }
 7     protected boolean isUTF8(String encoding) {
 8     if (encoding == null) {
 9       // check platform's default encoding
10       encoding = System.getProperty("file.encoding");
11     }
12     return "UTF8".equalsIgnoreCase(encoding)
13       || "UTF-8".equalsIgnoreCase(encoding);
14   }

然後將ExtZipOutputStream的(134行和158行左右)iso-8859-1編碼替換成上面設定的編碼格式

接著,再將106行左右檔名長度取得程式碼改成:

writeShort(entry.getName().getBytes(encoding).length); // file name length

這裡有個地方需要注意,當檔名是utf8編碼格式的時候,需要設定Zip包的通用位標誌 (不明白)

第十一個位元為1,程式碼修改如下:

修改ExtZipEntry類在initEncryptedEntry方法基礎上增加一個過載方法:

 1 public void initEncryptedEntry(boolean utf8Flag) {
 2     setCrc(0); // CRC-32 / for encrypted files it's 0 as AES/MAC checks integritiy 
 3     this.flag |= 1; // bit0 - encrypted
 4     if (utf8Flag) {
 5         this.flag |=(1 << 11);
 6     }
 7     // flag |= 8; // bit3 - use data descriptor
 8     this.primaryCompressionMethod = 0x63;
 9  
10     byte[] extraBytes = new byte[11];
11     extraBytes = new byte[11];
12     // extra data header ID for AES encryption is 0x9901
13     extraBytes[0] = 0x01;
14     extraBytes[1] = (byte)0x99; 
15     // data size (currently 7, but subject to possible increase in the
16     // future)
17     extraBytes[2] = 0x07; // data size
18     extraBytes[3] = 0x00; // data size
19     // Integer version number specific to the zip vendor
20     extraBytes[4] = 0x02; // version number
21     extraBytes[5] = 0x00; // version number
22  
23     // 2-character vendor ID
24     extraBytes[6] = 0x41; // vendor id
25     extraBytes[7] = 0x45; // vendor id 
26     // AES encryption strength - 1=128, 2=192, 3=256
27     extraBytes[8] = 0x03; 
28     // actual compression method - 0x0000==stored (no compression) - 2 bytes
29     extraBytes[9] = (byte) (getMethod() & 0xff);
30     extraBytes[10] = (byte) ((getMethod() & 0xff00) >> 8);
31  
32     setExtra(extraBytes);
33 }

其實就是增加一個引數並增加了下面這段程式碼:

1 if (utf8Flag) {
2   this.flag |=(1 << 11);
3 }

當然不要忘了將呼叫該方法地方修改一下,傳進utf8Flag引數

AesZipFileEncrypter類裡有兩處(在兩個add方法中)其它地方不需改動。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援指令碼之家。