Pingchas 发表于 2025-7-24 17:29:32

Java解压zip压缩包

public static void unzip(String zipFilePath, String destDirPath) {
      File destDir = new File(destDirPath);
      if (!destDir.exists()) {
            destDir.mkdirs();
      }

      try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath), Charset.forName("GBK"))) {
            ZipEntry entry;

            while ((entry = zis.getNextEntry()) != null) {
                String filePath = destDirPath + File.separator + entry.getName();

                if (entry.isDirectory()) {
                  // 如果是目录,创建目录
                  new File(filePath).mkdirs();
                } else {
                  // 如果是文件,创建父目录并写入文件
                  File parent = new File(filePath).getParentFile();
                  if (!parent.exists()) {
                        parent.mkdirs();
                  }

                  try (FileOutputStream fos = new FileOutputStream(filePath)) {
                        byte[] buffer = new byte;
                        int len;
                        while ((len = zis.read(buffer)) != -1) {
                            fos.write(buffer, 0, len);
                        }
                  }
                }
                zis.closeEntry();
            }

            System.out.println("解压完成!");
      } catch (IOException e) {
            e.printStackTrace();
      }
    }

alglsf666 发表于 2025-7-25 14:48:35

谢谢分享

H.U.C清风 发表于 2025-7-26 12:40:19

谢谢分享,已回复
页: [1]
查看完整版本: Java解压zip压缩包