decompress/compress 7z,zip,rar files in Android studio source

decompress/compress 7z,zip,rar files in Android studio source code

is a important topic in java and android studio apps development.

decompress/compress 7z,zip,rar files in Android studio source

This question is frequently asked that how to zip or unzip, pack or unpack, open or extract archived files programmatically in android?


Recently, due to the needs of the project, I need to understand the decompression function on the mobile platform. I decompress it on the mobile platform. I personally feel that it is not necessary. After all, the performance of the mobile phone is limited. However, it is not denied that the decompression function of the mobile terminal is necessary, because if for some resource manager applications, the access decompression function will be more complete, for example: resource manager, mobile QQ, UC browser, etc. Column application, involving the management and transmission of files, it is necessary to view the contents of the compressed package. So, if necessary, mobile applications still need to support the decompress/compress 7z,zip,rar files in Android studio source code function.

Compressed file introduction

Since you want to extract, you must first understand the compressed file. This article is only the first draft, so here are a few common compressed file formats, which will be added later.

  1. In zip format , ZIP is a compression algorithm for computer files, formerly known as Deflate (vacuum), invented by Phil Katz), who published the format in January 1989. ZIP usually uses the suffix “.zip” and its MIME format is application/zip . Currently, the ZIP format is one of several mainstream compression formats, and its competitors include the RAR format and the open source 7-Zip format. In terms of performance comparison, the RAR format has a higher compression ratio than the ZIP format, but its compression time is much higher than that of Zip. And 7-Zip is gradually being applied in more fields because it provides free compression tools.
  2. The rar format is also lossless data compression. The RAR file is usually higher than the [IP file compression ratio, but the compression speed is slower. Because the RAR file header also takes up a certain amount of space, when the data compression space is small, the compressed file may be larger than the original file.One of the main advantages of RAR is that it can split the file compression target into multiple files, and it is easy to extract the source file from such a split compressed file. The MIME format is application/x-rar-compressed .
  3. 7z format , 7z is a mainstream and efficient compression format, it has a very high compression ratio. In computer science, 7z is a file format that can be compressed using multiple compression algorithms. The format was originally implemented and adopted by 7-Zip, but the file format is public, and the 7-Zip software itself is open source under the GNU General Public License (GNU LGPL). The current version of the LZMA Software Development Kit is V9.34. The MIME type for the 7z format is application/x-7z-compressed .

Specifically introduce the method of decompressing each format

  • Zip format, zip format is our most common compressed file format, of course, is also the most easy to handle compression format, jdk itself supports the decompression of zip files, but jdk comes with decompression after all, there are certain limitations, we are sure There can be better solutions to deal with decompress/compress 7z,zip,rar files in Android studio source code.
    After decompiling some applications, I found that many of them are not using the decompression method provided by If it is a small file, it can be processed with this, but considering performance and efficiency, I decided to look for more. suitable method. zip4j
    Zip4j is a zip file compression and decompression library that is easy to use on java. It has powerful functions, such as compression and decompression, encryption, update, and removal. The interested children’s shoes can view the corresponding documents according to the links. Since it is a library of the java platform, then it is not a big problem to run on the mobile side. After all, the performance requirements of the zip decompression machine are not very high.
  • Rar format
    Rar format is one of the more troublesome formats, jdk is decompressed without rar format, so only open source libraries can be used for decompression. Say, the rar format is not public because the compression algorithm is not open, so we can’t compress files in general, and generally only the library for decompression. After exploration, I found a more efficient rar format decompression library, which is fully applicable on Android.  java-unrar
  • The 7z format is the toughest search for the 7z format. Because 7z is a highly compressed format, if you use java code to decompress, the efficiency is very inefficient, so I found several open source libraries, which are packaged into jar libraries using c/c++ code, and then Decompression, then the problem comes, if you use dynamic library, then the machine architecture and platform are different, applicable on windows and linux, not necessarily applicable to the Android platform. So I found it at first. 7-Zip-JBinding
    This open source library is very powerful. It uses the compression and decompression algorithm written in C++. It has the corresponding jar package for most platforms. However, there is no good support for Android. I tried to put all-window. When I imported the project, I knew that it was wrong. Looked at the results of the compilation, the problem is that because the library code at compile time, the machine does not have enough memory and performance to compile, resulting in direct throw errors, I looked for the problem on the stackoverflow, found that the memory problem is solved, but Inexplicably, there are another bunch of problems. I think this should be a problem with the platform architecture. It is estimated that I can’t solve it now.
    However, 7-Zip-JBinding inherits the spirit of open source and provides the source code of c. We can use this source code to develop jni. I found that this solution is still mentioned on stackoverflow.
    Friends in need can look at the problem of stackoverflow:

So the idea of ​​using jbinding is a bubble. Through the above problems, I found a solution to the idea of ​​**andro7z **, and also provided the corresponding c code. We need to compile ndk ourselves and package it into a dynamic library to execute.

But what is the better way?
After the boss’s guidance, I found a github ready-made project AndroidUn7zip , The author has compiled and packaged it into a dynamic library and a jar package, so if we need it, we can download the corresponding base library and run it or we can easily add it with dependencies to our apps:

dependencies {
    compile 'com.hzy:un7zip:1.3.0'

The specific use is as follows:
decompress/compress 7z,zip,rar files in Android studio source
Because it is the ndk method, we are more difficult to monitor the decompression process, but for this method that requires high-performance decompression, it is difficult to add progress.
Moreover, the author also decompresses other formats and can use the same library to decompress different formats.

Thank you very much for the guidance of the boss and the author.

There is also a 7z decompression method, which is to use the compressor open source library decompression, but also can be run on Android, but the actual use process is still a bit problem, and then have time to write a specific article to explain.

in conclusion

I think the whole process of inquiry is quite interesting. I have learned a lot of knowledge that I have not touched before. When I have time, I found perfect demo with decompression class library. Welcome to this decompression demo ( the language of this demo is chinese! ).

Demo structure:
decompress/compress 7z,zip,rar files in Android studio source

I hope the developer of this demo can improve it.


Leave a Reply

Your email address will not be published. Required fields are marked *