nowxue
nowxue

Reputation: 425

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

Upvotes: 2

Views: 2889

Answers (1)

zsolt.lengyel
zsolt.lengyel

Reputation: 71

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

Upvotes: 7

Related Questions