如何从文件字节开始在内存中创建一个zip文件? [英] How to create a zip file in memory, starting from file bytes?

查看:91
本文介绍了如何从文件字节开始在内存中创建一个zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to create a zip file in memory with c#, but the result is a zip file with corrupted files inside it. All file to zip are in a database. I store the bytes. The files are PDF. my code is the following

//[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]

预先感谢您的支持

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

SOLUTION BY TONIGNO: the problem was that i use GetBuffer(); instead 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使用System.IO.Compression在内存中存档。解决方案是这样的:

Try to take a look to this: Creating a ZIP Archive in Memory Using System.IO.Compression. The solution is this:

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创建了包含以下解决方案的无效ZIP文件

It explains how to create the zip archive in memory and contains a link to another useful article that explains the use of the leaveOpen argument to prevent the closing of the stream: ZipArchive creates invalid ZIP file that contains this solution:

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()

这篇关于如何从文件字节开始在内存中创建一个zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆