Package COM. archfree. demo;/*** Android zip compression and decompression * @ author Ren. xia * @ version 1.0 * @ updated 26-August 26-August 2010 13:04:27 */public class xzip {public xzip () {}/ *** get the file list (folder, file (optional) * @ Param zipfilestring compressed package name * @ Param bcontainfolder whether to include a folder * @ Param bcontainfile whether to include a file * @ return * @ throws exception */public static Java. util. list
Getfilelist (string zipfilestring, Boolean bcontainfolder, Boolean bcontainfile) throws exception {Android. util. log. V ("xzip", "getfilelist (string)"); Java. util. List
Filelist = new java. util. arraylist
(;;Java.util.zip. zipinputstream inzip = new java.util.zip. zipinputstream (new example. zipentry; string szname = ""; while (zipentry = inzip. getnextentry ())! = NULL) {szname = zipentry. getname (); If (zipentry. isdirectory () {// get the folder Name of the widgetszname = szname. substring (0, szname. length ()-1); Java. io. file Folder = new Java. io. file (szname); If (bcontainfolder) {filelist. add (folder) ;}} else {Java. io. file file = new Java. io. file (szname); If (bcontainfile) {filelist. add (File) ;}}// end of whileinzip. close (); Return filelist;}/*** return the file input in the compressed package Stream * @ Param zipfilestring name of the compressed file * @ Param filestring name of the extracted file * @ return inputstream * @ throws exception */public static Java. io. inputstream upzip (string zipfilestring, string filestring) throws exception {android. util. log. V ("xzip", "upzip (string, string)" );java.util.zip. zipfile = new java.util.zip.zipfile(zipfilestring?;java.util.zip. zipentry = zipfile. getentry (filestring); Return Zipfile. getinputstream (zipentry );} /*** decompress a compressed file to the specified position * @ Param zipfilestring name * @ Param outpathstring path * @ throws exception */public static void unzipfolder (string zipfilestring, string outpathstring) throws exception {android. util. log. V ("xzip", "unzipfolder (string, string)" contains invalid java.util.zip. zipinputstream inzip = new java.util.zip. zipinputstream (New Java. io. fileinputstream (zipfilestring); j Ava.util.zip. zipentry; string szname = ""; while (zipentry = inzip. getnextentry ())! = NULL) {szname = zipentry. getname (); If (zipentry. isdirectory () {// get the folder Name of the widgetszname = szname. substring (0, szname. length ()-1); Java. io. file Folder = new Java. io. file (outpathstring + Java. io. file. separator + szname); folder. mkdirs ();} else {Java. io. file file = new Java. io. file (outpathstring + Java. io. file. separator + szname); file. createnewfile (); // get the output stream of th E filejava. io. fileoutputstream out = new Java. io. fileoutputstream (File); int Len; byte [] buffer = new byte [1024]; // read (LEN) bytes into bufferwhile (LEN = inzip. read (buffer ))! =-1) {// write (LEN) byte from buffer at the position 0out. write (buffer, 0, Len); out. flush ();} Out. close () ;}// end of whileinzip. close ();} // end of func/*** compressed file, folder * @ Param srcfilestring name of the file/folder to be compressed * @ Param zipfilestring specifies the compression destination and name * @ throws exception */public static void zipfolder (string srcfilestring, string zipfilestring) throws exception {android. util. log. V ("xzip", "zipfolder (string, string )"); // Create zippackjava.util.zip. zipoutputstream outzip = new java.util.zip. zipoutputstream (New Java. io. fileoutputstream (zipfilestring); // open the file to be output in Java. io. file file = new Java. io. file (srcfilestring); // compress zipfiles (file. getparent () + Java. io. file. separator, file. getname (), outzip); // complete, close outzip. finish (); outzip. close ();} // end of func/*** compressed file * @ Param folderstring * @ Param filestring * @ Param zipoutputsteam * @ t Hrows exception */Private Static void zipfiles (string folderstring, string filestring, java.util.zip. zipoutputstream zipoutputsteam) throws exception {android. util. log. V ("xzip", "zipfiles (string, String, zipoutputstream)"); If (zipoutputsteam = NULL) return; Java. io. file file = new Java. io. file (folderstring + filestring); // determines whether the file is an IF (file. isfile () implements java.util.zip. zipentry = new java.util.zip. zip Entry (filestring); Java. io. fileinputstream inputstream = new Java. io. fileinputstream (File); zipoutputsteam. putnextentry (zipentry); int Len; byte [] buffer = new byte [4096]; while (LEN = inputstream. read (buffer ))! =-1) {zipoutputsteam. write (buffer, 0, Len);} zipoutputsteam. closeentry ();} else {// folder method to obtain the sub-file string filelist [] = file in the folder. list (); // if no sub-file exists, add it to If (filelist. length new
Java.util.zip. zipentry (filestring + Java. io. file. separator); zipoutputsteam. putnextentry (zipentry); zipoutputsteam. closeentry () ;}// if a sub-file exists, traverse the sub-file for (INT I = 0; I // end of for} // end of if} // end of funcpublic void finalize () throws throwable {}}