/
ZipPwdUtil.java
144 lines (138 loc) · 5.15 KB
/
ZipPwdUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* 文件名:ZipPwdUtil.java
* 版权:Copyright 2007-2017 517na Tech. Co. Ltd. All Rights Reserved.
* 描述: ZipPwdUtil.java
* 修改人:xiaofan
* 修改时间:2017年2月5日
* 修改内容:新增
*/
package com.zxiaofan.util.other;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
/**
* 压缩文件密码破解工具类.
*
* 7Z:jar(commons-compress-1.9.jar、xz-1.6.jar)
*
* @author xiaofan
*/
public class ZipPwdUtil {
public static void main(String[] args) throws IOException {
// Scanner scanner = new Scanner(System.in);
// String zipPath = scanner.nextLine();
String zipPath = "e:\\123.7z";
String pwd = "79";
boolean bool = validatePwd(zipPath, pwd);
System.out.println(bool);
}
/**
* 校验xx.7Z压缩包的密码是否正确.
*
* 若抛异常(java.security.InvalidKeyException: Illegal key size),
*
* 在官方网站下载JCE无限制权限策略文件解决JDK不兼容问题(亦可从工程lib包下载)。
*
* JDK7的下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
*
* JDK8的下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
*
* 将解压后的local_policy.jar和US_export_policy.jar放到%JDK_HOME%\jre\lib\security目录和%JRE_HOME%\lib\security目录下覆盖原文件即可。
*
* @param path7ZFile
* 7Z压缩包文件路径
* @param pwd
* 密码
* @return 密码是否正确
*/
public static boolean validatePwd(String path7ZFile, String pwd) {
boolean bool = true;
File file = new File(path7ZFile);
if (!file.exists()) {
System.out.println("no file[" + path7ZFile + "]");
return false;
}
if (null == pwd) {
pwd = "";
}
try {
@SuppressWarnings("resource")
SevenZFile sevenZFile = new SevenZFile(file, pwd.getBytes(Charset.forName("UTF-16LE")));
SevenZArchiveEntry entry = null;
while (null != (entry = sevenZFile.getNextEntry())) {
byte[] content = new byte[2]; // 流输出
try {
// sevenZFile.read(content, 0, 2);
// sevenZFile.read(content, (int) (entry.getSize() - 2), (int) entry.getSize());
sevenZFile.read(content, 0, (int) entry.getSize());
// sevenZFile.read(content, 0, 2);
} catch (Exception e) { // 读取数据异常,说明密码错误
return false;
}
}
} catch (IOException e) {
return false;
}
return bool;
}
/**
* 解压7Z文件到当前位置.
*
* @param path7ZFile
* 7Z压缩包文件路径
* @param pwd
* 密码(无密码传null)
* @return 解压成功
*/
public static boolean un7z(String path7ZFile, String pwd) {
SevenZFile sevenZFile = null;
try {
File file = new File(path7ZFile);
if (!file.exists()) {
System.out.println("no file[" + path7ZFile + "]");
return false;
}
if (null == pwd) {
pwd = "";
}
sevenZFile = new SevenZFile(file, pwd.getBytes(Charset.forName("UTF-16LE")));
SevenZArchiveEntry archiveEntry = null;
while ((archiveEntry = sevenZFile.getNextEntry()) != null) {
String entryFileName = archiveEntry.getName(); // 文件名
byte[] content = new byte[(int) archiveEntry.getSize()]; // 流输出
sevenZFile.read(content, 0, content.length);
String entryFilePath = file.getParent() + entryFileName; // 解压文件存放路径
OutputStream os = null;
try {
File entryFile = new File(entryFilePath);
os = new BufferedOutputStream(new FileOutputStream(entryFile)); // 输出解压文件
os.write(content);
} finally {
if (os != null) {
os.flush();
os.close();
}
}
}
sevenZFile.close();
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (sevenZFile != null) {
sevenZFile.close();
}
} catch (IOException ex) {
ex.printStackTrace();
return false;
}
}
return true;
}
}