【问题标题】:How to copy file from zipArchive into another zipArchive without unzipping - in c# [closed]如何在不解压缩的情况下将文件从 zipArchive 复制到另一个 zipArchive - 在 c# 中 [关闭]
【发布时间】:2021-09-02 15:05:40
【问题描述】:

我想将来自zipArchive1 的条目添加到zipArchive2 而不解压缩zipArchive1

using (var zipArchive1 = ZipFile.Open(zipFile1, ZipArchiveMode.Read))
{
    using (var zipArchive2 = ZipFile.Open(zipFile2, ZipArchiveMode.Update))
    {
        var entry = zipArchive1.GetEntry("fileName");
        
        // I want to do something like
        // zipArchive2.Add(entry) 
    }
}

【问题讨论】:

  • 好的,那么你从你的尝试中得到了什么错误?
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 以下 Srecko Ristic 的提议可行!

标签: c# .net zipfile


【解决方案1】:

这里是:

using (ZipArchive sourceArchive = ZipFile.OpenRead(sourceZip))
{
    var entry = sourceArchive.GetEntry(fileFromSource);
    using (ZipArchive destArchive = ZipFile.Open(destZip, ZipArchiveMode.Update))
    {
        using (var existinFileStream = entry.Open())
        {
            var newFile = destArchive.CreateEntry(entry.FullName);
            using (var newFileStream = newFile.Open())
            {
                existinFileStream.CopyTo(newFileStream);
            }
        }
    }
}

sourceZipdestZip 是您的 zip 文件的路径,fileFromSource 是源存档中文件的名称。

【讨论】:

  • 非常感谢,成功了!
猜你喜欢
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
相关资源
最近更新 更多