Java代码中的(解压7z加密版)

maven:需要加上这个下载这两个包

<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding</artifactId>
<version>9.20-2.00beta</version>
</dependency>


<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding-all-platforms</artifactId>
<version>9.20-2.00beta</version>
</dependency>

普通的架构:需要自己下载

sevenzipjbinding-9.20-2.00beta.jar

sevenzipjbinding-all-platforms-9.20-2.00beta.jar

/**
*
* @Description (解压7z)
* @param file7zPath(7z文件路径)
* @param outPutPath(解压路径)
* @param passWord(文件密码.没有可随便写,或空)
* @return
* @throws Exception
*/
public static int un7z(String file7zPath, final String outPutPath, String passWord) throws Exception {
IInArchive archive;
RandomAccessFile randomAccessFile;
randomAccessFile = new RandomAccessFile(file7zPath, "r");
archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord);
int numberOfItems = archive.getNumberOfItems();
ISimpleInArchive simpleInArchive = archive.getSimpleInterface();
for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
final int[] hash = new int[] { 0 };
if (!item.isFolder()) {
ExtractOperationResult result;
final long[] sizeArray = new long[1];
result = item.extractSlow(new ISequentialOutStream() {
public int write(byte[] data) throws SevenZipException {
try {
IOUtils.write(data, new FileOutputStream(new File(outPutPath + File.separator + item.getPath()),true));
} catch (Exception e) {
e.printStackTrace();
}
hash[0] ^= Arrays.hashCode(data); // Consume data
sizeArray[0] += data.length;
return data.length; // Return amount of consumed
}
},passWord);
if (result == ExtractOperationResult.OK) {
logger.error("解压成功...." +String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));
// LogUtil.getLog().debug(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));
} else {
logger.error("解压失败:密码错误或者其他错误...." +result);
// LogUtil.getLog().debug("Error extracting item: " + result);
}
}
}
archive.close();
randomAccessFile.close();

return numberOfItems;
}

/**

*不含加密,普通解压

**/

// 解压.Z文件   如:D:/test/test.Z   D:/test/test.txt 
public static void unZFile(String inFileName, String outFileName) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(inFileName);
inputStream = new UncompressInputStream(inputStream);

File file = new File(outFileName);

outputStream = new FileOutputStream(file);

int bytesRead = 0;
byte[] buffer = new byte[100000];
while ((bytesRead = inputStream.read(buffer, 0, 100000)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("unZFile Exception " + e.getMessage());
} finally {
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("outputStream Close Exception " + e.getMessage());
}
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("inputStream Close Exception "+ e.getMessage());
}
}
}
}

原文地址:https://www.cnblogs.com/huangwentian/p/6782074.html