将zip条目提取到另一个zip文件中

本文关键字:zip 另一个 文件 提取 | 更新日期: 2024-10-22 02:45:32

有人能告诉我为什么下面的代码不起作用吗?我正在为Zip流使用SharpZipLib API,今天从他们的网站上发布了最新版本的DL。我正试图使用此逻辑将一个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) { }
        }      
    }

将zip条目提取到另一个zip文件中

我最终使用了一个不同的API。DotNet zip来自http://dotnetzip.codeplex.com/.以下是实现:

    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;
        }
    }

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

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

下面是一些更简单的代码,它们将从输入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函数,以便在ZipFile.Save时打开正确的流。请记住,zipEntryName在循环中每次都会更改值。此外,ZipEntry.OpenReader()在实际zip数据上打开一个流,该流在进行时进行读取和解压缩。每个ZipFile在任何时候都只能打开其中一个。getInStreamReturner每次通过循环都会创建一个新函数,从而创建一个闭包来保留zipEntryName的值,以供在ZipFile.Save时参考。

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

  4. 我还没有测试过这个。


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