我想用Telerik Wpf Zip实用程序压缩MemoryStream。我通过复制接受ms文件的Telerik文档,使用Zip Archive的名称和密码
来制作以下方法 private static MemoryStream MakeZipFile(MemoryStream ms, string ZipFileName, string pass)
{
MemoryStream msZip = new MemoryStream();
DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings();
encryptionSettings.Password = pass;
using (ZipArchive archive = new ZipArchive(msZip, ZipArchiveMode.Create, false, null, null, encryptionSettings))
{
using (ZipArchiveEntry entry = archive.CreateEntry(ZipFileName))
{
// Here I don't know how to add my ms to the archive, and return msZip
}
}
任何帮助将不胜感激。
答案 0 :(得分:2)
经过几天的搜索,终于找到了答案。我的问题是将LeaveOpen选项存档为false。无论如何,以下工作正在进行中:
private static MemoryStream MakeZipFile(MemoryStream ms, string ZipFileName, string pass)
{
MemoryStream zipReturn = new MemoryStream();
ms.Position = 0;
try
{
DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings { Password = pass };
// Must make an archive with leaveOpen to true
// the ZipArchive will be made when the archive is disposed
using (ZipArchive archive = new ZipArchive(zipReturn, ZipArchiveMode.Create, true, null, null, encryptionSettings))
{
using (ZipArchiveEntry entry = archive.CreateEntry(ZipFileName))
{
using (BinaryWriter writer = new BinaryWriter(entry.Open()))
{
byte[] data = ms.ToArray();
writer.Write(data, 0, data.Length);
writer.Flush();
}
}
}
zipReturn.Seek(0, SeekOrigin.Begin);
}
catch (Exception ex)
{
string strErr = ex.Message;
zipReturn = null;
}
return zipReturn;
}