如何从gziptream生成的字节数组创建gz zip文件

时间:2019-05-11 11:14:35

标签: .net .net-core compression gzipstream c#-7.1

我有.net核心应用程序,其中我在内存中用三个文件创建zip并将其以字节数组发送到另一个客户端应用程序。 现在,我正在努力在客户端应用程序中将此字节数组保存到.gz zip文件中。 我创建存档的代码是

byte[] fileBytes = null;
 using (MemoryStream memoryStream = new MemoryStream())
            {
                // create a zip
                using (ZipArchive zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true, Encoding.UTF8))
                {
                    // iterate through the source files
                    foreach (var f in obj.CsvDictionary)
                    {
                        // add the item name to the zip
                        ZipArchiveEntry zipItem = zip.CreateEntry(f.Key);//f.key contains file name
                        // add the item bytes to the zip entry by opening the original file and copying the bytes 
                        using (var originalFileMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(f.Value.ToString())))//f.value contains file contents
                        {
                            using (var entryStream = zipItem.Open())
                            {
                                originalFileMemoryStream.CopyTo(entryStream);
                            }
                        }
                    }
                }
                fileBytes = memoryStream.ToArray();
            }

1 个答案:

答案 0 :(得分:0)

就像

一样简单
File.WriteAllBytes(@"D:\Temp\Myfile.zip", myrequestarray);

https://www.codeproject.com/Questions/666195/Byte-Array-to-Zip-File