简体   繁体   中英

Rename file in zip with zip4j

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM