package com.ninemax.demo.zip.decrypt.zh_cn;
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");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java使用winzipaes对zip文件的操作,支持中文
共76个文件
html:69个
jar:2个
gif:1个
5星 · 超过95%的资源 需积分: 16 336 下载量 114 浏览量
2012-07-07
15:35:43
上传
评论 4
收藏 1.56MB ZIP 举报
温馨提示
winzipaes是Java操作ZIP文件的开源项目,支持密码,但不支持中文文件名。此文件中包含修改后的源码、文档、jar包、依赖包及示例程序等文件。可以压缩文件名含有中文的文件,修改之处可以参看博客http://xjlsgcjdtc.iteye.com/blog/1439514或者http://blog.csdn.net/zhangyihui1986/article/detai
资源推荐
资源详情
资源评论
收起资源包目录
winzipaes_zh_CN_supported.zip (76个子文件)
bcprov-jdk16-140.jar 1.58MB
winzipaes-docs
overview-frame.html 1KB
constant-values.html 22KB
deprecated-list.html 5KB
resources
inherit.gif 57B
de
idyl
winzipaes
package-tree.html 6KB
package-frame.html 1014B
package-summary.html 6KB
class-use
AesZipFileDecrypter.html 6KB
AesZipFileEncrypter.html 6KB
AesZipFileDecrypter.html 21KB
impl
package-tree.html 9KB
package-frame.html 3KB
package-summary.html 10KB
ZipFileEntryInputStream.html 14KB
CentralDirectoryEntry.html 21KB
AESUtilsJCA.html 14KB
AESEncrypterJCA.html 14KB
AESEncrypterBC.html 16KB
class-use
ZipFileEntryInputStream.html 6KB
CentralDirectoryEntry.html 9KB
AESUtilsJCA.html 6KB
AESEncrypterJCA.html 6KB
AESEncrypterBC.html 6KB
ExtZipEntry.html 14KB
AESEncrypter.html 13KB
AESCryptoBase.html 8KB
ExtZipOutputStream.html 6KB
ByteArrayHelper.html 6KB
AESDecrypterBC.html 6KB
AESDecrypterJCA.html 6KB
ZipConstants.html 10KB
ExtRandomAccessFile.html 8KB
AESDecrypter.html 10KB
ExtZipEntry.html 45KB
AESEncrypter.html 11KB
AESCryptoBase.html 12KB
ExtZipOutputStream.html 18KB
ByteArrayHelper.html 13KB
package-use.html 10KB
AESDecrypterBC.html 14KB
AESDecrypterJCA.html 13KB
ZipConstants.html 11KB
ExtRandomAccessFile.html 18KB
AESDecrypter.html 10KB
package-use.html 5KB
AesZipFileEncrypter.html 23KB
overview-tree.html 9KB
package-list 43B
index-files
index-12.html 7KB
index-13.html 7KB
index-11.html 7KB
index-9.html 6KB
index-5.html 12KB
index-8.html 10KB
index-1.html 14KB
index-18.html 8KB
index-2.html 7KB
index-16.html 8KB
index-4.html 8KB
index-15.html 8KB
index-10.html 7KB
index-17.html 7KB
index-3.html 9KB
index-14.html 10KB
index-6.html 7KB
index-7.html 18KB
allclasses-noframe.html 3KB
help-doc.html 8KB
index.html 1KB
overview-summary.html 5KB
allclasses-frame.html 3KB
stylesheet.css 1KB
winszipaes_zh_CN_supported_20120416_src.zip 26KB
winszipaes_zh_CN_supported_20120416.jar 33KB
DecryptionZipUtil.java 5KB
共 76 条
- 1
玄月初心
- 粉丝: 225
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页