【问题标题】:Extract zip entries to another Zip file将 zip 条目提取到另一个 Zip 文件
【发布时间】:2012-08-07 04:57:54
【问题描述】:

谁能告诉我为什么下面的代码不起作用?我正在为 Zip 流使用 SharpZipLib API,这是今天从他们的网站上下载的最新版本。我试图使用这种逻辑将一个 zip 文件的内容合并到另一个中,而不必在磁盘上执行 IO,因为预期的 zip 文件可能包含为 windows 保留的文件名。我已经尝试过使用多个不同的源和目标 zip 文件(包含保留名称和不包含保留名称的文件)。代码没有抛出任何异常,如果你在每次写操作之前检查缓冲区,你可以看到它包含真实数据,但整个操作完成后目标zip文件的大小没有改变,你可以探索它确认没有新文件(代码应该添加的文件)实际上已添加到目标文件中。 :(

    public static void CopyToZip(string inArchive, string outArchive)
    {

        ZipOutputStream outStream = null;
        ZipInputStream inStream = null;
        try
        {
            outStream = new ZipOutputStream(File.OpenWrite(outArchive));
            outStream.IsStreamOwner = false;
            inStream = new ZipInputStream(File.OpenRead(inArchive));
            ZipEntry currentEntry = inStream.GetNextEntry();
            while (currentEntry != null)
            {

                byte[] buffer = new byte[1024];
                ZipEntry newEntry = new ZipEntry(currentEntry.Name);
                newEntry.Size = currentEntry.Size;
                newEntry.DateTime = currentEntry.DateTime;
                outStream.PutNextEntry(newEntry);
                int size = 0;
                while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, size);
                }
                outStream.CloseEntry();

                currentEntry = inStream.GetNextEntry();
            }
            outStream.IsStreamOwner = true;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            try { outStream.Close(); }
            catch (Exception ignore) { }
            try { inStream.Close(); }
            catch (Exception ignore) { }
        }      
    }

【问题讨论】:

    标签: c# sharpziplib


    【解决方案1】:

    我最终使用不同的 API 来执行此操作。来自http://dotnetzip.codeplex.com/ 的DotNet zip。这是实现:

        public static void CopyToZip(string inArchive, string outArchive, string tempPath)
        {
            ZipFile inZip = null;
            ZipFile outZip = null;
    
            try
            {
                inZip = new ZipFile(inArchive);
                outZip = new ZipFile(outArchive);
                List<string> tempNames = new List<string>();
                List<string> originalNames = new List<string>();
                int I = 0;
                foreach (ZipEntry entry in inZip)
                {
                    if (!entry.IsDirectory)
                    {
                        string tempName = Path.Combine(tempPath, "tmp.tmp");
                        string oldName = entry.FileName;
                        byte[] buffer = new byte[4026];
                        Stream inStream = null;
                        FileStream stream = null;
                        try
                        {
                            inStream = entry.OpenReader();
                            stream = new FileStream(tempName, FileMode.Create, FileAccess.ReadWrite);
                            int size = 0;
                            while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                stream.Write(buffer, 0, size);
                            }
                            inStream.Close();
                            stream.Flush();
                            stream.Close();
                            inStream = new FileStream(tempName, FileMode.Open, FileAccess.Read);
    
                            outZip.AddEntry(oldName, inStream);
                            outZip.Save();
                        }
                        catch (Exception exe)
                        {
                            throw exe;
                        }
                        finally
                        {
                            try { inStream.Close(); }
                            catch (Exception ignore) { }
                            try { stream.Close(); }
                            catch (Exception ignore) { }
                        }
                    }
                }
    
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    

    【讨论】:

    • 它通过在给定目录中使用临时文件 (tmp.tmp) 来解决文件名问题,然后在存档中为该文件指定原始名称
    • 您的代码可以正常工作,但是 .. 我有几个注意事项给您。 1.您的代码为添加的每个条目保存一次输出文件。这是不必要的。 2.您可以完全避免写入文件系统。详情见我的回答。
    • 我不得不在那里调用保存:(。在我的测试期间,当我将要添加的文件列表排队并尝试一次添加它们时,只需调用一次 Save( ) 方法,无论出于何种原因,文件保持不变。我很清楚我在保存的地方受到了巨大的性能影响,但不幸的是我无法让它以适当的方式工作。
    【解决方案2】:

    我看到的一个问题是您正在使用 File.OpenWrite() 打开输出 zip 文件,这将替换现有的输出文件,而不是将新条目合并到其中。

    SharpDevelop Wiki 上有一个示例,该示例提供了使用内存流更新 zip 文件的示例。可以在http://wiki.sharpdevelop.net/SharpZipLib_Updating.ashx#Updating_a_zip_file_in_memory_1找到它

    【讨论】:

    • 我已经按照这些示例创建了我在此处发布的代码。此时无论是否覆盖都没有关系,上面的代码根本不会改变目标zip文件的状态。如果它被覆盖,至少我会有一些东西要调试......我不需要使用内存流。我所说的“不使用磁盘”的意思是我无法将一个 zip 的内容提取到硬盘驱动器上,然后将其重新打包到另一个 zip 中。由于 Windows 文件命名约定,它必须从一个 zip 移动到下一个而不在 HD 上解压缩。
    • 我使用 new FileStream(outArchive, FileMode.Open) 代替 File.OpwnWrite()。它根本没有改变结果。
    【解决方案3】:

    以下是一些更简单的代码,它们将从输入 zip 读取并写入可能已经存在的输出 zip。它不需要将临时数据写入文件系统。

      public static void CopyToZip(string inArchive, string outArchive)
      {
          using (inZip = new ZipFile(inArchive),
                 outZip = new ZipFile(outArchive))
          {
              Func<String,Func<String,Stream>> getInStreamReturner = (name) => {
                  return new Func<String,Stream>(a){ return inZip[a].OpenReader(); };
              };
              foreach (ZipEntry entry in inZip)
              {
                  if (!entry.IsDirectory)
                  {
                      string zipEntryName = entry.FileName;
                      outZip.AddEntry(zipEntryName,
                                      getInStreamReturner(zipEntryName),
                                      (name, stream) => stream.Close() );
                  }
              }
              outZip.Save();
          }
      }
    

    注意事项:

    1. 此方法使用ZipFile.AddEntry 重载,它接受两个委托:一个开启者和一个关闭者。这些函数在ZipFile.Save 时被调用。前一个委托需要打开并返回包含要压缩的数据的流。后一个委托只需要关闭流。

    2. 需要定义getInStreamReturner Func,以便在ZipFile.Save时打开正确的流。请记住,zipEntryName 每次通过循环都会更改值。此外,ZipEntry.OpenReader() 会在实际 zi​​p 数据上打开一个流,该流会在运行时进行读取和解压缩。在任何时候,每个 ZipFile 只能打开其中一个。 getInStreamReturner 每次循环都会创建一个新函数,从而创建一个闭包来保留zipEntryName 的值,以供ZipFile.Save 时参考。

    3. 如果 inArchive 和 outArchive 之间存在名称冲突,此方法将失败。为避免这种情况,您需要检查并以某种方式避免它。要么设计一个新的、唯一的名称,要么跳过将具有重复名称的条目添加到 outarchive 中。

    4. 我没有测试过这个。


    虽然这种方法不会写入文件系统,但它会解压缩并重新压缩文件数据。有一个开放的请求向 DotNetZip 提供一项功能,以在没有解压缩/重新压缩跳转的情况下迁移条目。我还没有实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多