C# Telerik从memoryStream制作Zip文件
我想用Telerik Wpf zip实用程序压缩内存流。我从接受ms文件的Telerik文档复制了以下方法,该文件的名称为Zip存档,密码为C# Telerik从memoryStream制作Zip文件,c#,.net,telerik,zip,C#,.net,Telerik,Zip,我想用Telerik Wpf zip实用程序压缩内存流。我从接受ms文件的Telerik文档复制了以下方法,该文件的名称为Zip存档,密码为 private static MemoryStream MakeZipFile(MemoryStream ms, string ZipFileName, string pass) { MemoryStream msZip = new MemoryStream(); DefaultEncryptionSe
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
}
}
任何帮助都将不胜感激 经过几天的搜索,我终于找到了答案。我的问题是将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;
}