Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Telerik从memoryStream制作Zip文件_C#_.net_Telerik_Zip - Fatal编程技术网

C# Telerik从memoryStream制作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

我想用Telerik Wpf zip实用程序压缩内存流。我从接受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
            }
        }

任何帮助都将不胜感激

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


    }