java 如何在 Android 中解压缩 7zip 存档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3435751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unzip a 7zip archive in Android?
提问by Wolkenjaeger
I have a 7ziparchive which contains some hundred files separated into different directories. The target is to download it from a FTP server and then extract it on the phone.
我有一个7zip存档,其中包含分成不同目录的数百个文件。目标是从FTP服务器下载它,然后在手机上解压。
My problem is that the 7zipSDK doesn't contain a lot. I am looking for examples, tutorials and snippets regarding the decompression of 7z files.
我的问题是7zipSDK 包含的内容不多。我正在寻找有关 7z 文件解压的示例、教程和片段。
(Decompression via Intentis only a secondary option)
(通过减压Intent方式只是次要选项)
回答by TienDC
回答by Simon
i found this pagethat provides an alternative that works like a charm. You only have to add compile 'org.apache.commons:commons-compress:1.8'
我发现这个页面提供了一个像魅力一样工作的替代方案。你只需要添加compile 'org.apache.commons:commons-compress:1.8'
to your build gradle script and use the feature you desire. For this issue i did the following :
到您的构建 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();
}
The only draw back is approximately 200k for the imported library. Other than that it is really easy to use.
对于导入的库,唯一的缺点是大约 200k。除此之外,它真的很容易使用。

