搜索内容

有一个问题?

如果您有任何疑问,可以在下面询问或输入您要寻找的!

Android 将文件压缩成ZIP

生成海报
LeeStudio_
LeeStudio_ 2020-08-31 17:45
阅读需:0

Android 将压缩照片成ZIP

关键API: ZipOutputStream

 public static void ZipFolder(String srcFileString, String zipFileString) {
        //建立ZIP
        try {
            ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(new File(zipFileString + "" + System.currentTimeMillis() + ".zip")));
            //创建文件
            File file = new File(srcFileString);
            //缩小
            ZipFiles(file.getParent() + File.separator, file.getName(), outZip);
            //进行和关掉
            outZip.finish();
            outZip.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) {
        try {
            if (zipOutputSteam == null)
                return;
            File file = new File(folderString + fileString);
            if (file.isFile()) {
                ZipEntry zipEntry = new ZipEntry(fileString);
                FileInputStream inputStream = new FileInputStream(file);
                zipOutputSteam.putNextEntry(zipEntry);
                int len;
                byte[] buffer = new byte[4096];
                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(fileString + File.separator);
                    zipOutputSteam.putNextEntry(zipEntry);
                    zipOutputSteam.closeEntry();
                }
                //子文档和递归
                for (int i = 0; i < fileList.length; i++) {
                    ZipFiles(folderString + fileString + "/", fileList[i], zipOutputSteam);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2个方式 拿下!

评论
  • 消灭零回复