使用zip4j重命名zip中的文件 [英] Rename file in zip with zip4j

查看:261
本文介绍了使用zip4j重命名zip中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 zip4j 1.3.1压缩应用程序中的文件.现在,我正在尝试重命名zip中的文件,而不必重命名文件本身.似乎有一种方法可以解决,但不起作用.

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.

我的代码如下:

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);
}

zip中的文件与我在srcFile中传递的文件同名. 也许尚未实施?您知道任何替代方法,方法或库吗?

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?

谢谢.

我想出了一种使用所需名称创建新文件,然后将其用作源文件的解决方案:

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);

推荐答案

今天我遇到了同样的问题.调试zip4j后,解决方案非常简单.

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.

最美好的祝愿

这篇关于使用zip4j重命名zip中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆