sharpcompress icon indicating copy to clipboard operation
sharpcompress copied to clipboard

Checking if a 7z file is encrypted will crash

Open njlr opened this issue 2 years ago • 3 comments

Create a 7z file:

$ 7zz a test.7z

Check the entries are encrypted:

Scratch.fsx

#r "nuget: SharpCompress, 0.33.0"

open SharpCompress
open SharpCompress.Archives

let archive = ArchiveFactory.Open("test.7z")

for e in archive.Entries do
  printfn $"IsEncrypted %b{e.IsEncrypted}"
$ dotnet fsi ./Scratch.fsx

It will crash:

System.NullReferenceException: Object reference not set to an instance of an object.
   at SharpCompress.Common.SevenZip.SevenZipFilePart.get_IsEncrypted()
   at SharpCompress.Common.SevenZip.SevenZipEntry.get_IsEncrypted()

njlr avatar Sep 14 '23 08:09 njlr

To see if I recreated the issue correctly, if you check if it is a directory before checking the IsEncrypted flag if not e.IsDirectory then Does that do anything for you?

Erior avatar Sep 23 '23 12:09 Erior

This does not crash:

#r "nuget: SharpCompress, 0.33.0"

open SharpCompress
open SharpCompress.Archives

let archive = ArchiveFactory.Open("test.7z")

for e in archive.Entries do
  if e.IsDirectory then
    printfn $"%A{e} is a directory"
  else
    printfn $"IsEncrypted %b{e.IsEncrypted}"

Perhaps the error message should be improved?

Can a directory be encrypted?

njlr avatar Sep 26 '23 12:09 njlr

This was fixed: https://github.com/adamhathcock/sharpcompress/pull/768

adamhathcock avatar Sep 26 '23 15:09 adamhathcock