【问题标题】:.net core unzip a file and zip subfolder.net core 解压文件并压缩子文件夹
【发布时间】:2019-05-07 19:00:27
【问题描述】:

我有一个 zip 文件的字节 []。我需要解压缩内存中的文件并在 zip 中找到“dist”文件夹(总会有一个 dist 文件夹),然后使用该“dist”文件夹中的任何内容创建一个新的 zip 文件。

“dist”文件夹中可以包含文件和子文件夹。我需要 byte[] 中的新 zip 文件,因此只能在内存中进行操作,而无法访问物理磁盘。

我尝试使用“SharpZipLib”没有运气。 我正在使用.net core 2.0

【问题讨论】:

  • 你能分享一下你试过的代码吗?
  • 老实说没什么好分享的。
  • 为什么不使用开箱即用的框架提供的 ZipArchive 呢? docs.microsoft.com/en-us/dotnet/api/…
  • 似乎在 net core 和 bytes[] 上不起作用

标签: .net .net-core zip compression sharpziplib


【解决方案1】:

请尝试以下代码

   using (MemoryStream srcMemoryStream = new MemoryStream())
        {
            using (MemoryStream targetMemoryStream = new MemoryStream())
            {
                // to have a byte array, I just read a file and store it into a memory stream
                using (FileStream sourceZipFile = new FileStream(@"f:\source-file.zip", FileMode.Open))
                {
                    sourceZipFile.CopyTo(srcMemoryStream);
                }

                using (ZipArchive srcArchive = new ZipArchive(srcMemoryStream, ZipArchiveMode.Read))
                {
                    using (ZipArchive destArchive = new ZipArchive(targetMemoryStream, ZipArchiveMode.Create, true))
                    {

                        srcArchive.Entries
                            .Where(entry => entry.FullName.Contains("dist/"))
                            .ToList()
                            .ForEach((entry) =>
                            {
                                // i simply create the same folder with the same structure in other archive
                                // if you want to change the structure, you have to rename or remove parts of 
                                // the path like below
                                ///  var newEntryName = entry.FullName.Replace("files/dist/", "new-dist/");
                                ///  ZipArchiveEntry newEntry = destArchive.CreateEntry(newEntryName);
                                ZipArchiveEntry newEntry = destArchive.CreateEntry(entry.FullName);
                                using (Stream srcEntry = entry.Open())
                                {
                                    using (Stream destEntry = newEntry.Open())
                                    {
                                        srcEntry.CopyTo(destEntry);
                                    }
                                }
                            });
                    }
                }

                // i just write the zip file on disk to make sure that it works, your desire state is already achieved
                // before this line of code, and the result byte Array is inside the targetMemoryStream memory stream
                using (FileStream fs = new FileStream(@"f:/destination-file.zip", FileMode.Create))
                {
                    targetMemoryStream.WriteTo(fs);
                    targetMemoryStream.Flush();
                    fs.Flush(true);
                }

            }
        }

您可以将这些字节数组存储在内存流中,我只是读取一个文件并将其存储到内存流中,但您可以简单地使用以下代码:

MemoryStream srcMemoryStream = new MemoryStream();
srcMemoryStream.Write(byteArray, 0, byteArray.Length);

【讨论】:

  • 这很好用,谢谢,只有一件事,新的 zip 文件中有一个 dist 文件夹。如何将 dist 文件夹项放在 zip 文件夹的根目录中
  • 不客气,请阅读评论,我已经在评论中提到了这一点。如果要删除路径的一部分,只需将其替换为 string empty => var newEntryName = entry.FullName.Replace("dist/", string.Empty);
猜你喜欢
  • 2010-09-05
  • 2013-03-09
  • 2010-12-14
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
相关资源
最近更新 更多