内容简介:嗨,你终于来啦~等你好久啦~从今天开始每天分享一篇Android需要了解的知识点,喜欢的小伙伴欢迎关注,我会定期分享Android知识点及解析,还会不断更新的BATJ面试专题,欢迎大家前来探讨交流,如有好的文章也欢迎投稿。
嗨,你终于来啦~
等你好久啦~
从今天开始每天分享一篇Android需要了解的知识点,喜欢的小伙伴欢迎关注,我会定期分享Android知识点及解析,还会不断更新的BATJ面试专题,欢迎大家前来探讨交流,如有好的文章也欢迎投稿。
最近会贡献一些平时开发中积累下来的 工具 类,希望能免去大家造轮子的体力活。
今天贡献一个解压缩zip文件的工具。
关键类
可以归纳起来所有跟文件操作相关的流程都要下面这几个类
- File
- InputStream
-
OutputStream
解压缩也一样,用于解压缩的工具类是这三个, - ZipFile
- ZipInputStream
- FileOutputStream
解压缩流程
首先实例化ZipFile对象,
通过InputStream读取流,ZipInputStream提供了一个方法
- getNextEntry()
返回一个 ZipEntry对象。这个对象所表示的意义是压缩/解压的文件信息,可以是文件夹,也可以是文件。
所以在获取ZipEntry的之后,如果用 getName()拿它的文件名的话,
会以路径+文件名的形式呈现。
比方我们现在有压缩文件 test.zip,
源文件 test/ 下面存在这么个层级结构,
test/a/b/c/file.txt
那么在解压缩的时候,我们拿到的file.txt的 ZipEntry的 name会是下面这样
a/b/c/file.txt
ZipEntry 的获取是在循环里不断迭代的,
而我们所获取到的 ZipEntry不仅会包括 file.txt文件,
同时也包括 a、b、c三个文件夹,
这也是为什么下面给出的工具类里会有这段代码的原因。
String fileName = zipEntry.getName();
if(fileName != null && fileName.contains(MAC_IGNORE)) {
continue;
}
File temp = new File(targetPath + File.separator + fileName);
if(zipEntry.isDirectory()) {
File dir = new File(targetPath + File.separator + fileName);
dir.mkdirs();
continue;
}
if (temp.getParentFile() != null && !temp.getParentFile().exists()) {
temp.getParentFile().mkdirs();
}
复制代码
完整代码
解压缩的思路在上面已经描述了,下面给出完整代码:
public class UnzipUtil {
//过滤在mac上压缩时自动生成的__MACOSX文件夹
private static final String MAC_IGNORE = "__MACOSX/";
public static void decompressFile(String target, String source) {
if(TextUtils.isEmpty(target)){
return;
}
try {
File file = new File(source);
if(!file.exists()) {
return;
}
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String fileName = zipEntry.getName();
if(fileName != null && fileName.contains(MAC_IGNORE)) {
continue;
}
File temp = new File(target + File.separator + fileName);
if(zipEntry.isDirectory()) {
File dir = new File(target + File.separator + fileName);
dir.mkdirs();
continue;
}
if (temp.getParentFile() != null && !temp.getParentFile().exists()) {
temp.getParentFile().mkdirs();
}
byte[] buffer = new byte[1024];
OutputStream os = new FileOutputStream(temp);
// 通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
InputStream is = zipFile.getInputStream(zipEntry);
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();
is.close();
}
zipInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Agile Web Development with Rails 4
Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2013-10-11 / USD 43.95
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details. Tens of thousands of deve......一起来看看 《Agile Web Development with Rails 4》 这本书的介绍吧!
随机密码生成器
多种字符组合密码
RGB HSV 转换
RGB HSV 互转工具