srikanth-lingala / zip4j

A Java library for zip files and streams
Apache License 2.0
2.06k stars 310 forks source link

ERROR: CRC Failed in encrypted file. Wrong password - while unzip using 7-zip #534

Open sreenivasulun2007 opened 8 months ago

sreenivasulun2007 commented 8 months ago

Hello Srikanth, Hopefully all is well with you. Thanks your wonderful library. We are using zip4j-1.3.2 library to create password encrypted zip file. And then, when we try to unzip the file with the tool 7-zip, we are getting the following error and the issue is intermittent, ERROR: CRC Failed in encrypted file. Wrong password

As follows the code util to create the zip file,


import java.io.File;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZipFileUtil {
    private static final Logger logger = LoggerFactory.getLogger(ZipFileUtil.class);
    public static final String ZIP_WITH_DOT                  = ".zip";
    public static final String DOT                  = ".";
    /**
     * Method for creating password protected zip file
     * 
     * @param sourcePath
     * @throws ZipException
     */
    public String compressWithPassword(String sourcePath, String zipFilePassword) throws ZipException {
        try {
            //String destPath = sourcePath + ".zip";
            String zipFileName=sourcePath.substring(0,sourcePath.lastIndexOf(DOT))+ ZIP_WITH_DOT;
            logger.info("ZIP File Name " + zipFileName);
            ZipFile zipFile = new ZipFile(zipFileName);
            // Setting parameters
            ZipParameters zipParameters = new ZipParameters();
            zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
            zipParameters.setEncryptFiles(true);
            zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
            zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            // Setting password
            zipParameters.setPassword(zipFilePassword);
            zipFile.addFile(new File(sourcePath), zipParameters);
            return zipFileName;
        } catch (ZipException e) {
            logger.error("exception while zip for file:" + sourcePath + " error:" + e.getMessage());
            throw new ZipException(e);
        }

    }
}

Please advice the best solution for this issue. . In order to fix it, we suggested to upgrade to the version2.11.5 to my boss but he is asking the root cause. My boss is telling that we cannot just upgrade the library in production without proper root cause to explain to management. Thank you, Sreenivasulu N

sreenivasulun2007 commented 8 months ago

Hello Srikanth, We decided to upgrade zip4j from v1.3.2 to v2.11.5 since it takes less effort. Thank you again for your wonderful library.

sreenivasulun2007 commented 8 months ago

I will wait for few more days if you have any feedback. Otherwise, I will close the ticket after few days.