import fr.opensagres.xdocreport.core.io.IOUtils;
import net.lingala.zip4j.ZipFile;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.*;
public
class
ZipAndRarTools {
private
static
final
Logger log = LoggerFactory.getLogger(ZipAndRarTools.
class
);
private
static
String unZip(String rootPath, String sourceRarPath, String destDirPath, String passWord) {
ZipFile zipFile = null;
try
{
if
(StringUtils.isNotBlank(passWord)) {
zipFile =
new
ZipFile(filePath, passWord.toCharArray());
}
else
{
zipFile =
new
ZipFile(filePath);
}
zipFile.extractAll(rootPath + destDirPath);
}
catch
(Exception e) {
log.error(
"unZip error"
, e);
return
e.getMessage();
}
return
""
;
}
private
static
String unRar(String rootPath, String sourceRarPath, String destDirPath, String passWord) {
String rarDir = rootPath + sourceRarPath;
String outDir = rootPath + destDirPath + File.separator;
RandomAccessFile randomAccessFile = null;
IInArchive inArchive = null;
try
{
randomAccessFile =
new
RandomAccessFile(rarDir,
"r"
);
if
(StringUtils.isNotBlank(passWord))
inArchive = SevenZip.openInArchive(null,
new
RandomAccessFileInStream(randomAccessFile), passWord);
else
inArchive = SevenZip.openInArchive(null,
new
RandomAccessFileInStream(randomAccessFile));
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
for
(
final
ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
final
int[] hash =
new
int[]{0};
if
(!item.isFolder()) {
ExtractOperationResult result;
final
long[] sizeArray =
new
long[1];
File outFile =
new
File(outDir + item.getPath());
File parent = outFile.getParentFile();
if
((!parent.exists()) && (!parent.mkdirs())) {
continue
;
}
if
(StringUtils.isNotBlank(passWord)) {
result = item.extractSlow(data -> {
try
{
IOUtils.write(data,
new
FileOutputStream(outFile, true));
}
catch
(Exception e) {
e.printStackTrace();
}
hash[0] ^= Arrays.hashCode(data);
sizeArray[0] += data.length;
return
data.length;
}, passWord);
}
else
{
result = item.extractSlow(data -> {
try
{
IOUtils.write(data,
new
FileOutputStream(outFile, true));
}
catch
(Exception e) {
e.printStackTrace();
}
hash[0] ^= Arrays.hashCode(data);
sizeArray[0] += data.length;
return
data.length;
});
}
if
(result == ExtractOperationResult.OK) {
log.error(
"解压rar成功...."
+ String.format(
"%9X | %10s | %s"
, hash[0], sizeArray[0], item.getPath()));
}
else
if
(StringUtils.isNotBlank(passWord)) {
log.error(
"解压rar成功:密码错误或者其他错误...."
+ result);
return
"password"
;
}
else
{
return
"rar error"
;
}
}
}
}
catch
(Exception e) {
log.error(
"unRar error"
, e);
return
e.getMessage();
} finally {
try
{
inArchive.close();
randomAccessFile.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
return
""
;
}
private
static
String un7z(String rootPath, String sourceRarPath, String destDirPath, String passWord) {
try
{
File srcFile =
new
File(rootPath + sourceRarPath);
if
(!srcFile.exists()) {
throw
new
Exception(srcFile.getPath() +
"所指文件不存在"
);
}
SevenZFile zIn = null;
if
(StringUtils.isNotBlank(passWord))
new
SevenZFile(srcFile, passWord.getBytes());
else
new
SevenZFile(srcFile);
SevenZArchiveEntry entry = null;
File file = null;
while
((entry = zIn.getNextEntry()) != null) {
if
(!entry.isDirectory()) {
file =
new
File(rootPath + destDirPath, entry.getName());
if
(!file.exists()) {
new
File(file.getParent()).mkdirs();
}
OutputStream out =
new
FileOutputStream(file);
BufferedOutputStream bos =
new
BufferedOutputStream(out);
int len = -1;
byte[] buf =
new
byte[1024];
while
((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bos.close();
out.close();
}
}
}
catch
(Exception e) {
log.error(
"un7z is error"
, e);
return
e.getMessage();
}
return
""
;
}
public
static
void unFile(String rootPath, String sourcePath, String destDirPath, String passWord) {
String result =
""
;
if
(sourcePath.toLowerCase().endsWith(
".zip"
)) {
result = unZip(rootPath, sourcePath, destDirPath, passWord);
}
else
if
(sourcePath.toLowerCase().endsWith(
".rar"
)) {
result = unRar(rootPath, sourcePath, destDirPath, passWord);
}
else
if
(sourcePath.toLowerCase().endsWith(
".7z"
)) {
result = un7z(rootPath, sourcePath, destDirPath, passWord);
}
resultMap.put(
"resultMsg"
, 1);
if
(StringUtils.isNotBlank(result)) {
if
(result.contains(
"password"
)) resultMap.put(
"resultMsg"
, 2);
if
(!result.contains(
"password"
)) resultMap.put(
"resultMsg"
, 3);
}
resultMap.put(
"files"
, data);
return
resultMap;
}
public
static
void main(String[] args) {
getFileList(
"G:\\ziptest\\"
,
"测试.zip"
,
"test3333"
,
"密码"
);
}
}