package com.hgq.util;
import com.github.junrar.Junrar;
import com.github.junrar.exception.RarException;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
import java.io.*;
import java.util.Arrays;
/**
* TODO 类的作用信息
*
* @author huguangquan
* @date 22-8-18 018
*/
public class RarUtil {
/**
* 通过7z-java-banding 解压rar 支持Rar5版本
* @param sourceZipFile
* @param destinationDir
*/
public static void unRar(String sourceZipFile, String destinationDir) {
RandomAccessFile randomAccessFile = null;
IInArchive inArchive = null;
try {
randomAccessFile = new RandomAccessFile(sourceZipFile, "r");
inArchive = SevenZip.openInArchive(null,
new RandomAccessFileInStream(randomAccessFile));
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
//如果解压到的目标文件夹不存在,则新建
File destinationFolder = new File(destinationDir);
if (!destinationFolder.exists()) {
new File(destinationDir).mkdirs();
}
for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
final int[] hash = new int[]{0};
if (!item.isFolder()) {
ExtractOperationResult result;
final long[] sizeArray = new long[1];
//如果压缩文件自带文件夹,在这里面创建文件夹
//另外,如果压缩文件中有空文件夹,空文件夹是无法解压出来的。
if (item.getPath().indexOf(File.separator) > 0) {
String path = destinationDir + File.separator
+ item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));
File folderExisting = new File(path);
if (!folderExisting.exists()) {
new File(path).mkdirs();
}
}
//被解压的文件
OutputStream out = new FileOutputStream(destinationDir + File.separator + item.getPath());
//如果文件很大用这个方法可以解压完整,当然文件小用这个也没毛病
result = item.extractSlow(new ISequentialOutStream() {
@Override
public int write(final byte[] data) throws SevenZipException {
try {
out.write(data);
} catch (Exception e) {
e.printStackTrace();
}
hash[0] |= Arrays.hashCode(data);
sizeArray[0] += data.length;
return data.length;
}
});
out.close();
if (result == ExtractOperationResult.OK) {
// 打印执行的压缩包中内容信息,根据实际情况是否打印
//System.out.println(String.format("%9X | %10s | %s", //
// hash[0], sizeArray[0], item.getPath()));
} else {
System.out.println("Error extracting item: " + result);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inArchive != null) {
try {
inArchive.close();
} catch (SevenZipException e) {
System.err.println("Error closing archive: " + e);
e.printStackTrace();
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
System.err.println("Error closing file: " + e);
e.printStackTrace();
}
}
}
}
/**
* Junrar 解压rar文件,不支持Rar5版本
*
* @param rarFileInputStream
* @param destDir
*/
public static void unRar(InputStream rarFileInputStream, String destDir) throws IOException, RarException {
Junrar.extract(rarFileInputStream, new File(destDir));
}
/**
* Junrar 解压rar文件,不支持Rar5版本
* @param rarFile
* @param destDir
*/
public static void unRar(File rarFile, String destDir) throws IOException, RarException {
FileInputStream rarFileInputStream = new FileInputStream(rarFile);
Junrar.extract(rarFileInputStream, new File(destDir));
}
}