【问题标题】:Telerik Making a Zip File From memoryStreamTelerik 从 memoryStream 制作 Zip 文件
【发布时间】:2015-08-18 18:41:22
【问题描述】:

我想用 Telerik Wpf Zip 实用程序压缩 MemoryStream。我通过从接受 ms 文件的 Telerik 文档中复制来制作以下方法,该文件的名称为 Zip 存档和密码

       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
            }
        }

任何帮助将不胜感激。

【问题讨论】:

    标签: c# .net telerik zip


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多