Compress using 7zip with password protection in java

111 Views Asked by At

I have an encrypted file which I need to compress using 7zip with password protection. I couldn't find any useful reference online. I'm new to this zip file stuff and I don't have an idea if this approach is correct or not.

The code I have tried

public int zipSingleFile(String compressedDo1FilePath, File encryptedFile) throws IOException {
    try{
    SevenZOutputFile out = new SevenZOutputFile(encryptedFile);
      SevenZArchiveEntry entry = out.createArchiveEntry(encryptedFile, compressedDo1FilePath);
      out.putArchiveEntry(entry);
      FileInputStream in = new FileInputStream(encryptedFile);
      byte[] b = new byte[1024];
      int count = 0;
      while ((count = in.read(b)) > 0) {
        out.write(b, 0, count);
      }
      out.closeArchiveEntry();
      return 0;
    } catch (IOException exception) {
      logger.info("exception occurred while zipping the file the file ={} ", exception);
      return 1;
    }
  }
1

There are 1 best solutions below

2
RealHowTo On

Apache Compress can only read encrypted 7Zip file, you can't create one.

Ref : https://commons.apache.org/proper/commons-compress/limitations.html

Encryption, solid compression and header compression are only supported when reading archives