Android zip文件压缩解压缩

/**
07  *  文件夹遍历
08  * @author once
09  *
10  */
11 public class DirTraversal {
12      
13     //no recursion
14     public static LinkedList<File> listLinkedFiles(String strPath) {
15         LinkedList<File> list = new LinkedList<File>();
16         File dir = new File(strPath);
17         File file[] = dir.listFiles();
18         for (int i = 0; i < file.length; i++) {
19             if (file[i].isDirectory())
20                 list.add(file[i]);
21             else
22                 System.out.println(file[i].getAbsolutePath());
23         }
24         File tmp;
25         while (!list.isEmpty()) {
26             tmp = (File) list.removeFirst();
27             if (tmp.isDirectory()) {
28                 file = tmp.listFiles();
29                 if (file == null)
30                     continue;
31                 for (int i = 0; i < file.length; i++) {
32                     if (file[i].isDirectory())
33                         list.add(file[i]);
34                     else
35                         System.out.println(file[i].getAbsolutePath());
36                 }
37             else {
38                 System.out.println(tmp.getAbsolutePath());
39             }
40         }
41         return list;
42     }
43  
44      
45     //recursion
46     public static ArrayList<File> listFiles(String strPath) {
47         return refreshFileList(strPath);
48     }
49  
50     public static ArrayList<File> refreshFileList(String strPath) {
51         ArrayList<File> filelist = new ArrayList<File>();
52         File dir = new File(strPath);
53         File[] files = dir.listFiles();
54  
55         if (files == null)
56             return null;
57         for (int i = 0; i < files.length; i++) {
58             if (files[i].isDirectory()) {
59                 refreshFileList(files[i].getAbsolutePath());
60             else {
61                 if(files[i].getName().toLowerCase().endsWith("zip"))
62                     filelist.add(files[i]);
63             }
64         }
65         return filelist;
66     }
67 }





/**
013  * Java utils 实现的Zip工具
014  *
015  * @author once
016  */
017 public class ZipUtils {
018     private static final int BUFF_SIZE = 1024 1024// 1M Byte
019  
020     /**
021      * 批量压缩文件(夹)
022      *
023      * @param resFileList 要压缩的文件(夹)列表
024      * @param zipFile 生成的压缩文件
025      * @throws IOException 当压缩过程出错时抛出
026      */
027     public static void zipFiles(Collection<File> resFileList, File zipFile) throwsIOException {
028         ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(newFileOutputStream(
029                 zipFile), BUFF_SIZE));
030         for (File resFile : resFileList) {
031             zipFile(resFile, zipout, "");
032         }
033         zipout.close();
034     }
035  
036     /**
037      * 批量压缩文件(夹)
038      *
039      * @param resFileList 要压缩的文件(夹)列表
040      * @param zipFile 生成的压缩文件
041      * @param comment 压缩文件的注释
042      * @throws IOException 当压缩过程出错时抛出
043      */
044     public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
045             throws IOException {
046         ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(newFileOutputStream(
047                 zipFile), BUFF_SIZE));
048         for (File resFile : resFileList) {
049             zipFile(resFile, zipout, "");
050         }
051         zipout.setComment(comment);
052         zipout.close();
053     }
054  
055     /**
056      * 解压缩一个文件
057      *
058      * @param zipFile 压缩文件
059      * @param folderPath 解压缩的目标目录
060      * @throws IOException 当解压缩过程出错时抛出
061      */
062     public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
063         File desDir = new File(folderPath);
064         if (!desDir.exists()) {
065             desDir.mkdirs();
066         }
067         ZipFile zf = new ZipFile(zipFile);
068         for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
069             ZipEntry entry = ((ZipEntry)entries.nextElement());
070             InputStream in = zf.getInputStream(entry);
071             String str = folderPath + File.separator + entry.getName();
072             str = new String(str.getBytes("8859_1"), "GB2312");
073             File desFile = new File(str);
074             if (!desFile.exists()) {
075                 File fileParentDir = desFile.getParentFile();
076                 if (!fileParentDir.exists()) {
077                     fileParentDir.mkdirs();
078                 }
079                 desFile.createNewFile();
080             }
081             OutputStream out = new FileOutputStream(desFile);
082             byte buffer[] = new byte[BUFF_SIZE];
083             int realLength;
084             while ((realLength = in.read(buffer)) > 0) {
085                 out.write(buffer, 0, realLength);
086             }
087             in.close();
088             out.close();
089         }
090     }
091  
092     /**
093      * 解压文件名包含传入文字的文件
094      *
095      * @param zipFile 压缩文件
096      * @param folderPath 目标文件夹
097      * @param nameContains 传入的文件匹配名
098      * @throws ZipException 压缩格式有误时抛出
099      * @throws IOException IO错误时抛出
100      */
101     public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
102             String nameContains) throws ZipException, IOException {
103         ArrayList<File> fileList = new ArrayList<File>();
104  
105         File desDir = new File(folderPath);
106         if (!desDir.exists()) {
107             desDir.mkdir();
108         }
109  
110         ZipFile zf = new ZipFile(zipFile);
111         for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
112             ZipEntry entry = ((ZipEntry)entries.nextElement());
113             if (entry.getName().contains(nameContains)) {
114                 InputStream in = zf.getInputStream(entry);
115                 String str = folderPath + File.separator + entry.getName();
116                 str = new String(str.getBytes("8859_1"), "GB2312");
117                 // str.getBytes("GB2312"),"8859_1" 输出
118                 // str.getBytes("8859_1"),"GB2312" 输入
119                 File desFile = new File(str);
120                 if (!desFile.exists()) {
121                     File fileParentDir = desFile.getParentFile();
122                     if (!fileParentDir.exists()) {
123                         fileParentDir.mkdirs();
124                     }
125                     desFile.createNewFile();
126                 }
127                 OutputStream out = new FileOutputStream(desFile);
128                 byte buffer[] = new byte[BUFF_SIZE];
129                 int realLength;
130                 while ((realLength = in.read(buffer)) > 0) {
131                     out.write(buffer, 0, realLength);
132                 }
133                 in.close();
134                 out.close();
135                 fileList.add(desFile);
136             }
137         }
138         return fileList;
139     }
140  
141     /**
142      * 获得压缩文件内文件列表
143      *
144      

你可能感兴趣的:(Android zip文件压缩解压缩)