I want to make a zip file with password with zip4j in java.

Asked 2 years ago, Updated 2 years ago, 31 views

I'm trying to create a zip file with a password in java.
Arguments are as follows

As a result, the path described in argument "1." had a zip file, but no password...
When extracted, it contains a folder with argument "3.", and the contents of the folder are the same as the original folder.
Also, the contents of the folder with the argument "3." are confused between the file and the folder.
Please tell me how to zip with a password

Below is the source code and reference site

package zip_with_password;

import java.io.File;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.EncryptionMethod;

public class test {

    public static void main(String[]args) {
        // compression parameter
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
         
        try{
            String outputPath=args[0];
            String password = args[1];
            System.out.println("getoutputPath:"+outputPath); // debug
            System.out.println("get password:"+password); // debug
         
            // Generates a ZipFile instance.
            // The constructor argument specifies the output destination and password.
            @SuppressWarnings ("resource")
            ZipFile zipFile = new ZipFile(outputPath, password.toCharArray());
            // Specify the file to be compressed by path
            String inputPath=args[2];
            System.out.println("get inputPath:"+inputPath); // debug
            File folder = new File(inputPath);
            zipFile.addFolder(folder,zipParameters);                       
                
            
        } catch(Exceptione){
            // exception handling
            e.printStackTrace();
        }
        
        System.out.println("end");// debug
    }

}

Note:
https://kita-note.com/java-password-protected-zip-zip4j

Thank you for your cooperation

java

2022-09-30 14:37

1 Answers

I tried to execute the code listed, but it looks like it's behaving as expected.
Also, the file name (directory structure such as) is visible in the ZIP specification.

.ZIP File Format Specification 6.2 also specifies file name encryption, but zip4j does not seem to support it (Reference).


2022-09-30 14:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.