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 studio实现日期 、时间选择器与进度条

    Android studio实现日期 、时间选择器与进度条

    这篇文章主要为大家详细介绍了Android studio实现日期、时间选择器与进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Android页面之间进行数据回传的方法分析

    Android页面之间进行数据回传的方法分析

    这篇文章主要介绍了Android页面之间进行数据回传的方法,结合实例形式分析了Android页面之间进行数据的传递与处理技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Android 实现手机拨打电话的功能

    Android 实现手机拨打电话的功能

    本篇文章主要介绍 Android 开发手机拨打电话的功能,这里提供示例代码,有兴趣的小伙伴可以参考下
    2016-08-08
  • Android开发学习之WallPaper设置壁纸详细介绍与实例

    Android开发学习之WallPaper设置壁纸详细介绍与实例

    这篇文章主要介绍了Android开发学习之WallPaper设置壁纸详细介绍与实例,有需要的朋友可以参考一下
    2013-12-12
  • android 完全退出应用程序实现代码

    android 完全退出应用程序实现代码

    这篇文章主要介绍了在android中完全退出应用的实现代码,多种实现方法,大家可以根据需求选择
    2013-06-06
  • Android 实现圆角图片的简单实例

    Android 实现圆角图片的简单实例

    这篇文章主要介绍了Android 实现圆角图片的简单实例的相关资料,Android 圆角图片的实现形式,包括用第三方、也有系统,需要的朋友可以参考下
    2017-07-07
  • kotlin延迟初始化和密封类详细讲解

    kotlin延迟初始化和密封类详细讲解

    Kotlin语言的许多特性,包括变量不可变,变量不可为空,等等。这些特性都是为了尽可能地保证程序安全而设计的,但是有些时候这些特性也会在编码时给我们带来不少的麻烦,下面我们来了解延迟初始化和密封类的特点
    2022-11-11
  • Eclipse NDK迁移到Android Studio的方法示例

    Eclipse NDK迁移到Android Studio的方法示例

    本篇文章主要介绍了Eclipse NDK迁移到Android Studio的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Android录音并且输出为Mp4文件的方法教程

    Android录音并且输出为Mp4文件的方法教程

    这篇文章主要给大家介绍了关于Android录音并且输出为Mp4文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • 多面分析HarmonyOS与Android的特点

    多面分析HarmonyOS与Android的特点

    请教身边的大佬们,公司的CTO、中台部门的总监、老东家数十年行业经验的老架构、以及在中科院读研究生的大学老室友、技术圈的网友等等,他们都给出了自己独特的看法,让我从多方面更好的去了解到了大家对鸿蒙的认识
    2021-08-08

最新评论