You can decompress the zip files that contain subdirectories.
/*** Decompression function. * decompress the zipFile file to the folderPath directory. * @ throws Exception */public int upZipFile (File zipFile, String folderPath) throws ZipException, IOException {// public static void upZipFile () throws Exception {ZipFile zfile = new ZipFile (zipFile); Enumeration zList = zfile. entries (); ZipEntry ze = null; byte [] buf = new byte [1024]; while (zList. hasMoreElements () {ze = (ZipEntry) zList. nextElement (); if (ze. isDirecto Ry () {Log. d ("upZipFile", "ze. getName () = "+ ze. getName (); String dirstr = folderPath + ze. getName (); // dirstr. trim (); dirstr = new String (dirstr. getBytes ("8859_1"), "GB2312"); Log. d ("upZipFile", "str =" + dirstr); File f = new File (dirstr); f. mkdir (); continue;} Log. d ("upZipFile", "ze. getName () = "+ ze. getName (); OutputStream OS = new BufferedOutputStream (new FileOutputStream (getRealFileName (folderPa Th, ze. getName (); InputStream is = new BufferedInputStream (zfile. getInputStream (ze); int readLen = 0; while (readLen = is. read (buf, 0, 1024 ))! =-1) {OS. write (buf, 0, readLen);} is. close (); OS. close ();} zfile. close (); Log. d ("upZipFile", "finishssssssssssssssssss"); return 0;}/*** returns the actual file name corresponding to a relative path for a given root directory. * @ param baseDir specify the root directory * @ param absFileName relative path name, from name * @ return java in ZipEntry. io. file the actual File */public static File getRealFileName (String baseDir, String absFileName) {String [] dirs = absFileName. split ("/"); File ret = new File (bas EDir); String substr = null; if (dirs. length> 1) {for (int I = 0; I <dirs. length-1; I ++) {substr = dirs [I]; try {// substr. trim (); substr = new String (substr. getBytes ("8859_1"), "GB2312");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace ();} ret = new File (ret, substr);} Log. d ("upZipFile", "1ret =" + ret); if (! Ret. exists () ret. mkdirs (); substr = dirs [dirs. length-1]; try {// substr. trim (); substr = new String (substr. getBytes ("8859_1"), "GB2312"); Log. d ("upZipFile", "substr =" + substr);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace ();} ret = new File (ret, substr); Log. d ("upZipFile", "2ret =" + ret); return ret ;}
Remember to add permissions in AndroidManifest. xml:
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>