Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unzip a 7zip archive in Android?

I have a 7zip archive 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.

My problem is that the 7zip SDK doesn't contain a lot. I am looking for examples, tutorials and snippets regarding the decompression of 7z files.

(Decompression via Intent is only a secondary option)

like image 844
Wolkenjaeger Avatar asked Aug 08 '10 19:08

Wolkenjaeger


People also ask

Can you use 7-Zip on Android?

7-Zip is not available for Android but there are some alternatives with similar functionality. The best Android alternative is WinRAR. It's not free, so if you're looking for a free alternative, you could try B1 Free Archiver or Kudesnik Archiver.

Can Android unzip ZIP files?

Download and install Files by Google from the Play Store if it's not available on your device already. Open the app, then navigate to the folder containing your ZIP file (ZIP files have a . ZIP file extension). Tap the ZIP file and select Extract from the pop-up to start the process.


2 Answers

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.

like image 121
TienDC Avatar answered Oct 13 '22 19:10

TienDC


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.

like image 24
Simon Avatar answered Oct 13 '22 19:10

Simon