首先引入 org.bouncycastle.jar ,AesZip.jar
下载路径:/download/yingfeng612/9933192 , /download/yingfeng612/9933208
package com.prac.zip;import java.io.File;import java.io.IOException;import java.util.zip.DataFormatException;import de.idyl.winzipaes.AesZipFileDecrypter;import de.idyl.winzipaes.AesZipFileEncrypter;import de.idyl.winzipaes.impl.AESDecrypterBC;import de.idyl.winzipaes.impl.AESEncrypterBC;import de.idyl.winzipaes.impl.ExtZipEntry;public class FileUtil {/*** 压缩单个文件并加密* @param srcFile待压缩的文件* @param desFileName 生成的目标文件* @param passWord压缩文件加密密码* @throws IOException*/public static void zipFile(String srcFile,String desFile,String passWord) throws IOException{AesZipFileEncrypter.zipAndEncrypt(new File(srcFile),new File(desFile), passWord, new AESEncrypterBC());}/*** 给指定的压缩文件进行加密* @param srcZipFile待加密的压缩文件* @param desFile 加密后的目标压缩文件* @param passWord 压缩文件加密密码* @throws IOException*/public static void encryptZipFile(String srcZipFile,String desFile,String passWord) throws IOException{AesZipFileEncrypter.zipAndEncryptAll(new File(srcZipFile), new File(desFile), passWord, new AESEncrypterBC());}/*** 解密抽取压缩文件中的某个文件* @param srcZipFile 加密的压缩文件* @param extractFileName 抽取压缩文件中的某个文件的名称* @param desFile解压后抽取后生成的目标文件* @param passWord 解压密码* @throws IOException* @throws DataFormatException*/public static void decrypterZipFile(String srcZipFile,String extractFileName,String desFile,String passWord)throws IOException, DataFormatException{AesZipFileDecrypter zipFile = new AesZipFileDecrypter( new File(srcZipFile),new AESDecrypterBC());ExtZipEntry entry = zipFile.getEntry(extractFileName);zipFile.extractEntry( entry, new File(desFile),passWord);}public static void main(String[] args) throws Exception {String srcFile="/app/1_admin_work.jpg";String desFile="/app/1_admin_work_encrypter.zip";FileUtil.zipFile(srcFile, desFile,"123456abc");String srczipfile="/app/work.zip";String deszipfile="/app/work_encrypter.zip";FileUtil.encryptZipFile(srczipfile, deszipfile,"123456abc");String decrypterSrcZipFile="/app/1_admin_work_encrypter.zip";String extractFileName="\\app\\"+"1_admin_work.jpg";String decrypterDesFile="/app/1_admin_work2.jpg";FileUtil.decrypterZipFile(decrypterSrcZipFile,extractFileName, decrypterDesFile,"123456abc");}}
上文中的AesZip.jar也可以自己下载源码打包,路径:/archive/p/winzipaes/downloads ,手工将源码打包即可使用。