我正在使用 DotNetZip 将文件从 a 添加MemoryStream
到 zip 文件,然后将该 zip 保存为 a MemoryStream
,以便我可以将其作为附件通过电子邮件发送。下面的代码没有错误,但MemoryStream
不能正确完成,因为它不可读。当我将 zip 保存到硬盘驱动器时,一切正常,只是当我尝试将其保存到流时却不行。
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
}