Java解壓和壓縮帶密碼的zip文件過(guò)程詳解
前言
JDK自帶的ZIP操作接口(java.util.zip包,請(qǐng)參看文章末尾的博客鏈接)并不支持密碼,甚至也不支持中文文件名。
為了解決ZIP壓縮文件的密碼問(wèn)題,在網(wǎng)上搜索良久,終于找到了winzipaes開(kāi)源項(xiàng)目。
該項(xiàng)目在google code下托管 ,僅支持AES壓縮和解壓zip文件( This library only supports Win-Zip's 256-Bit AES mode.)。網(wǎng)站上下載的文件是源代碼,最新版本為winzipaes_src_20120416.zip,本示例就是在此基礎(chǔ)上編寫(xiě)。
詳述
項(xiàng)目使用很簡(jiǎn)單,利用源碼自己導(dǎo)出一個(gè)jar文件,在項(xiàng)目中引用即可。
這里有一個(gè)需要注意的問(wèn)題,就是如果給定ZIP文件沒(méi)有密碼,那么就不能使用該項(xiàng)目解壓,如果壓縮文件沒(méi)有密碼卻使用該項(xiàng)目解壓在這里會(huì)報(bào)一個(gè)異常,所以使用中需要注意:加密ZIP文件可以使用它解壓,沒(méi)有加密的就需要采取其它方式了。
此文就是采用修改后的winzipaes編寫(xiě),并記錄詳細(xì)修改步驟。
winzipaes項(xiàng)目依賴(lài)bcprov的jar包
示例
在研究該項(xiàng)目時(shí)寫(xiě)了一個(gè)工具類(lèi),本來(lái)準(zhǔn)備用在項(xiàng)目中,最后找到了更好的解決方案zip4j來(lái)代替,所以最終沒(méi)有采用。
package com.ninemax.demo.zip.decrypt;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;
import org.apache.commons.io.FileUtils;
import de.idyl.winzipaes.AesZipFileDecrypter;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESDecrypter;
import de.idyl.winzipaes.impl.AESDecrypterBC;
import de.idyl.winzipaes.impl.AESEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import de.idyl.winzipaes.impl.ExtZipEntry;
/**
* 壓縮指定文件或目錄為ZIP格式壓縮文件
* 支持中文(修改源碼后)
* 支持密碼(僅支持256bit的AES加密解密)
* 依賴(lài)bcprov項(xiàng)目(bcprov-jdk16-140.jar)
*
* @author zyh
*/
public class DecryptionZipUtil {
/**
* 使用指定密碼將給定文件或文件夾壓縮成指定的輸出ZIP文件
* @param srcFile 需要壓縮的文件或文件夾
* @param destPath 輸出路徑
* @param passwd 壓縮文件使用的密碼
*/
public static void zip(String srcFile,String destPath,String passwd) {
AESEncrypter encrypter = new AESEncrypterBC();
AesZipFileEncrypter zipFileEncrypter = null;
try {
zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);
/**
* 此方法是修改源碼后添加,用以支持中文文件名
*/
zipFileEncrypter.setEncoding("utf8");
File sFile = new File(srcFile);
/**
* AesZipFileEncrypter提供了重載的添加Entry的方法,其中:
* add(File f, String passwd)
* 方法是將文件直接添加進(jìn)壓縮文件
*
* add(File f, String pathForEntry, String passwd)
* 方法是按指定路徑將文件添加進(jìn)壓縮文件
* pathForEntry - to be used for addition of the file (path within zip file)
*/
doZip(sFile, zipFileEncrypter, "", passwd);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipFileEncrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 具體壓縮方法,將給定文件添加進(jìn)壓縮文件中,并處理壓縮文件中的路徑
* @param file 給定磁盤(pán)文件(是文件直接添加,是目錄遞歸調(diào)用添加)
* @param encrypter AesZipFileEncrypter實(shí)例,用于輸出加密ZIP文件
* @param pathForEntry ZIP文件中的路徑
* @param passwd 壓縮密碼
* @throws IOException
*/
private static void doZip(File file, AesZipFileEncrypter encrypter,
String pathForEntry, String passwd) throws IOException {
if (file.isFile()) {
pathForEntry += file.getName();
encrypter.add(file, pathForEntry, passwd);
return;
}
pathForEntry += file.getName() + File.separator;
for(File subFile : file.listFiles()) {
doZip(subFile, encrypter, pathForEntry, passwd);
}
}
/**
* 使用給定密碼解壓指定壓縮文件到指定目錄
* @param inFile 指定Zip文件
* @param outDir 解壓目錄
* @param passwd 解壓密碼
*/
public static void unzip(String inFile, String outDir, String passwd) {
File outDirectory = new File(outDir);
if (!outDirectory.exists()) {
outDirectory.mkdir();
}
AESDecrypter decrypter = new AESDecrypterBC();
AesZipFileDecrypter zipDecrypter = null;
try {
zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);
AesZipFileDecrypter.charset = "utf-8";
/**
* 得到ZIP文件中所有Entry,但此處好像與JDK里不同,目錄不視為Entry
* 需要?jiǎng)?chuàng)建文件夾,entry.isDirectory()方法同樣不適用,不知道是不是自己使用錯(cuò)誤
* 處理文件夾問(wèn)題處理可能不太好
*/
List<ExtZipEntry> entryList = zipDecrypter.getEntryList();
for(ExtZipEntry entry : entryList) {
String eName = entry.getName();
String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);
File extractDir = new File(outDir, dir);
if (!extractDir.exists()) {
FileUtils.forceMkdir(extractDir);
}
/**
* 抽出文件
*/
File extractFile = new File(outDir + File.separator + eName);
zipDecrypter.extractEntry(entry, extractFile, passwd);
}
} catch (IOException e) {
e.printStackTrace();
} catch (DataFormatException e) {
e.printStackTrace();
} finally {
try {
zipDecrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 測(cè)試
* @param args
*/
public static void main(String[] args) {
/**
* 壓縮測(cè)試
* 可以傳文件或者目錄
*/
// zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");
// zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");
unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");
}
}
壓縮多個(gè)文件時(shí),有兩個(gè)方法(第一種沒(méi)試):
(1) 預(yù)先把多個(gè)文件壓縮成zip,然后調(diào)用enc.addAll(inZipFile, password);方法將多個(gè)zip文件加進(jìn)來(lái)。
(2)針對(duì)需要壓縮的文件循環(huán)調(diào)用enc.add(inFile, password);,每次都用相同的密碼。
修改源碼后的項(xiàng)目可到上面提到的博客去下載,或者參照博客自己修改,其實(shí)也很容易,畢竟只有幾處改動(dòng)。
另外我的CSDN下載頻道也上傳了修改后的源碼和jar包,也可以去那里下載。
修改記錄
需要修改的文件有:
- ExtZipOutputStream
- ExtZipEntry
- AesZipFileEncrypter
在ExtZipOutputStream里增加一成員變量并添加兩個(gè)方法:
protected String encoding = "iso-8859-1";
public boolean utf8Flg = false;
public void setEncoding(String encoding) {
this.encoding = encoding;
utf8Flg |= isUTF8(encoding);
}
protected boolean isUTF8(String encoding) {
if (encoding == null) {
// check platform's default encoding
encoding = System.getProperty("file.encoding");
}
return "UTF8".equalsIgnoreCase(encoding)
|| "UTF-8".equalsIgnoreCase(encoding);
}
然后將ExtZipOutputStream的(134行和158行左右)iso-8859-1編碼替換成上面設(shè)置的編碼格式
接著,再將106行左右文件名長(zhǎng)度取得代碼改成:
writeShort(entry.getName().getBytes(encoding).length); // file name length
這里有個(gè)地方需要注意,當(dāng)文件名是utf8編碼格式的時(shí)候,需要設(shè)置Zip包的通用位標(biāo)志 (不明白)
第十一個(gè)比特為1,代碼修改如下:
修改ExtZipEntry類(lèi)在initEncryptedEntry方法基礎(chǔ)上增加一個(gè)重載方法:
public void initEncryptedEntry(boolean utf8Flag) {
setCrc(0); // CRC-32 / for encrypted files it's 0 as AES/MAC checks integritiy
this.flag |= 1; // bit0 - encrypted
if (utf8Flag) {
this.flag |=(1 << 11);
}
// flag |= 8; // bit3 - use data descriptor
this.primaryCompressionMethod = 0x63;
byte[] extraBytes = new byte[11];
extraBytes = new byte[11];
// extra data header ID for AES encryption is 0x9901
extraBytes[0] = 0x01;
extraBytes[1] = (byte)0x99;
// data size (currently 7, but subject to possible increase in the
// future)
extraBytes[2] = 0x07; // data size
extraBytes[3] = 0x00; // data size
// Integer version number specific to the zip vendor
extraBytes[4] = 0x02; // version number
extraBytes[5] = 0x00; // version number
// 2-character vendor ID
extraBytes[6] = 0x41; // vendor id
extraBytes[7] = 0x45; // vendor id
// AES encryption strength - 1=128, 2=192, 3=256
extraBytes[8] = 0x03;
// actual compression method - 0x0000==stored (no compression) - 2 bytes
extraBytes[9] = (byte) (getMethod() & 0xff);
extraBytes[10] = (byte) ((getMethod() & 0xff00) >> 8);
setExtra(extraBytes);
}
其實(shí)就是增加一個(gè)參數(shù)并增加了下面這段代碼:
if (utf8Flag) {
this.flag |=(1 << 11);
}
當(dāng)然不要忘了將調(diào)用該方法地方修改一下,傳進(jìn)utf8Flag參數(shù)
AesZipFileEncrypter類(lèi)里有兩處(在兩個(gè)add方法中)其它地方不需改動(dòng)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java壓縮zip文件中文亂碼問(wèn)題解決方法
- java使用gzip實(shí)現(xiàn)文件解壓縮示例
- Java實(shí)現(xiàn)解壓zip壓縮包的兩種方法(支持多層級(jí))
- Java后臺(tái)實(shí)現(xiàn)瀏覽器一鍵導(dǎo)出下載zip壓縮包
- Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法
- 用Java進(jìn)行zip文件壓縮與解壓縮
- Java實(shí)現(xiàn)導(dǎo)出ZIP壓縮包的方法
- java實(shí)現(xiàn)一次性壓縮多個(gè)文件到zip中的方法示例
- Java創(chuàng)建ZIP壓縮文件的方法
- Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包
- 淺談Java?Zip?壓縮及其優(yōu)化
相關(guān)文章
SpringCloudAlibaba整合Feign實(shí)現(xiàn)遠(yuǎn)程HTTP調(diào)用的簡(jiǎn)單示例
這篇文章主要介紹了SpringCloudAlibaba 整合 Feign 實(shí)現(xiàn)遠(yuǎn)程 HTTP 調(diào)用,文章中使用的是OpenFeign,是Spring社區(qū)開(kāi)發(fā)的組件,需要的朋友可以參考下2021-09-09
啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法
這篇文章主要為大家詳細(xì)介紹了啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Java字符編碼簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java字符編碼簡(jiǎn)介,本文主要包括以下幾個(gè)方面:編碼基本知識(shí),Java,系統(tǒng)軟件,url,工具軟件等,感興趣的朋友一起看看吧2017-08-08
Java利用EasyExcel解析動(dòng)態(tài)表頭及導(dǎo)出實(shí)現(xiàn)過(guò)程
以前做導(dǎo)出功能,表頭和數(shù)據(jù)都是固定的,下面這篇文章主要給大家介紹了關(guān)于Java利用EasyExcel解析動(dòng)態(tài)表頭及導(dǎo)出實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
java 中動(dòng)態(tài)代理機(jī)制的實(shí)例講解
這篇文章主要介紹了java 中動(dòng)態(tài)代理機(jī)制的實(shí)例講解的相關(guān)資料,希望通過(guò)本文大家能夠理解掌握動(dòng)態(tài)代理機(jī)制,需要的朋友可以參考下2017-09-09

