AbbasFaisal
AbbasFaisal

Reputation: 1490

Sevenzipsharp CompressStream throws Exception: Value cannot be null

I am trying out following code in order to compress and save a bitmap of a screenshot but get this error. I haven't tried using CompressFiles as I have to do it using memory stream.

        public void CompressAndSaveBitmap(Bitmap bitmap)
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                using (FileStream file = new FileStream("XYZ", FileMode.Create, System.IO.FileAccess.Write))
                {
                    bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
                    // tried these but they don't work.
                    //memStream.Seek(0, 0);
                    //memStream.Position = 0; 
                    this.compressor.CompressStream(memStream, file); // throws error here

                // also tried the following to see if Bitmap contains the problem
                //UnicodeEncoding uniEncoding = new UnicodeEncoding();
                //byte[] firstString = uniEncoding.GetBytes("Invalid file path characters are: ");

                //int count = 0;
                //while (count < firstString.Length)

                //memStream.WriteByte(firstString[count++]);
                //memStream.Flush();
                ////memStream.Seek(0, 0);
                //memStream.Position = 0;
                //this.compressor.CompressStream(memStream, file);
                }
           }
        }

Compressor initialization code:

public Compressor()
{
    SevenZipCompressor.SetLibraryPath(@"D:\7z.dll");

    this.compressor = new SevenZip.SevenZipCompressor();
    compressor.CompressionLevel = CompressionLevel.Ultra;
    compressor.CompressionMethod = CompressionMethod.Ppmd;
}

It's crashes on this line in the library:

    var lockObject = (object) _files ?? _streams;
    lock (lockObject) // here in ArchiveUpdateCallback.cs

I see that the file is created but it's corrupt.

Upvotes: 1

Views: 345

Answers (1)

FaizanHussainRabbani
FaizanHussainRabbani

Reputation: 3439

I was able to successfully compress file using following code:

public void CompressAndSaveBitmap(Bitmap bitmap)
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                using (FileStream file = new FileStream("XYZ", FileMode.Create, System.IO.FileAccess.Write))
                {
                    bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
                    // tried these but they don't work.
                    //memStream.Seek(0, 0);
                    //memStream.Position = 0; 

                    SevenZipCompressor compressor =
                        new SevenZipCompressor
                        {
                            CompressionLevel = CompressionLevel.Ultra,
                            CompressionMethod = CompressionMethod.Lzma
                        };

                    compressor.CompressStream(memStream, file); // throws error here
                }
            }
        }

Following was the bitmap file:

enter image description here

Executed code:

            Bitmap bmp = new Bitmap("Test.bmp");
            res.CompressAndSaveBitmap(bmp);

After execution got following output:

enter image description here

Opened the file with WinRar and extracted file: enter image description here

In folder found the extracted image:

enter image description here

UPDATE: I think the only problem with OP's code is this.compressor is not initialized.

Upvotes: 2

Related Questions