srikanth-lingala / zip4j

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

Decrypting excel problems in zip #532

Closed dooubb closed 6 months ago

dooubb commented 6 months ago

as is

Zip can be decompressed normally with password, and excel can be opened normally.

I want to double-click zip do not need a password, double-click excel in zip need a password, enter the password after the excel directly open. But now excel is not open after entering the password.

to be

I want to double-click zip do not need a password, double-click excel in zip need a password, enter the password after the excel directly open.

    public static File compressWithPassword(InputStream inputStream, String fileNameInZip,
                                            String password) throws IOException {
        ZipParameters parameters = new ZipParameters();
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        parameters.setFileNameInZip(fileNameInZip + XLSX_EXTENSION);
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        parameters.setIncludeRootFolder(true);
        File tmpZipedFile = new File(tempDir, fileNameInZip + ".zip");
        if (tmpZipedFile.exists()) {
            if (!tmpZipedFile.delete()) {
                tmpZipedFile.deleteOnExit();
            }
        }
        try (ZipFile zipFile = new ZipFile(tmpZipedFile)) {
            zipFile.setPassword(password.toCharArray());
            zipFile.addStream(inputStream, parameters);
        }
        return tmpZipedFile;
    }
dooubb commented 6 months ago

I found that the excel file name of yes contains: