Android压缩、解压ZIP文件

运用场景:大文件输出效率低,需要压缩文件以缩短运行时间提高体验。
文件压缩方法工具(参考链接:http://www.manongjc.com/article/4206.html)

 /**
     * 压缩文件和文件夹(SDCard文件压缩)
     * http://www.manongjc.com/article/4206.html
     *
     * @param srcFile 要压缩的文件或文件夹
     * @param zipFile 压缩完成的Zip文件
     * @throws Exception
     */
    public static void ZipFolder(File srcFile, File zipFile) {
        try {
            //创建ZIP
            ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
            //压缩
            ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip);
            //完成和关闭
            outZip.finish();
            outZip.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 压缩文件
     *
     * @param srcFileParentName
     * @param srcFileName
     * @param zipOutputSteam
     * @throws Exception
     */
    private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam) throws Exception {
        if (zipOutputSteam == null)
            return;
        File file = new File(srcFileParentName + srcFileName);

        if (file.isFile()) {
            ZipEntry zipEntry = new ZipEntry(srcFileName);
            FileInputStream inputStream = new FileInputStream(file);
            zipOutputSteam.putNextEntry(zipEntry);
            int len;
            byte[] buffer = new byte[1024 * 1024 * 10]; //10M
            while ((len = inputStream.read(buffer)) != -1) {
                zipOutputSteam.write(buffer, 0, len);
            }
            zipOutputSteam.closeEntry();
        } else {
            //文件夹
            String fileList[] = file.list();
            //没有子文件和压缩
            if (fileList.length <= 0) {
                ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator);
                zipOutputSteam.putNextEntry(zipEntry);
                zipOutputSteam.closeEntry();
            }
            //子文件和递归
            for (int i = 0; i < fileList.length; i++) {
                if (USBMTPReceiver.isCanceled || !USBMTPReceiver.isRunning) break;
                ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam);
                double progress = DialogUtils.getProgress(i + 1, fileList.length);
                EventBus.getDefault().post("zipFile&" + progress);
            }
        }
    }

Usb文件的压缩(两种方法实现)
方法一:仿以上方法实现压缩(直接对Usb文件压缩,一步到位,但是耗时多)

 /**
     * 压缩文件和文件夹(usb文件压缩)
     *
     * @param srcFile 要压缩的文件或文件夹
     * @param zipFile 压缩完成的Zip文件
     * @throws Exception
     */
    public static void ZipUsbFolder(UsbFile srcFile, UsbFile zipFile) throws Exception {
        //创建ZIP
        ZipOutputStream outZip = new ZipOutputStream(new UsbFileOutputStream(zipFile));
        //压缩
        Log.i("whh0923", "srcFile=" + srcFile.getParent() + ",zipFile=" + zipFile.getName());
        ZipUsbFiles(srcFile, outZip);
        //完成和关闭
        outZip.finish();
        outZip.close();
    }

    /**
     * 压缩文件
     *
     * @param srcFile
     * @param zipOutputSteam
     * @throws Exception
     */
    private static void ZipUsbFiles(UsbFile srcFile, ZipOutputStream zipOutputSteam) throws Exception {
        Log.i("whh0923", "srcFileName:" + srcFile.getName());
        if (zipOutputSteam == null)
            return;
        if (!srcFile.isDirectory()) {
            ZipEntry zipEntry = new ZipEntry(srcFile.getName());
            UsbFileInputStream inputStream = new UsbFileInputStream(srcFile);
            zipOutputSteam.putNextEntry(zipEntry);
            int len;
            byte[] buffer = new byte[4096];
            while ((len = inputStream.read(buffer)) != -1) {
                zipOutputSteam.write(buffer, 0, len);
            }
            zipOutputSteam.closeEntry();
        } else {
            //文件夹
            UsbFile[] usbFiles = srcFile.listFiles();
            //没有子文件和压缩
            if (usbFiles.length <= 0) {
                ZipEntry zipEntry = new ZipEntry(srcFile.getName() + File.separator);
                zipOutputSteam.putNextEntry(zipEntry);
                zipOutputSteam.closeEntry();
            }
            //子文件和递归
            for (int i = 0; i < usbFiles.length; i++) {
                ZipUsbFiles(usbFiles[i], zipOutputSteam);
            }
        }
    }

方法二:通过上述方法将压缩文件暂存SDCard,再拷贝到U盘(流程多,但效率高)
SDCard zip文件拷贝到USB设备方法

/**
     * 拷贝文件到USB设备(以每20M的速度传输)
     * @param file SDcard文件
     * @param usbFile USB文件
     */
    public static void copyFile(File file, UsbFile usbFile) {
        FileInputStream inStream = null;
        try {
            inStream = new FileInputStream(file);
            UsbFileOutputStream outStream = new UsbFileOutputStream(usbFile);
            byte[] buf = new byte[1024 * 1024 * 20]; //20M
            int len;
            while ((len = inStream.read(buf)) > 0) {
                outStream.write(buf, 0, len);
            }
            outStream.close();
            inStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

解压文件

public static String tempFileName = "/sdcard/GADataTemp"; //临时文件夹名
/**
     * 解压zip压缩文件到指定目录
     *
     * @param zipPath
     */
    public static boolean unzipFile(String zipPath) {
        try {
            Log.e("whh0927", "开始解压的文件:" + zipPath + "," + "解压的目标路径:" + tempFileName);
            // 创建解压目标目录
            File file = new File(tempFileName);
            // 如果目标目录不存在,则创建
            if (!file.exists()) {
                file.mkdirs();
            }
            // 打开压缩文件
            InputStream inputStream = new FileInputStream(zipPath);

            ZipInputStream zipInputStream = new ZipInputStream(inputStream);

            // 读取一个进入点
            ZipEntry zipEntry = zipInputStream.getNextEntry();
            // 使用1Mbuffer
            byte[] buffer = new byte[1024 * 1024];
            // 解压时字节计数
            int count = 0;
            // 如果进入点为空说明已经遍历完所有压缩包中文件和目录
            while (zipEntry != null) {
//                Log.e("whh0927", "解压文件 入口 1: " + zipEntry);
                if (!zipEntry.isDirectory()) {  //如果是一个文件
                    // 如果是文件
                    String fileName = zipEntry.getName();
//                    Log.e("whh0927", "解压文件 原来 文件的位置: " + fileName);
                    fileName = fileName.substring(fileName.lastIndexOf("/") + 1);  //截取文件的名字 去掉原文件夹名字
//                    Log.e("whh0927", "解压文件 的名字: " + fileName);
                    file = new File(tempFileName + File.separator + fileName);  //放到新的解压的文件路径

                    file.createNewFile();
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    while ((count = zipInputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, count);
                    }
                    fileOutputStream.close();

                }

                // 定位到下一个文件入口
                zipEntry = zipInputStream.getNextEntry();
//                Log.e("whh0927", "解压文件 入口 2: " + zipEntry);
            }
            zipInputStream.close();
            FileUtils.deleteFile(new File(zipPath)); //删除原文件
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("whh0927", "unzipFile Exception" + e.toString());
            return false;
        }

    }

每天进步一点点。。。(2019-10-09)

你可能感兴趣的:(Android压缩、解压ZIP文件)