Android之zip文件加密解压及进度条的实现
zip文件的解压能够使用java的zip库,可是没有实现对加密文件的解压功能,这里能够使用zip4j来实现。详细能够參看该文《Android下zip压缩文件加密解密的完美解决方式》。该文件里没有实现解压进度的功能,这里进行一简单的实现。
Zip4jSp.java
/**
* unzip file to dest dir with password in thread.
*
* @param zipFile
* @param dest
* @param passwd
* @param charset
* null or empty is utf-8
* @param Handler
* handler in thread
* @param isDeleteZipFile
* true:delete zip file.false:not.
* @throws ZipException
*/
public static void Unzip(final File zipFile, String dest, String passwd,
String charset, final Handler handler, final boolean isDeleteZipFile)
throws ZipException {
ZipFile zFile = new ZipFile(zipFile);
if (TextUtils.isEmpty(charset)) {
charset = "UTF-8";
}
zFile.setFileNameCharset(charset);
if (!zFile.isValidZipFile()) {
throw new ZipException(
"Compressed files are not illegal, may be damaged.");
}
File destDir = new File(dest); // Unzip directory
if (destDir.isDirectory() && !destDir.exists()) {
destDir.mkdir();
}
if (zFile.isEncrypted()) {
zFile.setPassword(passwd.toCharArray());
} final ProgressMonitor progressMonitor = zFile.getProgressMonitor(); Thread progressThread = new Thread(new Runnable() { @Override
public void run() {
Bundle bundle = null;
Message msg = null;
try {
int percentDone = 0;
// long workCompleted=0;
// handler.sendEmptyMessage(ProgressMonitor.RESULT_SUCCESS)
if (handler == null) {
return;
}
handler.sendEmptyMessage(CompressStatus.START);
while (true) {
Thread.sleep(1000); percentDone = progressMonitor.getPercentDone();
bundle = new Bundle();
bundle.putInt(CompressKeys.PERCENT, percentDone);
msg = new Message();
msg.what = CompressStatus.HANDLING;
msg.setData(bundle);
handler.sendMessage(msg);
if (percentDone >= 100) {
break;
}
}
handler.sendEmptyMessage(CompressStatus.COMPLETED);
} catch (InterruptedException e) {
bundle = new Bundle();
bundle.putString(CompressKeys.ERROR, e.getMessage());
msg = new Message();
msg.what = CompressStatus.ERROR;
msg.setData(bundle);
handler.sendMessage(msg);
e.printStackTrace();
}
finally
{
if(isDeleteZipFile)
{
zipFile.deleteOnExit();//zipFile.delete();
}
}
}
}); progressThread.start();
zFile.setRunInThread(true);
zFile.extractAll(dest);
}
注:
(1)、字符集默认採用UTF-8
(2)、解压文件在线程中进行,所以须要setRunInThread(true).因为採用线程中解压文件,所以调用该函数时相当于异步运行,调用代码会直接往下走,须要注意并加以处理。
(3)、进度条另开了一个线程来处理,并将处理的结果以handler的形式发送
(4)、使用zipFile.deleteOnExit()而不是zipFile.delete();由于使用线程解压时,尽管从progressMonitor获得的percentDone已经达到了100,而其实数据并没有全然解压完毕。这时退出循环执行finally的delete函数,假设使用zipFile.delete(),将会删除文件,这样会使兴许的解压失败。而使用zipFile.deleteOnExit()函数,该函数是当VM终止时才会删除文件,与zipFile.delete()删除文件不同。APP在执行时,VM始终是在的,所以这样删除可以确保兴许的解压可以正常进行。或者不去删除文件。
(5)、handler的代码见后文的MainActivity.java。
CompressKeys.java
package com.sparkle.compress;
public class CompressKeys {
public final static String PERCENT="PERCENT";
public final static String ERROR="ERROR";
}
CompressStatus.java
package com.sparkle.compress;
public class CompressStatus {
public final static int START=0;
public final static int HANDLING=1;
public final static int COMPLETED=2;
public final static int ERROR=3;
}
MainActivity.java
private Handler _handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case CompressStatus.START:
{
setTitle("Start...");
break;
}
case CompressStatus.HANDLING:
{
Bundle bundle=msg.getData();
int percent=bundle.getInt(CompressKeys.PERCENT);
setTitle(percent+"%");
break;
}
case CompressStatus.ERROR:
{
Bundle bundle=msg.getData();
String error=bundle.getString(CompressKeys.ERROR);
_info_textView.setText(error);
break;
}
case CompressStatus.COMPLETED:
{
setTitle("Completed");
byte[] data=FileSp.read(tempFilePath);
try {
String dataStr=new String(data,"UTF-8");
_info_textView.setText(dataStr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
default:
break;
}
};
};
}
以下是效果图
Android之zip文件加密解压及进度条的实现的更多相关文章
- 「Python实用秘技01」复杂zip文件的解压
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的新系列文章「Python实用秘技」的第1 ...
- linux ubuntu12.04 解压中文zip文件,解压之后乱码
在windows下压缩后的zip包,在ubuntu下解压后显示为乱码问题 1.zip文件解压之后文件名乱码: 第一步 首先安装7zip和convmv(如果之前没有安装的话) 在命令行执行安装命令如下: ...
- php上传zip文件在线解压文件在指定目录下,CI框架版本
我从网上找的文件php在线解压zip压缩文件 文件为jy.php可以直接执行,但是怎样将其加到CI框架中呢?? jy.php文件 <?php header("content-Type: ...
- 如何把zip文件直接解压到内存里?
解压到硬盘再读进来耽误时间. var LZip: TZipFile; LMem: TMemoryStream; LBytes: TBytes;begin LZip := TZipFile.Cr ...
- java实现zip文件的解压
使用到的包 org.apache.commons 下载文件 url:文件所在地址需要是http:// filePath:将下载的文件保存的路径 public static void getDownlo ...
- Android下载压缩文件与解压案例
ackage com.example.jsontest.biz; import java.io.BufferedInputStream; import java.io.BufferedOutputSt ...
- .Net类库 压缩文件 与 Ionic.Zip 批量压缩不同目录文件与解压 文件
using System; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; us ...
- Android 下载zip压缩文件并解压
网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,在此记录一下下载zip文件并直接解压的方法. 其实也很简单,就是把下载文件和解压zip文件结合到一起.下面即代码: URLConne ...
- Java实现zip压缩文件的解压
需求描述: 前段时间写了一篇博客<Java实现对文本文件MD5加密并ftp传送到远程主机目录>,实现了一部分的业务需求.然而有些业务可能不止传送一个文件,有时候客户需要传多个文件,原有系统 ...
随机推荐
- 如何在vscode里面调试js和node.js
一般大家调试都是在浏览器端调试js的,不过有些时候也想和后台一样在代码工具里面调试js或者node.js,下面介绍下怎样在vscode里面走断点. 1,用来调试js 一:在左侧扩展中搜索Debugge ...
- 设计模式 UML & java code
A: 创造性模式 1. 工厂方法模式(FactoryMethod) 1.1 类图 1.2 代码1 public interface Pet { public String petSound(); } ...
- Excel 2016 for Mac
1. Excel for Mac 的 Developer tab下没有XML组,因此无法从xml导入或者导出到xml: 2. Excel for Mac 中没有Mark as Finnal的功能: 3 ...
- Android 黑色样式menu
效果图:
- Sql server 查询数据库中包含某字段的所有的表
我们有时候会需要查询数据库中包含某字段的所有的表,去进行update,这时就可以用下面的SQL来实现: select object_name(id) objName,Name as colName f ...
- activiti查询
一 1.根据当前任务id获得当前任务对象 Task task = processEngine.getTaskService().createTaskQuery().taskId(taskId).s ...
- qss 对子控件的设置样式 使用setProperty --Qt 之 QSS(动态属性)
https://blog.csdn.net/liang19890820/article/details/51693956 学习了 代码: 当鼠标划过控件时,设置样式 void CustomLabelW ...
- 对数损失函数(Logarithmic Loss Function)的原理和 Python 实现
原理 对数损失, 即对数似然损失(Log-likelihood Loss), 也称逻辑斯谛回归损失(Logistic Loss)或交叉熵损失(cross-entropy Loss), 是在概率估计上定 ...
- Mysql常用语句与函数(待续)
-- 查询语句select class from stu_info where sid=1000000102;select * from stu_info t where t.age=88; -- t ...
- Asp.Net MVC源码调试
首先下载MVC源代码,下载地址为:https://aspnetwebstack.codeplex.com/ 打开项目,卸载test文件夹下的所有项目和System.Web.WebPages.Admin ...