12

I am using DotNetZip to add a file from a MemoryStream to a zip file and then to save that zip as a MemoryStream so that I can email it as an attachment. The code below does not err but the MemoryStream must not be done right because it is unreadable. When I save the zip to my hard drive everything works perfect, just not when I try to save it to a stream.

using (ZipFile zip = new ZipFile())
{
var memStream = new MemoryStream();
var streamWriter = new StreamWriter(memStream);

streamWriter.WriteLine(stringContent);

streamWriter.Flush();      
memStream.Seek(0, SeekOrigin.Begin);

ZipEntry e = zip.AddEntry("test.txt", memStream);
e.Password = "123456!";
e.Encryption = EncryptionAlgorithm.WinZipAes256;

var ms = new MemoryStream();
ms.Seek(0, SeekOrigin.Begin);

zip.Save(ms);

//ms is what I want to use to send as an attachment in an email                                   
}
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
user229133
  • 469
  • 2
  • 6
  • 16
  • IF the memory-stream is zip data wouldn't you expect it to be unreadable? – Sam I am says Reinstate Monica Jun 28 '12 at 16:01
  • Have you tried to save the memory stream to a file and verified that the conent of that file differs from the file when you save it to file directly from ZipFile? – erikH Jun 28 '12 at 16:03
  • Sam I am - I am taking the memory stream and sending it as an attachment called test.zip. Then went I get it I thought I should be able to treat it as a regular zip file. Am I wrong in thinking this? – user229133 Jun 28 '12 at 16:29
  • erikH - Yes the content of the file is different. I don't know what I'm doing wrong though. – user229133 Jun 28 '12 at 16:31
  • well, if that's the case, than the problem you're having is outside the scope of the code you posted, since ms is what it should be – Sam I am says Reinstate Monica Jun 28 '12 at 16:35
  • @user229133 - Do you actually get the different file before sending it as a mail attachment!? Can you modify your code to both `zip.Save("test1.zip")` and `zip.Save(ms); System.IO.File.WriteAllBytes("test2.zip", ms.ToArray())` and make sure that your files are different? (Since @SamIam can get it right...). Just to be sure that the mail part of the question can be ignored... – erikH Jun 28 '12 at 17:56

2 Answers2

15

Ok, I figured out my problem, pretty stupid actually. Thanks for everyone's help!

ZipEntry e = zip.AddEntry("test.txt", memStream);
e.Password = "123456!";
e.Encryption = EncryptionAlgorithm.WinZipAes256;

//zip.Save("C:\\Test\\Test.zip");

//Stream outStream;

var ms = new MemoryStream();

zip.Save(ms);

    //--Needed to add the following 2 lines to make it work----
ms.Seek(0, SeekOrigin.Begin);
ms.Flush();
user229133
  • 469
  • 2
  • 6
  • 16
9

I've copied your code, and then saved your final memory steam to disk as data.txt. It was completely unreadable to me, but then I realized that it wasn't a text file, it was a zip file, so i saved it as data.zip and it worked as expected

the method I used to save ms to disk is the following(immediately after your zip.Save(ms); line)

            ms.Position = 0;
            byte[] data = ms.ToArray();
            File.WriteAllBytes("data.zip", data);

So, I believe that your memory stream is what It is supposed to be, which is compressed text. It won't be readable until you decompress it.