解压zip压缩文件,Java

x33g5p2x  于2022-07-10 转载在 Java  
字(0.9k)|赞(0)|评价(0)|浏览(438)

解压zip压缩文件,Java

//解压
    public static void decompress(String srcPath, String destPath) throws Exception {
        File file = new File(srcPath);
        if (!file.exists()) {
            throw new RuntimeException(srcPath + "文件不存在");
        }

        ZipFile zf = new ZipFile(file);
        Enumeration entries = zf.entries();
        ZipEntry entry;
        while (entries.hasMoreElements()) {
            entry = (ZipEntry) entries.nextElement();
            //非文件
            if (entry.isDirectory()) {
                String dirPath = destPath + File.separator + entry.getName();
                File dir = new File(dirPath);
                dir.mkdirs();
            } else {
                //文件
                File f = new File(destPath + File.separator + entry.getName());
                if (!f.exists()) {
                    f.createNewFile();
                }

                // 文件数据写入文件
                InputStream is = zf.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(f);

                int count;
                byte[] buf = new byte[1024 * 4];
                while ((count = is.read(buf)) != -1) {
                    fos.write(buf, 0, count);
                }

                is.close();
                fos.close();
            }
        }
    }

传入一个压缩文件(srcPath),然后将压缩文件的内容(文件,目录)递归的解压到目的路径(destPath)下重建文件层次结构。

解压特定zip压缩文件中特定文件,Java_zhangphil的博客-CSDN博客

https://zhangphil.blog.csdn.net/article/details/125522938?spm=1001.2014.3001.5502

相关文章