zipparameters 使用_使用zip4j重命名zip中的文件

作者分享了在使用Zip4j 1.3.1压缩文件时,如何尝试不改源文件名直接重命名压缩包内的文件,遇到的问题及最终通过设置sourceExternalStream为true实现的解决方案。建议对Zip4j API有疑问的开发者参考。
摘要由CSDN通过智能技术生成

I'm using zip4j 1.3.1 to compress files in my application. Now I'm trying to rename the file inside the zip without having to rename the file itself. There seems to be a method for that, but it's not working.

My code looks like:

public static void zipFile(File dstPath, File srcFile, String optionalName) throws ZipException {

ZipFile zipFile = new ZipFile(dstPath);

ZipParameters parameters = new ZipParameters();

parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_MAXIMUM);

parameters.setEncryptFiles(true);

parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);

parameters.setPassword(RutasDAO.getPassZip());

//here I'm setting the name in the zip, but it's not working

parameters.setFileNameInZip(optionalName);

zipFile.addFile(srcFile, parameters);

}

The file inside the zip has the same name as the file I'm passing in srcFile.

Maybe it's not yet implemented? do you know any alternative, method or library?

Thanks.

Edit:

I've come up with a solution creating a new file with the desired name, then using it as source file:

File renamedFile = new File(tmpDirectory + optionalFileName);

copyFile(srcFile, renamedFile);

解决方案

today I met the same problem. After debugging zip4j, the solution is very simple.

parameters.setSourceExternalStream(true);

No other changes happened, but the file appeared in archive with the new name. I hope this setting will help you too.

Best wishes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值