How to unzip a 7zip archive in Android?

你离开我真会死。 提交于 2019-12-01 04:35:23
TienDC

Go here:

LZMA SDK just provides the encoder and decoder for encoding/decoding the raw data, but 7z archive is a complex format for storing multiple files.

i found this page that provides an alternative that works like a charm. You only have to add 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 :

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!