gpt4 book ai didi

c# - DotNetZip 保存到流

转载 作者:太空狗 更新时间:2023-10-29 17:33:22 26 4
gpt4 key购买 nike

我正在使用 DotNetZip 将文件从 MemoryStream 添加到 zip 文件,然后将该 zip 保存为 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
}

最佳答案

好吧,我想出了我的问题,实际上很愚蠢。感谢大家的帮助!

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();

关于c# - DotNetZip 保存到流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11248096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com