我有一个7zip
存档,其中包含数百个文件,这些文件分为不同的目录。目标是从 FTP 服务器下载它,然后在手机上提取它。
我的问题是7zip
SDK 不包含很多内容。我正在寻找有关解压缩 7z 文件的示例、教程和片段。
(通过解压Intent
只是次要选项)
去这里:
LZMA SDK 只提供编码器和解码器,用于对原始数据进行编码/解码,但 7z 存档是一种用于存储多个文件的复杂格式。
我发现这个页面提供了一个像魅力一样的替代方案。您只需添加compile 'org.apache.commons:commons-compress:1.8'
到您的构建 gradle 脚本并使用您想要的功能。对于这个问题,我做了以下事情:
AssetManager am = getAssets();
InputStream inputStream = null;
try {
inputStream = am.open("a7ZipedFile.7z");
File file1 = createFileFromInputStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
SevenZFile sevenZFile = null;
try{
File f = new File(this.getFilesDir(), "a7ZipedFile.7z");
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
try {
sevenZFile = new SevenZFile(f);
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
System.out.println(entry.getName());
FileOutputStream out = openFileOutput(entry.getName(), Context.MODE_PRIVATE);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
entry = sevenZFile.getNextEntry();
}
sevenZFile.close();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}catch (IOException e) {
//Logging exception
e.printStackTrace();
}
导入库的唯一缺点是大约 200k。除此之外,它真的很容易使用。