android 解压zip中特定文件

 1 /**
 2      * 解压文件名包含传入文字的文件
 3      *
 4      * @param zipFile 压缩文件
 5      * @param folderPath 目标文件夹
 6      * @param nameContains 传入的文件匹配名
 7      * @throws ZipException 压缩格式有误时抛出
 8      * @throws IOException IO错误时抛出
 9      */
10     public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
11             String nameContains) throws ZipException, IOException {
12         ArrayList<File> fileList = new ArrayList<File>();
13   
14         File desDir = new File(folderPath);
15         if (!desDir.exists()) {
16             desDir.mkdir();
17         }
18   
19         ZipFile zf = new ZipFile(zipFile);
20         for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
21             ZipEntry entry = ((ZipEntry)entries.nextElement());
22             if (entry.getName().contains(nameContains)) {
23                 InputStream in = zf.getInputStream(entry);
24                 String str = folderPath + File.separator + entry.getName();
25                 str = new String(str.getBytes("8859_1"), "GB2312");
26                 // str.getBytes("GB2312"),"8859_1" 输出
27                 // str.getBytes("8859_1"),"GB2312" 输入
28                 File desFile = new File(str);
29                 if (!desFile.exists()) {
30                     File fileParentDir = desFile.getParentFile();
31                     if (!fileParentDir.exists()) {
32                         fileParentDir.mkdirs();
33                     }
34                     desFile.createNewFile();
35                 }
36                 OutputStream out = new FileOutputStream(desFile);
37                 byte buffer[] = new byte[BUFF_SIZE];
38                 int realLength;
39                 while ((realLength = in.read(buffer)) > 0) {
40                     out.write(buffer, 0, realLength);
41                 }
42                 in.close();
43                 out.close();
44                 fileList.add(desFile);
45             }
46         }
47         return fileList;
48     }

 

参考:http://www.itstrike.cn/Question/4f10bddd-2bb5-4e61-ad11-d22efa828861.html

 

posted @ 2016-03-09 18:15  海之涯2008  阅读(636)  评论(0)    收藏  举报