public class Ziputil {public Ziputil () {}/** * Decompress the ZIP to the path * @param zipfilestring name of ZI P * @param outpathstring path to be UnZIP * @throws Exception */public static void Unzipfolder (Strin G zipfilestring, String outpathstring) throws Exception {Zipinputstream inzip = new Zipinputstream (New FileInput Stream (zipfilestring)); ZipEntry ZipEntry; String szName = ""; System.out.println ("Start"); while ((ZipEntry = Inzip.getnextentry ()) = null) {SzName = Zipentry.getname (); System.out.println (SzName); if (Zipentry.isdirectory ()) {//Get the folder name of the widget SzName = Szname.subst Ring (0, Szname.length ()-1); File Folder = new file (outpathstring + file.separator + szName); Folder.mkdirs (); } else {File file = new file (outpathstring + FIle.separator + szName); File.createnewfile (); Get the output stream of the file FileOutputStream out = new FileOutputStream (file); int Len; byte[] buffer = new byte[1024]; Read (len) bytes to buffer while (len = inzip.read (buffer))! =-1) {//write (len) byte from buffer at the position 0 out.write (buffer, 0, Len); Out.flush (); } out.close (); }} System.out.println ("Ssssss"); Inzip.close (); }/** * Compress file and folder * @param srcfilestring file or folder to be Compress * @param zip Filestring the path name of result ZIP * @throws Exception */public static void Zipfolder (String srcfiles Tring, String zipfilestring) throws Exception {//create ZIP zipoutputstream oUtzip = new Zipoutputstream (new FileOutputStream (zipfilestring)); Create the file File = new file (srcfilestring); Compress Zipfiles (file.getparent () +file.separator, File.getname (), outzip); Finish and close outzip.finish (); Outzip.close (); }/** * Compress files * @param folderstring * @param filestring * @param zipoutputsteam * @throws Exception */private static void Zipfiles (String folderstring, String filestring, Zipoutputstream zipout Putsteam) throws exception{if (Zipoutputsteam = = null) return; File File = new file (folderstring+filestring); if (File.isfile ()) {ZipEntry zipentry = new ZipEntry (filestring); FileInputStream InputStream = new 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 String filelist[] = File.list (); No child file and compress if (filelist.length <= 0) {zipentry zipentry = new Zipentr Y (Filestring+file.separator); Zipoutputsteam.putnextentry (ZipEntry); Zipoutputsteam.closeentry (); }//child files and recursion for (int i = 0; i < filelist.length; i++) {Zi Pfiles (folderstring, Filestring+java.io.file.separator+filelist[i], zipoutputsteam); }//end of For}}/** * Return the InputStream of file in the ZIP * @param zipfilestring Name of ZIP * @param filestring name of file in the zip * @return InputStream * @throws exception */public static InputStream Upzip (String zipfilestring, String filestring) throws Exception {ZipFile ZipFile = new ZipFile (zipfilestring); ZipEntry zipentry = Zipfile.getentry (filestring); Return Zipfile.getinputstream (ZipEntry); }/** * Return files list (file and folder) in the zip * @param zipfilestring zip name * @param bcont Ainfolder contain folder or not * @param bcontainfile contain file or not * @return * @throws Excepti On */public static list<file> getfilelist (String zipfilestring, Boolean bcontainfolder, Boolean Bcontainf Ile) throws Exception {list<file> fileList = new arraylist<file> (); Zipinputstream inzip = new Zipinputstream (new FileInputStream (zipfilestring)); ZipEntry ZipEntry; String szName = ""; while ((ZipEntry = Inzip.getnextentry ()) = null) {SzName = Zipentry.getname (); if (ZipenTry.isdirectory ()) {//Get the folder name of the widget SzName = szname.substring (0, S Zname.length ()-1); File Folder = new file (szName); if (Bcontainfolder) {Filelist.add (folder); }} else {file file = new file (szName); if (bcontainfile) {filelist.add (file); }}} inzip.close (); return fileList; } }
Android_zip Decompression Tool