android解压zip文件进度条,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;

}

};

};

}

以下是效果图

765fc5132c0dac8c3c1dd9e09cdf4d6c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 平台上解压 ZIP 文件,你可以使用以下代码示例: ```java import android.util.Log; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipUtils { private static final int BUFFER_SIZE = 4096; public static void unzip(String zipFilePath, String destDirectory) { try { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { extractFile(zipIn, filePath); } else { File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); } catch (Exception e) { Log.e("ZipUtils", "Error while unzipping file: " + e.getMessage()); } } private static void extractFile(ZipInputStream zipIn, String filePath) { try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } catch (Exception e) { Log.e("ZipUtils", "Error while extracting file: " + e.getMessage()); } } } ``` 使用上述代码,你只需调用 `unzip` 方法,传入 ZIP 文件的路径和解压目标目录的路径即可。注意确保你的应用已经获取了文件读写权限。 示例用法: ```java String zipFilePath = "/path/to/your/zip/file.zip"; String destDirectory = "/path/to/your/destination/directory"; ZipUtils.unzip(zipFilePath, destDirectory); ``` 将上述路径替换为你实际的文件路径和目标目录路径。 希望对你有所帮助!如有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值