3

I have this password protected 7z archive that can be properly opened and extracted with 7z alone. But using the code below:

    byte[] PASSWORD = "secret".getBytes();
    String fileName = "r:/txt.7z";
    SevenZArchiveEntry entry;
    try (SevenZFile arch = new SevenZFile(new File(fileName), PASSWORD)) {
        while ((entry = arch.getNextEntry()) != null) {
            System.out.println(entry.getName());
        }
//      for (var e : arch.getEntries()) {
//          System.out.println(e.getName());
//      }
    }

causes this exception:

Exception in thread "main" java.io.IOException: Stream is not in the BZip2 format
  at org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream.init(BZip2CompressorInputStream.java:252)
  at org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream.<init>(BZip2CompressorInputStream.java:134)
  at org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream.<init>(BZip2CompressorInputStream.java:112)
  at org.apache.commons.compress.archivers.sevenz.Coders$BZIP2Decoder.decode(Coders.java:254)
  at org.apache.commons.compress.archivers.sevenz.Coders.addDecoder(Coders.java:79)
  at org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecoderStack(SevenZFile.java:933)
  at org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecodingStream(SevenZFile.java:909)
  at org.apache.commons.compress.archivers.sevenz.SevenZFile.getNextEntry(SevenZFile.java:222)

Please note, that if you uncomment for loop and comment out while loop, it actually prints (the sole) file name in the archive. It's getNextEntry that triggers the exception about BZip2 format.

I wasn't able to find any bug reports concerning this problem. Of course I thought it might be incorrect password, so I changed it to some rubbish, to see the outcome, but then it gives different exception with suggestion in message, that probably the password is incorrect. So, it seems like password is definitely correct, yet I can't uncompress the file.

I have also xz-1.8.jar (org.tukaani.xz) in my classpath.

I have also tried SevenZFile constructor with SeekableByteBuffer, but exactly the same exception pops up.

Any clues?

Cromax
  • 1,822
  • 1
  • 23
  • 35

1 Answers1

1

The credit for this answer should go to Stefan Bodewig from Apache (see https://issues.apache.org/jira/browse/COMPRESS-452 where I put bug report), but I put it also here in case anyone would fall into the same problem. It turns out, that byte array representing the password should be in UTF16-LE encoding, so what actually fixes the problem is this little snippet:

byte[] PASSWORD = "secret".getBytes("UTF16-LE");

Of course one would have to take care of UnsupportedEncodingException. Thanks Stefan!

Cromax
  • 1,822
  • 1
  • 23
  • 35
  • 1
    Or better `getBytes(StandardCharsets.UTF_16LE)` to avoid typos. This is what Compress' examples page is going to say once the site gets updated. You're welcome, thanks for reporting the issue. – Stefan Bodewig May 07 '18 at 10:01