【问题标题】:How to create a zip file in memory, starting from file bytes?如何从文件字节开始在内存中创建一个 zip 文件?
【发布时间】:2015-09-28 21:41:18
【问题描述】:

我尝试使用 c# 在内存中创建一个 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;

【问题讨论】:

  • 你的代码有什么问题?
  • 我无法从 zip 中提取文件。 wirar 说文件已损坏

标签: c# zip compression


【解决方案1】:

试着看看这个: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.ToArray()代替zipArchiveMemoryStream.GetBuffer()

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 好吧,对不起!谢谢你的建议!
  • 没问题,作为建议! ;)
  • 我再试一次,但我已经阅读了链接的文章。我的不同之处在于我没有物理文件,只有字节。在压缩之前我无法将其保存在硬盘上。我会告诉你的
  • 好的,试试zipArchiveMemoryStream.GetBuffer(),而不是zipArchiveMemoryStream.ToArray()
猜你喜欢
  • 2014-11-16
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 2021-10-09
  • 2019-05-20
相关资源
最近更新 更多