【Java压缩zip】以及【压缩异常问题】

一、Java压缩zip代码:

public static Boolean picListToZip(List<String> mognoList, String ftpPath, String zipName) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        //链接
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp sftpChannel = null;
        try {
            session = jsch.getSession("用户名", "xxx.xxx.xxx.xxx", 端口号);
            session.setConfig("StrictHostKeyChecking", "no");//使用带私钥的JSch来FTP文件
            session.setPassword("密码");
            session.connect(30000);
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftpChannel = (ChannelSftp) channel;
            //链接成功
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
            byte[] buff = new byte[1024];
            //添加文件源
            for (int i = 0; i < mognoList.size(); i++) {
                FSObject fsObject = (MongoDBUtil.getObject("mongo桶", mognoList.get(i))); //从mongoDB中获取文件信息
                InputStream is = fsObject.getInputstream();
                zos.putNextEntry(new ZipEntry(fsObject.getName()));//会在压缩文件中建立名字为zipName的文件夹中建立fsObject.getName()文件
                int len = 0;
                while ((len = is.read(buff)) > 0) {
                    zos.write(buff, 0, len);
                }
                zos.closeEntry();
                is.close();
            }
            zos.close();
            FileInputStream in = new FileInputStream(new File(zipName)); // 转换为输入流
            //切换到指定文件夹---线上操作
            if (sftpChannel.ls(ftpPath) == null) {
                log.info("如果文件夹不存在,则创建文件夹");
                sftpChannel.mkdir(ftpPath);
            }
            sftpChannel.cd(ftpPath);
            sftpChannel.put(in, zipName);
            log.info("一次结束");
            sftpChannel.exit();
        } catch (Exception e) {
            sftpChannel.exit();
            session.disconnect();
            e.printStackTrace();
            return false;
        }
        return true;
    }

二、使用时出现的异常问题:

java.util.zip.ZipException: duplicate entry: xxx文件
    at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:215)
出现这种错误的原因是:打包的过程中,出现相同的文件名称
 zos.putNextEntry(new ZipEntry(fsObject.getName()));//会在压缩文件中建立名字为zipName的文件夹中建立fsObject.getName()文件

参数 fsObject.getName() 存在相同的文件名称时,就会出现开头处的异常信息。

解决方法: 针对文件名做唯一处理,在原文件名的基础上加上UUID,避免文件名一致
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果使用 Java 压缩 ZIP 文件时出现损坏的问题,可能是由于以下原因导致的: 1. 压缩文件路径或文件名包含特殊字符,例如中文字符、空格、特殊符号等,这可能会导致文件路径或文件名无法正确解析,从而导致压缩文件损坏。 2. 压缩文件中包含非法字符或不兼容的文件格式,例如压缩文件中包含了损坏的文件或不支持的文件格式,这些都可能导致压缩文件损坏。 3. 压缩算法或压缩级别不正确,如果压缩算法或压缩级别设置不正确,也可能导致压缩文件损坏。 下面是一些可能有用的建议来解决这些问题: 1. 避免在压缩文件路径和文件名中使用特殊字符,例如中文字符、空格和特殊符号。可以尝试使用英文字母和数字来命名文件和路径。 2. 确保要压缩的文件都是合法的,并且文件格式兼容。可以尝试对要压缩的文件进行检查,确保它们没有损坏并且可以被正确读取。 3. 确认使用的压缩算法和级别,确保它们是合适的。例如,使用较高的压缩级别可能导致压缩文件变得更小,但也可能导致压缩时间变长,甚至可能导致压缩文件损坏。可以尝试使用默认的压缩算法和级别,以避免这些问题。 下面是一个示例代码来演示如何使用 Java 压缩 ZIP 文件: ```java import java.io.*; import java.util.zip.*; public class ZipCompressor { public static void compress(File sourceFile, File zipFile) throws IOException { // 创建 ZIP 输出流 FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); // 压缩文件 compressFile(sourceFile, zos, ""); // 关闭 ZIP 输出流 zos.close(); fos.close(); } private static void compressFile(File file, ZipOutputStream zos, String path) throws IOException { if (file.isDirectory()) { // 压缩目录 File[] files = file.listFiles(); if (files != null && files.length > 0) { for (File subFile : files) { compressFile(subFile, zos, path + file.getName() + "/"); } } } else { // 压缩文件 String entryPath = path + file.getName(); ZipEntry entry = new ZipEntry(entryPath); zos.putNextEntry(entry); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } } ``` 在上面的示例代码中,我们使用 ZipOutputStream 类来创建 ZIP 输出流,并使用 compressFile() 方法来递归地压缩目录和文件。需要注意的是,在压缩文件时,我们需要确保文件名和路径不包含特殊字符,并且压缩算法和级别设置正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值