2015-08-18 58 views
0

我想用Telerik Wpf Zip實用工具壓縮MemoryStream。我通過從接受ms文件,名稱爲Zip Archive的Telerik文檔複製以下方法,並且使用密碼Telerik從memoryStream製作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 
      } 
     } 

任何幫助將不勝感激。

回答

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; 


    }