SimonC
SimonC

Reputation: 1618

Cannot write to new MemoryStream - Stream closed

I have methods that compress one or more files to either a tar(.gz) or a zip. In these files I use bytes that I have already loaded in to memory, and I just need them to be compressed, and the compressed file to be written to a MemoryStream.

At first, I was using memorystream references, to save on resources. I began receiving following messages from the application:

[MailArchive] [14:06] [Fatal] A fatal error occurred while executing the command "export-email" in the plugin RestService!
[MailArchive] [14:06] [Fatal] This error was not caught by the command handler and may have caused a severe application crash!
[MailArchive] [14:06] [Fatal] Error details are following:
[MailArchive] [14:06] [Trace] Cannot access a closed Stream.
[MailArchive] [14:06] [Trace] mscorlib
[MailArchive] [14:06] [Trace]    at System.IO.__Error.StreamIsClosed()
   at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)
   at SharpCompress.Archives.AbstractWritableArchive`2.<>c.<SaveTo>b__18_0(IWritableArchiveEntry x)
   at SharpCompress.Utility.ForEach[T](IEnumerable`1 items, Action`1 action)
   at SharpCompress.Archives.AbstractWritableArchive`2.SaveTo(Stream stream, WriterOptions options)
   at SharpCompress.Archives.Zip.ZipArchive.SaveTo(Stream stream)
   at De.Nwt.MailArchive.MailProcessor_2.MailProcessor_2.CompressToZip(MemoryStream& memStream, IntFile[] files) in C:\Users\sc\Documents\Git\MailArchive\MailProcessor_2\Src\Classes\MailProcessor_2.FileIO.cs:line 312
   at De.Nwt.MailArchive.MailProcessor_2.MailProcessor_2.ExportAttachments(ArchiveType archType, String domain, String messageId, Boolean singleAttachment, String fName, Int32 idx) in C:\Users\sc\Documents\Git\MailArchive\MailProcessor_2\Src\Classes\MailProcessor_2.CommandHandling.cs:line 360
   at De.Nwt.MailArchive.MailProcessor_2.MailProcessor_2.Command_ExportEmail(Command sender, String[] args) in C:\Users\sc\Documents\Git\MailArchive\MailProcessor_2\Src\Classes\MailProcessor_2.CommandHandling.cs:line 192
   at De.Nwt.MailArchive.Commands.Command.Invoke(String[] args) in C:\Users\sc\Documents\Git\MailArchive\Command\Src\Classes\Command.cs:line 120
   at De.Nwt.MailArchive.MainClass.baseEvents_PluginExecutedCommand(Object sender, PluginExecutedCommandEventArgs e) in C:\Users\sc\Documents\Git\MailArchive\MailArchive\Src\Classes\MailArchive.cs:line 979
[MailArchive] [14:06] [Warn] Will return a CommandExceptionResult containing the above error!

After receiving these errors, I switched to using out-parameters for the MemoryStream objects, and even after trying to use multiple new instances of a memory stream, I still receive the same error.

One of the methods is below:

    /// <summary>
    /// Compresses one or more files to a zip archive.
    /// </summary>
    /// <param name="memStream">The memory stream.</param>
    /// <param name="files">The files.</param>
    void CompressToZip(out MemoryStream memStream, params MailProcessor_2.IntFile[] files) {
        using (var zipArchive = ZipArchive.Create()) {
            foreach (var file in files) {
                using (var _memStream = new MemoryStream(file.Contents)) {
                    zipArchive.AddEntry(file.Filename, _memStream, _memStream.Length);
                }
            }

            var tmpStream = new MemoryStream();
            zipArchive.SaveTo(tmpStream); // The error occurrs here
            memStream = tmpStream;
        }
    }

I've gone through the debugger, and setting a breakpoint right where the error occurrs, tmpStream's _isOpen variable is set to true.

Any help with the matter would be much appreciated.


EDIT: I forgot to mention I'm using SharpCompress for Tar-archival and Zip compression. For Gzip compression I'm using the standard .Net framework.

Upvotes: 1

Views: 1225

Answers (1)

xanatos
xanatos

Reputation: 111860

As I suspected, the code takes ownership of the Stream you pass as the file, so you can't Close()/Dispose() it...

Write it as:

var _memStream = new MemoryStream(file.Contents);
zipArchive.AddEntry(file.Filename, _memStream, true);

It should use this overload. The true is so that it closes the Stream when not used.

Probably it will auto-discover the length (I see that it is an optional value with 0 as default)

Upvotes: 3

Related Questions