DotNetZip Saving to Stream
Solution 1
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.
Solution 2
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
Updated on June 29, 2020Comments
-
user229133 almost 4 years
I am using DotNetZip to add a file from a
MemoryStream
to a zip file and then to save that zip as aMemoryStream
so that I can email it as an attachment. The code below does not err but theMemoryStream
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 }
-
user229133 almost 12 yearsWhen I get the memory stream named "ms" to disk and try to unzip it I get the error "The archive is either in unknown format or damaged". Are you talking about the memory stream "ms" or "memStream"?
-
Sam I am says Reinstate Monica almost 12 years@user229133 I've just edited my post to show you how I saved ms to disk. Try that
-
SerG over 9 yearsInstead of last two lines a Using-statment should be used over
ms
.