Android Zip 解压与进度 实现

if (mZipEntry.isDirectory()){// 如果是文件夹

pathFileName = pathFileName.substring(0,pathFileName.length() - 1);

//当层文件夹路经

currentPathFileName = outPath + File.separator + pathFileName;

//创建

new File(currentPathFileName).mkdirs();

}else {

//非文件夹直接显示

File file = new File(outPath + “/” + pathFileName);

if (!file.exists()){

file.getParentFile().mkdirs();

file.createNewFile();

}

}

}

}catch (FileNotFoundException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}

}

在这个代码中我们可以看到传入了两个参数,一个是文件的原始路径,一个是解压路径,接着,我将原始路径转为ZipInputStream后进行while循环,如果是文件夹则创建文件夹路径,不是文件夹则直接创建文件,此时如果运行的话,将会有解压功能,但是内容却是空的,因为我们的数据流还没有写入,同时我们也可以在写入的时候计算解压进度使用百分比显示,所以这里我们可以写一个接口或者其他呈现的方式,因为解压是耗时操作,我这里毕竟是demo就简单点写了:

/**

  • 解压

  • @param inPath

  • @param outPath

*/

private void unZip(String inPath,String outPath) {

long inPathFileZize = getZipFileSize(new File(inPath));

//*

if (inPathFileZize !=0){

tv_ZipFileSize.setText(“Zip大小:” + inPathFileZize);

}

//*

try {

//ZipInputStream:android提供的zip包输入流

//FileInputStream(inPath)😕/先读取文件

//ZipInputStream://将文件流变成zip输入流

ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(inPath));

if (zipInputStream == null || zipInputStream.available() == 0){

Toast.makeText(this, “Zip File Error!”, Toast.LENGTH_SHORT).show();

return;

}

ZipEntry mZipEntry;//zip实体

String pathFileName = “”;

while ((mZipEntry = zipInputStream.getNextEntry()) != null){

//获取到当层名称

pathFileName = mZipEntry.getName();

String currentPathFileName = “”;

//判断是否是文件夹

//*****

//*****

if (mZipEntry.isDirectory()){// 如果是文件夹

pathFileName = pathFileName.substring(0,pathFileName.length() - 1);

//当层文件夹路经

currentPathFileName = outPath + File.separator + pathFileName;

//创建

new File(currentPathFileName).mkdirs();

}else {

//非文件夹直接显示

File file = new File(outPath + “/” + pathFileName);

if (!file.exists()){

file.getParentFile().mkdirs();

file.createNewFile();

}

FileOutputStream out = new FileOutputStream(file);

int len;

long count = 0;

byte[] buffer = new byte[1024];

//读取(字节)字节到缓冲区

while ((len = zipInputStream.read(buffer)) != -1){

count +=len;

final int progress = (int)((count *100)/inPathFileZize);

runOnUiThread(new Runnable() {

@Override

public void run() {

tv_unzip_progress.setText(progress +“%”);

}

});

//从缓冲区(0)位置写入(字节)字节

out.write(buffer,0,len);

out.flush();

}

out.close();

//读取内容

readFileContent();

}

}

}catch (FileNotFoundException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}

}

仔细看我增加的问他,我通过FileOutputStream来写入数据,并且对进度进行了计算,当拷贝完成后我通过readFileContent函数将里面的内容读取出来,我们来看下readFileContent的代码:

private void readFileContent() {

File file = new File(“/sdcard/hello.txt”);

if (!file.exists()){

Toast.makeText(this, “文件不存在”, Toast.LENGTH_SHORT).show();

return;

}

try {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)

最后

愿你有一天,真爱自己,善待自己。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

视频,并且会持续更新!**

如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)

最后

愿你有一天,真爱自己,善待自己。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 18
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值