java 压缩文件夹有几率压缩失败,打开显示不可预料的末端且用其他专业软件解压文件没压缩完全

java 压缩文件夹有几率压缩失败,打开显示不可预料的末端且用其他专业软件解压文件没压缩完全,求解

代码如下:

/**
 * 批量压缩文件(夹)
 *
 * @param resFileList 要压缩的文件(夹)列表
 * @param zipFile     生成的压缩文件
 * @param comment     压缩文件的注释
 * @throws IOException 当压缩过程出错时抛出
  public static boolean zipFiles(Collection<File> resFileList, File zipFile, String comment) {
    boolean bool = false;
    BufferedOutputStream fo = null;
    ZipOutputStream zipout = null;
    try {
        fo = new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE);
        zipout = new ZipOutputStream(fo);
        int totalSize = resFileList.size();
        int count = 0;
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
            ++count;
        }
        bool = true;
    } catch (Exception e) {
        e.printStackTrace();
        bool = false;
    } finally {
        if (zipout != null) {
            try {
                zipout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fo != null) {
            try {
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return bool;
}



/**
 * 压缩文件
 *
 * @param resFile  需要压缩的文件(夹)
 * @param zipout   压缩的目的文件
 * @param rootpath 压缩的文件路径
 * @throws FileNotFoundException 找不到文件时抛出
 * @throws IOException           当压缩过程出错时抛出
 */
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException {
    rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
    // rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
    if (resFile.isDirectory()) {
        File[] fileList = resFile.listFiles();
        for (File file : fileList) {
            zipFile(file, zipout, rootpath);
        }
    } else {
        if (resFile.length() == 0) {
            return;
        }
        byte buffer[] = new byte[BUFF_SIZE];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
        zipout.setMethod(ZipOutputStream.DEFLATED);
        zipout.setLevel(Deflater.BEST_COMPRESSION);
        zipout.putNextEntry(new ZipEntry(rootpath));
        int realLength;
        while ((realLength = in.read(buffer)) != -1) {
            zipout.write(buffer, 0, realLength);
        }
        zipout.flush();
        in.close();
        zipout.closeEntry();
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值