如何在Android上解压7zip归档文件?

6

我有一个包含几百个文件的7zip归档文件,这些文件被分成不同的目录。目标是从FTP服务器下载它,然后在手机上解压缩。

我的问题是7zip SDK 包含的内容很少。我正在寻找有关解压缩7z文件的示例、教程和代码片段。

(通过Intent进行解压缩只是次要选项)


7-Zip教程列表位于<a href="http://www.7-zip.org/links.html">此页面</a>底部。 - gregS
谢谢您的回答,但是7zip页面上的链接非常古老,对我来说无法使用。我将这个问题分成了另一个:https://dev59.com/80_Ta4cB1Zd3GeqPDbtj - Wolkenjaeger
你可能想要看一下AndroXplorer v.3。它支持许多文件格式,包括7z。 - Yongki
你尝试过这个解决方案吗 -> http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)? - Veaceslav Gaidarji
2个回答

2

请前往 这里

LZMA SDK仅提供编码/解码原始数据的编码器和解码器,但7z归档是一种用于存储多个文件的复杂格式。


你有任何演示项目吗? - Astha Garg

1
我发现这个页面提供了一个很好的替代方案。你只需要在你的构建gradle脚本中添加compile 'org.apache.commons:commons-compress:1.8',然后使用你想要的功能即可。对于这个问题,我做了以下操作:
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。除此之外,它非常易于使用。

似乎还需要 NDK。 - Bugs Happen
@错误总会发生,但不需要使用NDK。 - Muhammad Saqib
Apache 7zip和zip存在一个限制,即它们不支持加密。 - Astha Garg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接