Android实现文件压缩与解压工具类
更新时间:2024年04月24日 09:40:44 作者:generallizhong
这篇文章主要为大家详细介绍了如何使用Android实现一个文件压缩与解压工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
一个简单压缩解压工具类
public class ZipUtils {
public static void compressFolder(Activity activity, String sourcePath, String zipFile) throws IOException {
File folder = new File(sourcePath);
File zipPath = new File(zipFile);
if (zipPath.exists()) {
zipPath.delete();
}
// 创建输出流并指定要生成的ZIP文件路径
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
addFilesToZip(folder, folder.getName(), zos);
System.out.println("文件夹已成功压缩为 " + zipFile);
Toast.makeText(activity, "压缩已完成", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
throw new RuntimeException("无法将文件夹压缩到ZIP文件中", e);
}
}
private static void addFilesToZip(File file, String parentName, ZipOutputStream zos) throws IOException {
if (!file.exists()) return;
for (File child : file.listFiles()) {
if (child.isDirectory()) {
addFilesToZip(child, parentName + "/" + child.getName(), zos);
} else {
byte[] buffer = new byte[1024];
// 获取当前文件相对于源文件夹的路径名称
String entryName = parentName + "/" + child.getName();
// 添加新条目到ZIP文件中
zos.putNextEntry(new ZipEntry(entryName));
// 读取文件内容并写入ZIP文件
try (InputStream is = new FileInputStream(child)) {
int length;
while ((length = is.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
} finally {
zos.closeEntry();
}
}
}
}
public static void Unzip(String zipFile, String targetDir) {
Log.e("lz>>","zipFile:"+zipFile+" targetDir:"+targetDir);
int BUFFER = 4096; //这里缓冲区我们使用4KB,
String strEntry; //保存每个zip的条目名称
try {
BufferedOutputStream dest = null; //缓冲输出流
FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry; //每个zip条目的实例
while ((entry = zis.getNextEntry()) != null) {
try {
Log.i("Unzip: ","="+ entry);
int count;
byte data[] = new byte[BUFFER];
strEntry = entry.getName();
File entryFile = new File(targetDir + strEntry);
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(entryFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
zis.close();
Log.i("Unzip: ","="+ "结束");
} catch (Exception cwj) {
cwj.printStackTrace();
}
}
压缩使用:
File appDir = getFilesDir();
String filePath = appDir+“”";//需要压缩的文件路径
String zipWalletfile = zipfile.getAbsolutePath();
try {
//zipWalletfile + "/wallet.zip"压缩后的文件名
ZipUtils.compressFolder(ZipActivity.this, filePath , zipWalletfile + "/wallet.zip");
} catch (Exception e) {
throw new RuntimeException(e);
}
解压使用:
File zipfile = Environment.getExternalStorageDirectory();
String zipWalletfile = zipfile.getAbsolutePath();//待解压文件
String unzipPath=getFilesDir().getPath();解压文件路径
ZipUtils.Unzip(zipWalletfile + "/wallet.zip",unzipPath+"/");
到此这篇关于Android实现文件压缩与解压工具类的文章就介绍到这了,更多相关Android文件压缩解压内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
-
这篇文章主要为大家详细介绍了Android自定义字母导航栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-12-12
-
上篇文章我们聊了些Android里那些我们平时碰不到但很重要的类ViewRootImpl,这一篇我们就来看看另外那个类ActivityThread,文中有相关的代码示例,感兴趣的同学可以跟着小编一起来学习2023-05-05
-
Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,具体调用方法大家可以通过本文学习2016-11-11
-
这篇文章主要介绍了Android控制闪光灯的方法,可实现闪光灯打开与关闭的效果,涉及Android操作Camera拍照闪光灯的相关技巧,需要的朋友可以参考下2016-01-01
-
这篇文章主要介绍了获取安卓的手机或者平板的唯一标识的方法,需要的朋友可以参考下2014-02-02
-
这篇文章主要为大家详细介绍了Android实现多个连续带数字圆圈效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-07-07
-
这篇文章主要介绍了Android 同时setTag两次保存多种值的示例代码,需要的朋友可以参考下2017-02-02
-
这篇文章主要介绍了 Android动态改变App桌面图标的方法,非常不错,具有参考借鉴价值,需要的朋友参考下2017-01-01
-
在一个View中,系统提供了scrollTo、scrollBy两种方式来改变一个View的位置,下面通过本文给大家介绍Android scrollTo和scrollBy方法使用解析,需要的朋友参考下吧2018-01-01
-
这篇文章主要为大家详细介绍了Android使用ViewPager实现启动引导页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-04-04
最新评论