Android(安卓)zip文件压缩解压缩
16lz
2022-06-16
http://www.oschina.net/code/snippet_4873_4142
DirTraversal.java
01 | package com.once; |
02 |
03 | import java.io.File; |
04 | import java.util.ArrayList; |
05 | import java.util.LinkedList; |
06 | /** |
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 | } |
[代码] ZipUtils.java
001 | package com.once; |
002 |
003 | import java.io.*; |
004 | import java.util.ArrayList; |
005 | import java.util.Collection; |
006 | import java.util.Enumeration; |
007 | import java.util.zip.ZipEntry; |
008 | import java.util.zip.ZipException; |
009 | import java.util.zip.ZipFile; |
010 | import java.util.zip.ZipOutputStream; |
011 |
012 | /** |
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) throws IOException { |
028 |
ZipOutputStream zipout = new ZipOutputStream( new BufferedOutputStream( new FileOutputStream( |
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( new FileOutputStream( |
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 |
* @param zipFile 压缩文件 |
145 |
* @return 压缩文件内文件名称 |
146 |
* @throws ZipException 压缩文件格式有误时抛出 |
147 |
* @throws IOException 当解压缩过程出错时抛出 |
148 |
*/ |
149 |
public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException { |
150 |
ArrayList<String> entryNames = new ArrayList<String>(); |
151 |
Enumeration<?> entries = getEntriesEnumeration(zipFile); |
152 |
while (entries.hasMoreElements()) { |
153 |
ZipEntry entry = ((ZipEntry)entries.nextElement()); |
154 |
entryNames.add( new String(getEntryName(entry).getBytes( "GB2312" ), "8859_1" )); |
155 |
} |
156 |
return entryNames; |
157 |
} |
158 |
159 |
/** |
160 |
* 获得压缩文件内压缩文件对象以取得其属性 |
161 |
* |
162 |
* @param zipFile 压缩文件 |
163 |
* @return 返回一个压缩文件列表 |
164 |
* @throws ZipException 压缩文件格式有误时抛出 |
165 |
* @throws IOException IO操作有误时抛出 |
166 |
*/ |
167 |
public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException, |
168 |
IOException { |
169 |
ZipFile zf = new ZipFile(zipFile); |
170 |
return zf.entries(); |
171 |
172 |
} |
173 |
174 |
/** |
175 |
* 取得压缩文件对象的注释 |
176 |
* |
177 |
* @param entry 压缩文件对象 |
178 |
* @return 压缩文件对象的注释 |
179 |
* @throws UnsupportedEncodingException |
180 |
*/ |
181 |
public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException { |
182 |
return new String(entry.getComment().getBytes( "GB2312" ), "8859_1" ); |
183 |
} |
184 |
185 |
/** |
186 |
* 取得压缩文件对象的名称 |
187 |
* |
188 |
* @param entry 压缩文件对象 |
189 |
* @return 压缩文件对象的名称 |
190 |
* @throws UnsupportedEncodingException |
191 |
*/ |
192 |
public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException { |
193 |
return new String(entry.getName().getBytes( "GB2312" ), "8859_1" ); |
194 |
} |
195 |
196 |
/** |
197 |
* 压缩文件 |
198 |
* |
199 |
* @param resFile 需要压缩的文件(夹) |
200 |
* @param zipout 压缩的目的文件 |
201 |
* @param rootpath 压缩的文件路径 |
202 |
* @throws FileNotFoundException 找不到文件时抛出 |
203 |
* @throws IOException 当压缩过程出错时抛出 |
204 |
*/ |
205 |
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) |
206 |
throws FileNotFoundException, IOException { |
207 |
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) |
208 |
+ resFile.getName(); |
209 |
rootpath = new String(rootpath.getBytes( "8859_1" ), "GB2312" ); |
210 |
if (resFile.isDirectory()) { |
211 |
File[] fileList = resFile.listFiles(); |
212 |
for (File file : fileList) { |
213 |
zipFile(file, zipout, rootpath); |
214 |
} |
215 |
} else { |
216 |
byte buffer[] = new byte [BUFF_SIZE]; |
217 |
BufferedInputStream in = new BufferedInputStream( new FileInputStream(resFile), |
218 |
BUFF_SIZE); |
219 |
zipout.putNextEntry( new ZipEntry(rootpath)); |
220 |
int realLength; |
221 |
while ((realLength = in.read(buffer)) != - 1 ) { |
222 |
zipout.write(buffer, 0 , realLength); |
223 |
} |
224 |
in.close(); |
225 |
zipout.flush(); |
226 |
zipout.closeEntry(); |
227 |
} |
228 |
} |
229 | } |
更多相关文章
- Android: Intent.ACTION_SEND分享文件
- Android对Window对象的管理机制分析
- Android的drawable文件夹的说明
- android 模拟按键单值/多值输入
- android图片压缩 —— 2
- Android(安卓)中设计模式 ----原型模式
- Android(安卓)Drawable绘图
- 类和 Json对象
- NPM 和webpack 的基础使用