溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中怎么解壓和壓縮帶密碼的zip文件

發(fā)布時(shí)間:2021-06-17 13:47:16 來源:億速云 閱讀:315 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)Java中怎么解壓和壓縮帶密碼的zip文件,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

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加密解密)
 * 依賴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 給定磁盤文件(是文件直接添加,是目錄遞歸調(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ò)誤
			 * 處理文件夾問題處理可能不太好
			 */
			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();
			}
		}
	} 
	/**
	 * 測試
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * 壓縮測試
		 * 可以傳文件或者目錄
		 */
//		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è)方法(第一種沒試):

(1) 預(yù)先把多個(gè)文件壓縮成zip,然后調(diào)用enc.addAll(inZipFile, password);方法將多個(gè)zip文件加進(jìn)來。

(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行左右文件名長度取得代碼改成:

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

這里有個(gè)地方需要注意,當(dāng)文件名是utf8編碼格式的時(shí)候,需要設(shè)置Zip包的通用位標(biāo)志 (不明白)

第十一個(gè)比特為1,代碼修改如下: 

修改ExtZipEntry類在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類里有兩處(在兩個(gè)add方法中)其它地方不需改動(dòng)。

以上就是Java中怎么解壓和壓縮帶密碼的zip文件,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI