纯Java 递归解压并删除指定目录(包含子目录) 下所有的压缩包

发布于:2024-10-15 ⋅ 阅读:(134) ⋅ 点赞:(0)

.ziw格式文件是为知笔记的文本文档,可以用为知笔记打开,但这样需要再安装一个大概200多M的程序,关键是这个程序一点也不好,除了广告收费外没有什么功能。
其实可以直接把文件名后缀名改成.zip,然后解压,解压成功之后生成的是html网页,直接点击index.html就可以在浏览器中打开文件了。

采用下面代码,甚至不到改文件后缀,直接解压就可以了。

public class DemoTest {
    public static void main(String[] args) throws Exception {
        fun(new File("E:\\视频\\金融项目--尚融宝\\课件"));
    }

    private static void fun(File file) throws Exception {
        File flist[] = file.listFiles();
        for (File f : flist) {
            if (f.isDirectory()) {
                //这里将列出所有的文件夹
                fun(f);
            } else {
                //这里将列出所有的文件
                final String name = f.getAbsolutePath();
                if (name.endsWith(".ziw")) {
                    System.out.println(name.substring(0, name.lastIndexOf(".ziw") - 1));
                    unzip(name, name.substring(0, name.lastIndexOf(".ziw") - 1));
                }
                f.deleteOnExit();
            }
        }
    }

    // 解压 ZIP 文件到目标目录
    public static void unzip(String zipPath, String destDir) throws IOException {
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipPath))) {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                File outFile = new File(destDir, entry.getName());
                if (entry.isDirectory()) {
                    outFile.mkdirs();
                } else {
                    outFile.getParentFile().mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(outFile)) {
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = zis.read(buffer)) != -1) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                zis.closeEntry();
            }
        }
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到