如何在内存中创建一个zip文件,从文件字节开始?

时间:2015-07-10 08:13:24

标签: c# zip compression

我尝试使用c#在内存中创建一个zip文件,但结果是一个zip文件,里面有损坏的文件。 所有zip文件都在数据库中。我存储字节。文件是PDF。 我的代码如下

//[extract bytes and file name for every file]

using (var zipArchiveMemoryStream = new MemoryStream())
{
    using (var zipArchive = new ZipArchive(zipArchiveMemoryStream,
                                           ZipArchiveMode.Create, true))
    {
        foreach (var file in fileData)
        {
            var zipEntry = zipArchive.CreateEntry(file.FileName);
            using (var entryStream = zipEntry.Open())
            {
                using (var tmpMemory = new MemoryStream(file.Bytes))
                {
                    tmpMemory.CopyTo(entryStream);
                };

            }
        }
    }
    zipArchiveMemoryStream.Position = 0;
    result = zipArchiveMemoryStream.GetBuffer();

//[download zip file]

提前感谢您的支持

TONIGNO解决方案: 问题是我使用GetBuffer();而是ToArray();

using (var zipArchiveMemoryStream = new MemoryStream())
        {
            using (var zipArchive = new ZipArchive(zipArchiveMemoryStream, ZipArchiveMode.Create, true))
            {
                foreach (var file in fileData)
                {
                    var zipEntry = zipArchive.CreateEntry(file.FileName);
                    using (var entryStream = zipEntry.Open())
                    {
                        using (var tmpMemory = new MemoryStream(file.Bytes))
                        {
                            tmpMemory.CopyTo(entryStream);
                        };

                    }
                }
            }

            zipArchiveMemoryStream.Seek(0, SeekOrigin.Begin);
            result = zipArchiveMemoryStream.ToArray();
        }

        return result;

1 个答案:

答案 0 :(得分:3)

试着看一下:Creating a ZIP Archive in Memory Using System.IO.Compression。解决方案是:

using (var memoryStream = new MemoryStream())
{
   using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
   {
      var demoFile = archive.CreateEntry("foo.txt");

      using (var entryStream = demoFile.Open())
      using (var streamWriter = new StreamWriter(entryStream))
      {
         streamWriter.Write("Bar!");
      }
   }

   using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
   {
      memoryStream.Seek(0, SeekOrigin.Begin);
      memoryStream.CopyTo(fileStream);
   }
}

它解释了如何在内存中创建zip存档,并包含指向另一篇有用文章的链接,该文章解释了如何使用leaveOpen参数来阻止关闭包含此解决方案的流:ZipArchive creates invalid ZIP file

using (MemoryStream zipStream = new MemoryStream())
{
    using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
    {
        var entry = zip.CreateEntry("test.txt");
        using (StreamWriter sw = new StreamWriter(entry.Open()))
        {
            sw.WriteLine(
            "Etiam eros nunc, hendrerit nec malesuada vitae, pretium at ligula.");
        }
    }

    var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
    "test.zip",
    CreationCollisionOption.ReplaceExisting);

    zipStream.Position = 0;
    using (Stream s = await file.OpenStreamForWriteAsync())
    {
        zipStream.CopyTo(s);
    }
}

我希望它有用!

修改

而不是zipArchiveMemoryStream.GetBuffer()使用zipArchiveMemoryStream.ToArray()