ç¨javaå å¯å缩zipæ件ï¼
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项ç®(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)
* æ¹æ³æ¯å°æ件ç´æ¥æ·»å è¿å缩æ件
*
* add(File f, String pathForEntry, String passwd)
* æ¹æ³æ¯ææå®è·¯å¾å°æ件添å è¿å缩æ件
* 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();
}
}
}
/**
* å
·ä½å缩æ¹æ³,å°ç»å®æ件添å è¿å缩æ件ä¸,并å¤çå缩æ件ä¸çè·¯å¾
* @param file ç»å®ç£çæ件(æ¯æ件ç´æ¥æ·»å ,æ¯ç®å½éå½è°ç¨æ·»å )
* @param encrypter AesZipFileEncrypterå®ä¾,ç¨äºè¾åºå å¯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
* éè¦å建æ件夹,entry.isDirectory()æ¹æ³åæ ·ä¸éç¨,ä¸ç¥éæ¯ä¸æ¯èªå·±ä½¿ç¨é误
* å¤çæ件夹é®é¢å¤çå¯è½ä¸å¤ªå¥½
*/
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");
}
}
å缩å¤ä¸ªæ件æ¶ï¼æ两个æ¹æ³(第ä¸ç§æ²¡è¯)ï¼
ï¼1ï¼ é¢å
æå¤ä¸ªæ件å缩æzipï¼ç¶åè°ç¨enc.addAll(inZipFile, password);æ¹æ³å°å¤ä¸ªzipæ件å è¿æ¥ã
ï¼2ï¼é对éè¦å缩çæ件循ç¯è°ç¨enc.add(inFile, password);ï¼æ¯æ¬¡é½ç¨ç¸åçå¯ç ã
温馨提示:内容为网友见解,仅供参考