簡體   English   中英

Telerik從memoryStream制作一個Zip文件

[英]Telerik Making a Zip File From memoryStream

我想用Telerik Wpf Zip實用程序壓縮MemoryStream。 我通過從接受一個ms文件,名為Zip存檔和密碼的ms的Telerik文檔中復制來進行以下方法

       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;


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM