Java解壓.7z格式壓縮包
阿新 • • 發佈:2019-02-11
package com.annet.upload.core.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.util.Arrays;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
public class Un7zUtils {
private static Logger logger = LoggerFactory.getLogger(Un7zUtils.class);
/**
*
* @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 {
String str = item.getPath();
str = str.substring(0, str.lastIndexOf(File.separator));
File file = new File(outPutPath + File.separator + str + File.separator);
if (!file.exists()) {
file.mkdirs();
}
File file1 = new File(outPutPath + File.separator + item.getPath());
IOUtils.write(data, new FileOutputStream(file1, 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()));
} else {
logger.error("解壓失敗:密碼錯誤或者其他錯誤...." + result);
}
}
}
archive.close();
randomAccessFile.close();
return numberOfItems;
}
/**
* 遞迴刪除資料夾
*
* @param file
*/
public static void deleteFile(File file) {
if (file.exists()) { // 判斷檔案是否存在
if (file.isFile()) { // 判斷是否是檔案
file.delete(); // 刪除檔案
} else if (file.isDirectory()) { // 否則如果它是一個目錄
File[] files = file.listFiles(); // 宣告目錄下所有的檔案 files[];
for (int i = 0; i < files.length; i++) { // 遍歷目錄下所有的檔案
deleteFile(files[i]); // 把每個檔案用這個方法進行迭代
}
file.delete(); // 刪除資料夾
}
}
}
}
maven依賴jar
<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>