build.gradle
// un7z
compile 'org.apache.commons:commons-compress:1.15'
compile 'org.tukaani:xz:1.8'
package com.jdsoft.util;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Un7zipUtils {
/**
* 解压 7z 格式的压缩文件 (apache的common-compress)
* @param orgPath 源压缩文件地址
* @param tarPath 解压后存放的目录地址
*/
public static void apacheUn7Z(String orgPath, String tarPath) {
try {
SevenZFile sevenZFile = new SevenZFile(new File(orgPath));
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
if (entry.isDirectory()) {
new File(tarPath + File.separator + entry.getName()).mkdirs();
entry = sevenZFile.getNextEntry();
continue;
}
FileOutputStream out = new FileOutputStream(tarPath
+ File.separator + entry.getName());
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
entry = sevenZFile.getNextEntry();
}
sevenZFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}