The following small code is written by week. Ant. jar is used to compress files. The following is all the code. The interface is as you may like. If you have any questions, I hope you can point them out.
XML file code:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> <button Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: id = "@ + ID/backup" <br/> Android: text = "backup"/> <br/> <button Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: Id = "@ + ID/recover" <br/> Android: TEXT = "Recover"/> <br/> </linearlayout> <br/>
Android code:
Package COM. android. file; </P> <p> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. ioexception; <br/> Import java.util.zip. zipexception; </P> <p> Import com.android.file.zip. zipcontrol; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import Android. widget. button; </P> <p> public class fileandroidactivity extends activity implements onclicklistener {<br/> private final static string tag = "fileandroidactivity "; <br/> private string [] filesrcstrings; // specifies the compression source, which can be an array of directories or files <br/> private string decompressdirstring = ""; // extract path <br/> private string archivestring = ""; // compressed package path <br/> private string commentstring = "androi Java zip test. "; // compressed package comment <br/> PRI Vate zipcontrol mzipcontrol; <br/> private button mbackupbutton; <br/> private button mrecoverbutton; <br/> private string srcstring; // path of the first file <br/> private string srctwostring; // path of the Second file <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Mbackupbutton = (button) findviewbyid (R. id. backup); <br/> mrecoverbutton = (button) findviewbyid (R. id. recover); <br/> mbackupbutton. setonclicklistener (this); <br/> mrecoverbutton. setonclicklistener (this); <br/> string pathstring = This. getcachedir (). tostring (); <br/> log. E (TAG, "path is" + pathstring); <br/> archivestring = pathstring + "/zip"; <br/> decompressdirstring = pathstring + "/unzip "; <br/> F Ile zipfile = new file (archivestring); <br/> If (! Zipfile. exists () <br/>{< br/> zipfile. mkdir (); <br/> log. E (TAG, "Make zipdir success"); <br/>}< br/> else {<br/> log. E (TAG, "Exit zipdir"); <br/>}< br/> file unzipfile = new file (decompressdirstring); <br/> If (! Unzipfile. exists () <br/>{< br/> unzipfile. mkdir (); <br/> log. E (TAG, "Make undir success"); <br/>}< br/> else <br/>{< br/> log. E (TAG, "Exit undir"); <br/>}< br/> string strfileone = pathstring + "/fileonedir "; <br/> file fileonefile = new file (strfileone); <br/> If (! Fileonefile. exists () <br/>{< br/> fileonefile. mkdir (); <br/> log. E (TAG, "Make fileonedir success"); <br/>}< br/> else {<br/> log. E (TAG, "Exit fileonedir"); <br/>}< br/> srcstring = strfileone + "/testfile01.txt "; <br/> string strfiletwo = pathstring + "/filetwodir"; <br/> file fileonefiletwo = new file (strfiletwo); <br/> If (! Fileonefiletwo. exists () <br/>{< br/> fileonefiletwo. mkdir (); <br/> log. E (TAG, "Make filetwodir success"); <br/>}< br/> else {<br/> log. E (TAG, "Exit filetwodir"); <br/>}< br/> srctwostring = strfiletwo + "/testfile02.txt "; <br/> filesrcstrings = new string [] {srcstring, srctwostring}; <br/> mzipcontrol = new zipcontrol (); <br/>}< br/> @ override <br/> Public void onclick (view v) <br/>{< br/> int id = v. GETID (); <br/> switch (ID) <br/>{< br/> case R. id. backup: <br/> log. E (TAG, "Start backup"); <br/> try <br/> {<br/> mzipcontrol. writebyapachezipoutputstream (filesrcstrings, archivestring + "/test.zip", commentstring); <br/>}< br/> catch (filenotfoundexception e) <br/>{ <br/> log. E (TAG, E. tostring (); <br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (ioexception e) <br/>{< br/> log. E (TAG, E. tostring (); <br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> break; <br/> case R. id. recover: <br/> try <br/> {<br/> zipcontrol. readbyapachezipfile (archivestring + "/test.zip", decompressdirstring); <br/>}< br/> catch (filenotfoundexception e) <br/>{< br/> log. E (TAG, E. tostring (); <br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (zipexception e) <br/>{< br/> log. E (TAG, E. tostring (); <br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (ioexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> log. E (TAG, "Start recover"); <br/> break; <br/> default: <br/> break; <br/>}< br/>}
Main Code class for File compression:
Package com.android.file.zip; </P> <p> Import Java. io. bufferedinputstream; <br/> Import Java. io. bufferedoutputstream; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. util. enumeration; <br/> Import java.util.zip. CRC32; <br/> Import java.util.zip. checkedinputstre Am; <br/> Import java.util.zip. checkedoutputstream; <br/> Import java.util.zip. deflater; <br/> Import java.util.zip. zipexception; <br/> Import java.util.zip. zipinputstream; <br/> Import org.apache.tools.zip. zipentry; <br/> Import org.apache.tools.zip. zipfile; <br/> Import org.apache.tools.zip. zipoutputstream; </P> <p> Import android. util. log; <br/>/** <br/> * [one-sentence function description] <br> <br/> * [function description] <br/> * @ Author zhouxin <br/> * @ version [Android mtvclient c01, 2011-3-4] <br/> */<br/> public class zipcontrol <br/> {<br/> Private Static Boolean iscreatesrcdir = false; // specify whether to create the source directory. If you want to create a source directory. Set true here or false; <br/> Private Static string tag = "zipcontrol "; <br/>/** <br/> * [compression of files in the specified path] <br> <br/> * [function description] <br/> * @ Param SRC path <br/> * @ Param archive specifies the path to the compressed folder <br/> * @ Param comment description <br /> * @ throws filenotfoundexception file no exception found <br/> * @ throws ioexception Io input exception <br/> */<br/> Public void writebyapachezipoutputstream (string [] SRC, <br/> string archive, string Comment) throws filenotfoundexception, <br/> ioexception <br/>{< br/> log. E (TAG, "writebyapachezipoutputstream"); <br/> // ---- compressed file: <br/> fileoutputstream F = new fileoutputstream (archive ); <br/> // use the specified checksum to create the output stream <br/> checkedoutputstream csum = new checkedoutputstream (F, new CRC32 ()); <br/> zipoutputstream ZOS = new zipoutputstream (csum); <br/> // Chinese supported <br/> ZOS. setencoding ("GBK"); <br/> bufferedo Utputstream out = new bufferedoutputstream (ZOS); <br/> // set the compressed package annotation <br/> ZOS. setcomment (comment); <br/> // enable compression <br/> ZOS. setmethod (zipoutputstream. deflated); <br/> // The compression level is the strongest, but it takes a little longer <br/> ZOS. setlevel (Deflater. best_compression); <br/> // if the file is compressed, modify it here <br/> for (INT I = 0; I <SRC. length; I ++) <br/>{< br/> log. E (TAG, "src [" + I + "] is" + SRC [I]); <br/> file srcfile = new file (SRC [I]); <br/> If (! Srcfile. exists () <br/> | (srcfile. isdirectory () & srcfile. list (). length = 0) <br/>{< br/> log. E (TAG ,"! Srcfile. exists () "); <br/> throw new filenotfoundexception (<br/>" file must exist and zip file must have at least one entry. "); <br/>}< br/> string strsrcstring = SRC [I]; <br/> // obtain the parent directory of the compression source. <br/> strsrcstring = strsrcstring. replaceall ("//", "/"); <br/> string prefixdir = NULL; <br/> If (srcfile. isfile () <br/>{< br/> prefixdir = strsrcstring. substring (0, strsrcstring <br/>. lastindexof ("/") + 1 ); <Br/>}< br/> else <br/>{< br/> prefixdir = (strsrcstring. replaceall ("/$", "") + "/"); <br/>}< br/> // if it is not the root directory <br/> If (prefixdir. indexof ("/")! = (Prefixdir. length ()-1) <br/> & iscreatesrcdir) <br/>{< br/> prefixdir = prefixdir. replaceall ("[^/] +/$", ""); <br/>}< br/> // start compression <br/> writerecursive (ZOS, out, srcfile, prefixdir); <br/>}</P> <p> out. close (); <br/> // Note: The Checksum must be prepared after the stream is closed. Be sure to use it after the stream is closed <br/> log. E (TAG, "checksum:" + csum. getchecksum (). getvalue (); <br/> @ suppresswarnings ("UNUSED") <br/> bufferedinputstream Bi; <br/>}< br/>/** <br /> * <Br/> * [* use org.apache.tools.zip. zipfile: Decompress the file and <br/> * java.util.zip. zipfile is a new method of usage, but it only adds an interface for setting the encoding method. <Br/> * Note: Apache does not provide the zipinputstream class. Therefore, you can only use the zipfile provided by Apache to read compressed files.] <Br> <br/> * @ Param archive path <br/> * @ Param decompressdir decompression path <br/> * @ throws ioexception <br/> * @ throws filenotfoundexception <br/> * @ throws zipexception <br/> */</P> <p> @ suppresswarnings ("unchecked ") <br/> Public static void readbyapachezipfile (string archive, string decompressdir) <br/> throws ioexception, filenotfoundexception, zipexception <br/>{< br/> log. E (TAG, "readbyapache Zipfile "); <br/> bufferedinputstream Bi; <br/> zipfile ZF = new zipfile (archive," GBK "); // Chinese supported <br/> enumeration E = ZF. getentries (); <br/> while (E. hasmoreelements () <br/>{< br/> zipentry ze2 = (zipentry) E. nextelement (); <br/> string entryname = ze2.getname (); <br/> string Path = decompressdir + "/" + entryname; <br/> If (ze2.isdirectory ()) <br/>{< br/> log. E (TAG, "creating extract directory-" + entryname); <Br/> file decompressdirfile = new file (PATH); <br/> If (! Decompressdirfile. exists () <br/>{< br/> decompressdirfile. mkdirs (); <br/>}< br/> else <br/>{< br/> log. E (TAG, "creating decompression file-" + entryname); <br/> string filedir = path. substring (0, path. lastindexof ("/"); <br/> file filedirfile = new file (filedir); <br/> If (! Filedirfile. exists () <br/>{< br/> filedirfile. mkdirs (); <br/>}< br/> bufferedoutputstream Bos = new bufferedoutputstream (<br/> New fileoutputstream (decompressdir + "/" + entryname )); <br/> Bi = new bufferedinputstream (ZF. getinputstream (ze2); <br/> byte [] readcontent = new byte [1024]; <br/> int readcount = Bi. read (readcontent); <br/> while (readcount! =-1) <br/>{< br/> Bos. write (readcontent, 0, readcount); <br/> readcount = Bi. read (readcontent); <br/>}< br/> Bos. close (); <br/>}< br/> ZF. close (); <br/>}< br/>/** <br/> * [decompress the file using the zipinputstream class in Java API, however, if <br/> * org.apache.tools.zip is used for compression. <br/> * java.util.zip. zipoutputstream, this method cannot be used because of inconsistent encoding methods. The following exception is thrown during runtime: <br/> * Java. lang. illegalarg Umentexception at <br/> * java.util.zip. zipinputstream. getutf8string (zipinputstream. java: 290) <br/> * Of course, if the compressed package uses java.util.zip of the Java library. zipoutputstream compression is not a problem, however, it does not support Chinese characters] <br> <br/> * [function description] <br/> * @ Param archive compressed package path <br/> *@ param decompressdir decompression path <br/> * @ throws filenotfoundexception <br/> * @ throws ioexception <br/> */<br/> Public static void readbyzipinputstream (string Archive, string decompressdir) <br/> throws filenotfoundexception, ioexception <br/>{< br/> bufferedinputstream Bi; <br/> // ---- decompress the file (the decompressed ZIP file actually reads data from the input stream): <br/> log. E (TAG, "Start reading compressed files"); <br/> fileinputstream Fi = new fileinputstream (archive); <br/> checkedinputstream csumi = new checkedinputstream (FI, new CRC32 (); <br/> zipinputstream in2 = new zipinputstream (csumi); <br/> Bi = new bufferedin Putstream (in2); <br/> java.util.zip. zipentry ze; // compressed file entries <br/> // traverse file entries in the compressed package <br/> while (ze = in2.getnextentry ())! = NULL) <br/>{< br/> string entryname = Ze. getname (); <br/> If (Ze. isdirectory () <br/>{< br/> log. E (TAG, "creating extract directory-" + entryname); <br/> file decompressdirfile = new file (decompressdir + "/" <br/> + entryname ); <br/> If (! Decompressdirfile. exists () <br/>{< br/> decompressdirfile. mkdirs (); <br/>}< br/> else <br/>{< br/> log. E (TAG, "Creating an decompression file-" + entryname ); <br/> bufferedoutputstream Bos = new bufferedoutputstream (<br/> New fileoutputstream (decompressdir <br/> + "/" <br/> + entryname. substring (entryname. lastindexof ("//"), <br/> entryname. length () <br/>-(entryname. lastindexof ("//")-2); <br/> byte [] Buffer = new byte [1024]; <br/> int readcount = Bi. Read (buffer); <br/> while (readcount! =-1) <br/>{< br/> Bos. write (buffer, 0, readcount); <br/> readcount = Bi. read (buffer); <br/>}< br/> Bos. close (); <br/>}< br/> bi. close (); <br/> log. E (TAG, "checksum:" + csumi. getchecksum (). getvalue ()); <br/>}</P> <p>/** <br/> * [recursive compression <br/> * use org.apache.tools.zip. zipoutputstream class for compression. The advantage of zipoutputstream class is that it supports Chinese paths, while <br/> * java.util.zip in Java class library. when zipoutputstream compresses the Chinese file name, garbled characters may occur in the compressed package. Using this class in Apache and Java <br/> * The usage in the class library is new, but the encoding method can be set.] <Br> <br/> * [Detailed function description] <br/> * @ Param ZOS <br/> * @ Param Bo <br/> *@ param srcfile <br/> * @ Param prefixdir <br/> * @ throws ioexception <br/> * @ throws filenotfoundexception <br/> */<br/> Private Static void writerecursive (zipoutputstream ZOS, <br/> bufferedoutputstream Bo, file srcfile, string prefixdir) <br/> throws ioexception, filenotfoundexception <br/> {<br/> log. E (TAG, "writerecursi Ve "); <br/> zipentry; <br/> string filepath = srcfile. getabsolutepath (). replaceall ("///", "/") <br/>. replaceall ("//", "/"); <br/> If (srcfile. isdirectory () <br/>{< br/> filepath = filepath. replaceall ("/$", "") + "/"; <br/>}< br/> string entryname = filepath. replace (prefixdir ,""). replaceall ("/$", ""); <br/> If (srcfile. isdirectory () <br/>{< br/> If (! "". Equals (entryname) <br/>{< br/> log. E (TAG, "creating directory-" + srcfile. getabsolutepath () <br/> + "entryname =" + entryname); <br/> // if it is a directory, add/<br/> zipentry = new zipentry (entryname + "/") after the write directory; <br/> ZOS. putnextentry (zipentry); <br/>}< br/> file srcfiles [] = srcfile. listfiles (); <br/> for (INT I = 0; I <srcfiles. length; I ++) <br/>{< br/> writerecursive (ZOS, Bo, srcfiles [I], prefixdir); <br/ >}< Br/> else <br/>{< br/> log. E (TAG, "Writing File-" + srcfile. getabsolutepath () <br/> + "entryname =" + entryname); <br/> bufferedinputstream Bi = new bufferedinputstream (<br/> New fileinputstream (srcfile )); <br/> // start to write a new zip file entry and locate the stream at the beginning of the entry data. <br/> zipentry = new zipentry (entryname ); <br/> ZOS. putnextentry (zipentry); <br/> byte [] buffer = new byte [1024]; <br/> int readcount = Bi. read (buff Er); <br/> while (readcount! =-1) <br/>{< br/> Bo. write (buffer, 0, readcount); <br/> readcount = Bi. read (buffer); <br/>}< br/> // Note: when using a buffer stream to write a compressed file, you must refresh the file after one condition is complete, no <br/> // some contents may be stored in the following entries. <br/> Bo. flush (); <br/> // close the file after reading it <br/> bi. close (); <br/>}< br/>