System.IndexOutOfRange exception when creating and appending to archives


Hi, I think this wrapper is pretty awesome and it definitely makes it much easier to use the LZMA SDK. However, there are still a lot of instability issues such as System.IndexOutOfRange. I've seen this occur for other people but never found the cause.

To reproduce this issue:
  1. Use CompressFileDictionary to append to an existing archive.
  2. Use it again under the same instance to create a new (and different) archive.
  3. Notice the IndexOutOfRange exception that should occur.
The code that I am working with look similar to this. Basically I am archiving files to different archives based on server names that I get elsewhere.

foreach (string webserver in destinationServers)
Console.WriteLine("Adding files to archive: " + webserver + ".7z");

//Check if server archives exist, create the archive if DNE, otherwise append to archive
serverArchive = Path.Combine(PackageOutputFolder, webserver + ".7z");
if (!File.Exists(serverArchive))
    compressor.CompressionMode = CompressionMode.Create;
    compressor.CompressionMode = CompressionMode.Append;
compressor.CompressFileDictionary(compressFileList, serverArchive);

Note that all webservers are unique in destinationServers, so append and create mode are applied to DIFFERENT archives. This will strangely give you the exception when it tries to do int GetStream(...), hopefully you can see it too when you try to follow the reproduction steps. Currently I still don't know the cause, but I've narrowed it down a bit.

PROBLEM: Occurs whenever you try to use the same SevenZipCompressor instance to create an archive AFTER you have already used it to append to an existing archive.

SOLUTION: To solve this (refering to my scenario), I have instantiated a new SevenZipCompressor for each "webserver" in the foreach loop and also made my "destinationServers" list ordered so that existing archives go last. No exception encountered.

The key is that when you instantiate a new SevenZipCompressor, it will be completely new and won't be affected by whether you previously appended or not. If you cannot instantiate a new one (because semantically/logically, it would be too dumb to do so), you will have to make sure all archive appends are done after creation.

Hope this gets fixed, and that it helps someone.