Android decompress the zip file

Source: Internet
Author: User

After more than n days, when I use the original blog article again:

Android compression and decompression zip file
When I decompressed the zip package, many problems were not found. The first is the problem of Chinese characters. The java zip package cannot solve the problem of decompression.

The value in getRealFileName () is determined by the previous "if (dirs. length> 1) "if it is> 1, some files cannot be decompressed and changed to: if (dirs. length> 0) if the zip package contains a Chinese directory or a Chinese file, refer to the resources on the Internet: "Use apache zip Toolkit (the package is ant. jar) replaces the JDK zip toolkit, because the java type does not support Chinese paths, but the two are used in the same way, but the apache compression tool has an interface for setting the encoding method, others are basically the same." If you want to use apache in android, You need to import the ant. jar package, right-click the project-> properties-> Java Build Path-> Order and Export, and set the Libray jar you just added to the top. The following is the specific source code for using the apache zip Toolkit:
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 extract directory-" + entryName );
14 File decompressDirFile = new File (path );
15 if (! DecompressDirFile. exists ())
16 {
17 decompressDirFile. mkdirs ();
18}
19} else
20 {
21 System. out. println ("creating decompression file-" + 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 [1, 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.

 

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.