Telerik 从 memoryStream 制作一个 Zip 文件

Telerik Making a Zip File From memoryStream

我想使用 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
            }
        }

任何帮助将不胜感激。

经过几天的搜索,我终于找到了答案。我的问题是通过将 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;


    }