java导出Zip包使用WinRAR解压时报不可预料的压缩文件末端

本文介绍了如何在Java中使用ZipOutputStream对文件进行压缩,通过遍历fileList,调用TestUtil.yasuo方法,最后强调了必须关闭ZipOutputStream以避免WinRAR解压时出现错误。
摘要由CSDN通过智能技术生成
ZipOutputStream zos = new ZipOutputStream(out);
for (File file : fileList) {
    TestUtil.yasuo(file, zos);
}
zos.close();//这里必须关闭,否则winrar解压会报错

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!如果您想要在Java中实现可研导出zip的功能,您可以使用JavaZipOutputStream类来完成。下面是一个简单的示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipExporter { public static void main(String[] args) { String sourceFolder = "path/to/source/folder"; // 源文件夹路径 String zipFilePath = "path/to/output/zip/file.zip"; // 输出zip文件路径 try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); // 递归压缩文件夹中的文件 File folder = new File(sourceFolder); addFilesToZip(folder, folder.getName(), zos); zos.close(); fos.close(); System.out.println("Zip文件生成成功!"); } catch (IOException e) { e.printStackTrace(); } } private static void addFilesToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException { for (File file : folder.listFiles()) { if (file.isDirectory()) { addFilesToZip(file, parentFolder + "/" + file.getName(), zos); continue; } FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(parentFolder + "/" + file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } } ``` 在上面的示例代码中,您需要将`sourceFolder`变量设置为您要压缩的源文件夹的路径,将`zipFilePath`变量设置为您要生成的zip文件的路径。然后,代码将会递归地压缩源文件夹中的所有文件和子文件夹。 请注意,这只是一个简单的示例代码,您可能需要根据您的具体需求进行适当的修改和扩展。希望对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值