Rename file in zip with zip4j

末鹿安然 提交于 2019-12-11 04:53:44

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/18702669/rename-file-in-zip-with-zip4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!