Android zip file decompression (support Chinese)

After n days, when once again use the original blog:

Android zip file compression decompression

To do when the zip packet extracting, there are a lot of problems had not found. The first is the Chinese Chinese characters, using the Java zip package can not solve the problem of extracting; followed by

getRealFileName()The judge.: 
Before the "if (dirs.length > 1)" if it is > 1, for some files can not extract, modified to: 
if(dirs.length>0)

For zip compressed package that contains Chinese Chinese directory or file, refer to the online resources:

“Use the Apache Zip Toolkit (package ant.jar) instead of the JDK toolkit of zip, because the Java types do not support Chinese own path, but they use the same way, just Apache compression tool set encoding mode interface, the other is basically the same. ”

If Android want Apache need to import the ant.jar package, a download link below:

http://download.csdn.net/detail/shizhending/4139787

For the Android project to add the jar package also provides the entrance.:

http://hi.baidu.com/yore2003/item/fb6d5cd21a4bec4dddf9bef2

The above add jar to the attention of the next best is:

Right click the project ->properties->Java Build Path->Order and Export 

And then just added Libray jar.

The following are the specific source code using Apache zip Kit:

 1 public static void    unZipFile(String archive, String decompressDir)throws IOException, FileNotFoundException, ZipException 
 2     {
 3         BufferedInputStream bi;
 4         ZipFile zf = new ZipFile(archive, "GBK");
 5         Enumeration e = zf.getEntries();
 6         while (e.hasMoreElements()) 
 7         {
 8             ZipEntry ze2 = (ZipEntry) e.nextElement();
 9             String entryName = ze2.getName();
10             String path = decompressDir + "/" + entryName;
11             if (ze2.isDirectory()) 
12             {
13                 System.out.println("Creating unzip - " + entryName);
14                 File decompressDirFile = new File(path);
15                 if (!decompressDirFile.exists()) 
16                 {
17                     decompressDirFile.mkdirs();
18                 }
19             } else 
20             {
21                 System.out.println("Creating file decompression - " + entryName);
22                 String fileDir = path.substring(0, path.lastIndexOf("/"));
23                 File fileDirFile = new File(fileDir);
24                 if (!fileDirFile.exists())
25                 {
26                     fileDirFile.mkdirs();
27                 }
28                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(decompressDir + "/" + entryName));
29                 bi = new BufferedInputStream(zf.getInputStream(ze2));
30                 byte[] readContent = new byte[1024];
31                 int readCount = bi.read(readContent);
32                 while (readCount != -1) 
33                 {
34                     bos.write(readContent, 0, readCount);
35                     readCount = bi.read(readContent);
36                 }
37                 bos.close();
38                 }
39         }
40         zf.close();
41         //bIsUnzipFinsh = true;
42     }

Also remember to add permissions in AndroidManifest.xml.

Posted by Randy at November 17, 2013 - 5:46 PM