Android compression and decompression zip file

Source: Internet
Author: User

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"/>

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.