0

i need to protect a list of files ( file can be any type ) by password . i chose to use zip4j to do it . i use :

  public static ZipFile creatZipFile(List<File> filesToAdd) {
        try {
            //This is name and path of zip file to be created
            ZipFile zipFile = new ZipFile("test.zip");
            //Initiate Zip Parameters which define various properties
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression

            //DEFLATE_LEVEL_FASTEST   - Lowest compression level but higher speed of compression
            //DEFLATE_LEVEL_FAST    - Low compression level but higher speed of compression
            //DEFLATE_LEVEL_NORMAL  - Optimal balance between compression level/speed
            //DEFLATE_LEVEL_MAXIMUM   - High compression level with a compromise of speed
            //DEFLATE_LEVEL_ULTRA     - Highest compression level but low speed
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            //Set the encryption flag to true
            parameters.setEncryptFiles(true);

            //Set the encryption method to AES Zip Encryption
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);

            //AES_STRENGTH_128 - For both encryption and decryption
            //AES_STRENGTH_192 - For decryption only
            //AES_STRENGTH_256 - For both encryption and decryption
            //Key strength 192 cannot be used for encryption. But if a zip file already has a
            //file encrypted with key strength of 192, then Zip4j can decrypt this file
            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

            //Set password
            parameters.setPassword("sendmail");
            //Now add files to the zip file
            zipFile.addFiles((ArrayList) filesToAdd, parameters);
            
            return zipFile;
        } catch (ZipException e) {
            e.printStackTrace();
            return null;
        }

    }

know i want to convert my zipFile to array of byte to send it as attachment in email .I find some method can do that but not by zip4j . anyone can help me ?

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • The zip is in the `test.zip` file. Just attach it to the email (of course, you should use a temporary file instead of a fixed name). – Olivier Mar 09 '22 at 11:50
  • i have a code to add attachement . it was add byte[] `````````````````attachments.add(emailGeneratorHelper.getAttachment(FileBytes, fileInfo.getName));```````````````` i went to add in place of file bytes a zipFileBytes – Akrimi Zoulfa Mar 09 '22 at 13:35
  • getAttachmentMethode : Attachments getAttachment(byte[] attachmentBytes, String name) { var attach = new Attachments(); String encodedString = Base64.getEncoder().encodeToString(attachmentBytes); attach.setContent(encodedString); attach.setFilename(name); return attach; – Akrimi Zoulfa Mar 09 '22 at 13:38

0 Answers0