我使用DotNetZip将文件从一个文件添加MemoryStream到一个zip文件,然后将该zip文件保存为一个,MemoryStream以便我可以将其作为附件发送.下面的代码没有错误,但MemoryStream必须做得不对,因为它是不可读的.当我将拉链保存到我的硬盘驱动器时,一切都很完美,只是当我尝试将其保存到流中时.
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
}
Run Code Online (Sandbox Code Playgroud)
use*_*133 14
好吧,我想出了我的问题,实际上非常愚蠢.谢谢大家的帮助!
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();
Run Code Online (Sandbox Code Playgroud)
我复制了你的代码,然后将你的最终内存流保存到磁盘上data.txt.这对我来说是完全不可读的,但后来我意识到它不是一个文本文件,它是一个zip文件,所以我把它保存为data.zip并且按预期工作
我用来将ms保存到磁盘的方法如下(紧跟在你的zip.Save(ms);行之后)
ms.Position = 0;
byte[] data = ms.ToArray();
File.WriteAllBytes("data.zip", data);
Run Code Online (Sandbox Code Playgroud)
所以,我相信你的内存流是应该的,它是压缩文本.在解压缩之前它是不可读的.
| 归档时间: |
|
| 查看次数: |
19521 次 |
| 最近记录: |