Ziputil is the best compression and decompression tool class in history with code

Source: Internet
Author: User
Package COM. example. a; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. printwriter; import Java. security. messagedigest; import Java. util. date; import java.util.zip. zipentry; import java.util.zip. zipinputstream; Import java.util.zip. zipoutputstream; import android. OS. handler; import android. OS. message; /*** tool class for File compression and decompression * @ author MX */public class ziputil {/*** MD5 encrypted ZIP file * @ Param path: compressed file path * @ return */public static string ziptomd5 (string path) {stringbuffer MD5 = new stringbuffer (); fileinputstream FCM = NULL; try {file F = new file (PATH); If (! F. exists () {Throw new runtimeexception (path + "does not exist! ");} Messagedigest MD = messagedigest. getinstance ("MD5"); byte [] STR = new byte [2048]; int length =-1; FD = new fileinputstream (f ); try {While (length = Fi. read (STR ))! =-1) {Md. update (STR, 0, length);} byte [] result = md. digest (); For (INT I = 0; I <result. length; I ++) {md5.append (result [I]) ;}} catch (ioexception e) {e. printstacktrace () ;}} catch (exception e) {e. printstacktrace ();} finally {try {FCM. close ();} catch (ioexception e) {e. printstacktrace () ;}} return md5.tostring () ;}/ *** compress the file into a zip file * @ Param filepath source file path * @ Param zipfilepath ZIP file path */public static Void createzipfile (string filepath, string zipfilepath) {fileoutputstream Fos = NULL; zipoutputstream ZOS = NULL; try {Fos = new fileoutputstream (zipfilepath); ZOS = new zipoutputstream (FOS ); writezipfile (new file (filepath), ZOS, "");} catch (filenotfoundexception e) {e. printstacktrace ();} finally {try {If (ZOS! = NULL) ZOS. Close () ;}catch (ioexception e) {e. printstacktrace () ;}try {If (FOS! = NULL) FOS. close ();} catch (ioexception e) {e. printstacktrace () ;}} Private Static void writezipfile (file F, zipoutputstream ZOS, string hiberarchy) {If (F. exists () {If (F. isdirectory () {hiberarchy + = f. getname () + "/"; file [] fif = f. listfiles (); For (INT I = 0; I <fif. length; I ++) {writezipfile (FIF [I], ZOS, hiberarchy) ;}} else {fileinputstream FCM = NULL; try {FD = new fileinputstream (f); zipentry Ze = new zipentry (hiberarchy + F. getname (); ZOS. putnextentry (ze); byte [] B = new byte [1024]; while (FCM. read (B )! =-1) {ZOS. write (B); B = new byte [1024];} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {If (FS! = NULL) fiis. close ();} catch (ioexception e) {e. printstacktrace ();}}}}} /*** zip compression of the file based on the input stream * @ Param in file input stream * @ Param sdestpath the destination path of the ZIP file * @ Param myhandler UI update through handler */public static void ectract (final inputstream in, final string sdestpath, final handler myhandler) {New thread () {@ overridepublic void run () {try {int length = in. available (); zipinputstream Zins = new zipinputstream (in); zipent Ry ze = NULL; int COUNT = 0; byte ch [] = new byte [2048]; while (ze = Zins. getnextentry ())! = NULL) {file zfile = new file (sdestpath + Ze. getname (); file fpath = new file (zfile. getparentfile (). getpath (); If (Ze. isdirectory () {If (! Zfile. exists () zfile. mkdirs (); Zins. closeentry ();} else {If (! Fpath. exists () fpath. mkdirs (); fileoutputstream Fouts = new fileoutputstream (zfile); int I; while (I = Zins. Read (CH ))! =-1) {count + = I; int downloadprogressvalue = (INT) (float) count/length) * 100); message. obtain (myhandler, 0, downloadprogressvalue ). sendtotarget (); Fouts. write (CH, 0, I);} Zins. closeentry (); Fouts. close () ;}} message. obtain (myhandler, 1 ). sendtotarget (); In. close (); Zins. close ();} catch (exception e) {message. obtain (myhandler, 2 ). sendtotarget ();}}}. start ();}/*** decompress the ZIP file * @ Param szippathfile Z IP file path * @ Param sdestpath the decompressed file path * @ return * @ throws ioexception */public static Boolean ziptofile (string szippathfile, string sdestpath) throws ioexception {Boolean flag = false; fileinputstream fins = NULL; zipinputstream Zins = NULL; fileoutputstream Fouts = NULL; try {fins = new fileinputstream (szippathfile); Zins = new zipinputstream (FINS); zipentry ze = NULL; byte ch [] = new byte [2048]; while (ze = Zins. getnextentry ())! = NULL) {file zfile = new file (sdestpath + "//" + Ze. getname (); file fpath = new file (zfile. getparentfile (). getpath (); If (Ze. isdirectory () {If (! Zfile. exists () zfile. mkdirs (); Zins. closeentry ();} else {If (! Fpath. exists () fpath. mkdirs (); Fouts = new fileoutputstream (zfile); int I; string zfilepath = zfile. getabsolutepath (); While (I = Zins. read (CH ))! =-1) Fouts. write (CH, 0, I); Zins. closeentry (); If (zfilepath. endswith (". zip ") {ziptofile (zfilepath, zfilepath. substring (0, zfilepath. lastindexof (". zip ") ;}}// if you delete the ZIP file after decompression, you can file = new file (szippathfile); file. delete (); flag = true;} catch (exception e) {flag = false; E. printstacktrace ();} finally {If (Fouts! = NULL) Fouts. Close (); If (Zins! = NULL) Zins. Close (); If (fins! = NULL) fins. close () ;}return flag ;} /*** @ Param Delete the compressed package before date * @ Param f * @ Param base * @ throws exception */public static void delzip (date, string path) {try {long d = date. gettime (); file F = new file (PATH); file [] files = f. listfiles (); For (INT I = 0; I <files. length; I ++) {If (files [I]. isfile () {long f_date = files [I]. lastmodified (); If (f_date <D) {files [I]. delete () ;}}} catch (exception E) {E. printstacktrace () ;}/ *** copy file ** @ Param frompath * @ Param topath * @ throws ioexception */public static void copy (string frompath, string topath) throws ioexception {printwriter PFP = NULL; filereader Fr = NULL; bufferedreader BR = NULL; try {file F1 = new file (frompath); If (f1.isdirectory ()) {} else {// Fr = new filereader (frompath); BR = new bufferedreader (FR); string temp = ""; string line = NUL L; while (line = Br. Readline ())! = NULL) {temp + = line? File F2 = new file (topath); f2.mkdir (); file F3 = new file (F2, f1.getname (); PFP = new printwriter (F3); PFP. print (temp) ;}} catch (exception e) {e. getstacktrace ();} finally {If (PFP! = NULL) PFP. Close (); If (BR! = NULL) Br. Close (); If (fr! = NULL) Fr. Close ();}}}

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.