Android ZIP file encryption decompression and progress bar implementation

Source: Internet
Author: User

ZIP file decompression can use the Java Zip Library, but there is no implementation of the encryption file decompression function, here can be implemented using ZIP4J. Detailed can be seen in this article, "Android Zip compressed file encryption and decryption of the perfect solution." The file does not have the ability to implement the decompression progress, here is a simple implementation.

Zip4jsp.java

/** * Unzip file to dest dir with password in thread.            * * @param zipfile * @param dest * @param passwd * @param charset * null or empty is utf-8 * @param Handler * Handler in thread * @param isdeletezipfile * true:delete zip file.false:not. * @throws zipexception */public static void Unzip (Final File zipfile, String dest, String passwd,string charset, final Han Dler Handler, final Boolean isdeletezipfile) throws Zipexception {ZipFile zfile = new ZipFile (ZipFile); if (textutils.isemp Ty (CharSet)) {charset = "UTF-8";} Zfile.setfilenamecharset (CharSet), if (!zfile.isvalidzipfile ()) {throw new zipexception ("Compressed files is not Illegal, May is damaged. "); File DestDir = new file (dest); Unzip Directoryif (destdir.isdirectory () &&!destdir.exists ()) {Destdir.mkdir ();} if (zfile.isencrypted ()) {Zfile.setpassword (Passwd.tochararray ());} Final Progressmonitor progressmonitor = Zfile.getprogressmonitor (); Thread progressthread = new Thread (new Runnable () {@Overridepublic void Run () {bundle bundle = NULL; Message msg = null;try {int percentdone = 0;//long workcompleted=0;//handler.sendemptymessage (progressmonitor.result_ SUCCESS) if (handler = = null) {return;} Handler.sendemptymessage (Compressstatus.start); while (true) {Thread.Sleep (+);p Ercentdone = Progressmonitor.getpercentdone (); bundle = new Bundle (); Bundle.putint (Compresskeys.percent, percentdone); msg = new Message (); msg.what = Compressstatus.handling;msg.setdata (bundle); Handler.sendmessage (msg); if (PercentDone >= 100 ) {break;}} Handler.sendemptymessage (compressstatus.completed);} catch (Interruptedexception e) {bundle = new bundle (); bundle.putstring (Compresskeys.error, E.getmessage ()); msg = new Message (); msg.what = Compressstatus.error;msg.setdata (bundle); Handler.sendmessage (msg); E.printstacktrace ();} Finally{if (isdeletezipfile) {zipfile.deleteonexit ();//zipfile.delete ();}}}); Progressthread.start (); Zfile.setruninthread (true); Zfile.extractall (dest);}

Note:

(1), character set by default UTF-8

(2), unzip the file in the thread, so it needs to Setruninthread (true). Because it is extracted from a thread, it is equivalent to running asynchronously when called, and the calling code goes straight down, needing attention and handling.

(3), the progress bar opens another thread to process, and sends the processed result in the form of handler

(4), using Zipfile.deleteonexit () instead of Zipfile.delete (); Since the use of thread decompression, although the PercentDone obtained from Progressmonitor has reached 100, In fact, the data has not been completely decompressed complete. The exit loop executes the finally delete function, assuming that using zipfile.delete () will delete the file, which may cause the decompression to fail. With the Zipfile.deleteonexit () function, the function is to delete the file when the VM terminates, unlike the Zipfile.delete () delete file. When the app is executing, the VM is always on, so the removal ensures that the decompression may work. Or not to delete the file.

(5), handler code see the following text of the Mainactivity.java.


Compresskeys.java
Package Com.sparkle.compress;public class Compresskeys {public final static String percent= "PERCENT";p ublic final static String error= "ERROR";}


Compressstatus.java

Package Com.sparkle.compress;public class Compressstatus {public final static int start=0;public final static int handling =1;public final static int completed=2;public final static int error=3;}


Mainactivity.java
Private Handler _handler=new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case Compressstatus.start:{settitle ("START ..."); Case Compressstatus.handling:{bundle bundle=msg.getdata (); int percent=bundle.getint (compresskeys.percent); Settitle (percent+ "%"); Case Compressstatus.error:{bundle Bundle=msg.getdata (); String error=bundle.getstring (Compresskeys.error); _info_textview.settext (error); break;} Case Compressstatus.completed:{settitle ("completed"); byte[] Data=filesp.read (TempFilePath); try {String dataStr=new String (data, "UTF-8"); _info_textview.settext (DATASTR);} catch (Unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} break;} Default:break;}};};}

Below are




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.