依赖:
net.sf.sevenzipjbinding
sevenzipjbinding
16.02-2.01
net.sf.sevenzipjbinding
sevenzipjbinding-all-platforms
16.02-2.01
方法:
public static void main(String[] args) throws Exception{
// decompressFile(“D:\testZip.rar”,“D:\aaa\testZip”);
// decompressFile(“D:\testRar.rar”,“D:\aaa\testRar”);
decompressFile(“D:\test7z.7z”,“D:\aaa\test7z”);
//获取文件夹下所有文件名称
File folder = new File("D:\\aaa\\testRar\\testRar");
File[] files = folder.listFiles();
List<String> fileNames=new ArrayList<>();
List<String> foldernames=new ArrayList<>();
for (File file : files) {
if (file.isFile()) {
fileNames.add(file.getName());
} else {
foldernames.add(file.getName());
}
}
Map<String,List<String>> map=new HashMap();
map.put("file",fileNames);
map.put("folder",foldernames);
}
//zip、rar、7z三合一
public static void decompressFile(String inputFilePath,final String targetFileDir) {
//解压7zip文件
RandomAccessFile randomAccessFile = null;
IInArchive inArchive = null;
try {
// 判断目标目录是否存在,不存在则创建
File newdir = new File(targetFileDir);
if (false == newdir.exists()) {
newdir.mkdirs();
newdir = null;
}
randomAccessFile = new RandomAccessFile(inputFilePath, "r");
RandomAccessFileInStream t = new RandomAccessFileInStream(randomAccessFile);
if (inputFilePath.endsWith("rar")) {
inArchive = SevenZip.openInArchive(ArchiveFormat.RAR5, t);
} else if (inputFilePath.endsWith("7z")) {
inArchive = SevenZip.openInArchive(ArchiveFormat.SEVEN_ZIP, t);
} else {
inArchive = SevenZip.openInArchive(ArchiveFormat.ZIP, t);
}
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
final int[] hash = new int[]{0};
System.out.println("item.isFolder()==" + item.isFolder()+"---1");
if (!item.isFolder()) {
ExtractOperationResult result;
final long[] sizeArray = new long[1];
result = item.extractSlow(new ISequentialOutStream() {
public int write(byte[] data) throws SevenZipException {
//写入指定文件
FileOutputStream fos;
try {
if (item.getPath().indexOf(File.separator) > 0) {
String path = targetFileDir + File.separator + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));
File folderExisting = new File(path);
if (!folderExisting.exists()) {
new File(path).mkdirs();
}
}
fos = new FileOutputStream(targetFileDir + File.separator + item.getPath(), true);
fos.write(data);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
hash[0] ^= Arrays.hashCode(data); // Consume data
sizeArray[0] += data.length;
return data.length; // Return amount of consumed data
}
});
if (result == ExtractOperationResult.OK) {
System.out.println(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath())+"---2");
} else {
System.err.println("Error extracting item: " + result+"---3");
}
}else {
//创建空文件夹
String filepath=targetFileDir + File.separator + item.getPath();
new File(filepath).mkdirs();
}
}
} catch (Exception e) {
System.err.println("Error occurs: " + e);
e.printStackTrace();
System.exit(1);
} finally {
if (inArchive != null) {
try {
inArchive.close();
} catch (SevenZipException e) {
System.err.println("Error closing archive: " + e);
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
System.err.println("Error closing file: " + e);
}
}
}
}