2012-08-07 116 views
2

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

回答

0

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

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

+0

我遵循这些示例来创建我在此处发布的代码。此时不管它是否覆盖,上面的代码根本不会改变目标zip文件的状态。如果它是覆盖,至少我会有东西来调试......我不需要使用memeory流。我的意思是“不使用磁盘”是因为我无法将一个zip文件的内容提取到硬盘上,然后将其重新打包到另一个zip文件中。由于windows文件的命名规则,它必须从一个zip文件移动到下一个文件,而不需要在HD上解压缩。 – 2012-08-07 21:38:59

+0

我使用了File.OpwnWrite()的新FileStream(outArchive,FileMode.Open)。它根本没有改变结果。 – 2012-08-07 21:45:43

1

我使用不同的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; 
     } 
    } 
+0

它通过在给定目录中使用临时文件(tmp.tmp)来解决文件名问题,然后在文件夹中给出文件的原始名称 – 2012-08-08 05:07:08

+0

您的代码可以正常工作,但是..我为您准备了一些注释。 1.您的代码为每个添加的条目保存一次输出文件。这是不必要的。 2.你可以避免完全写入文件系统。详情请参阅我的答案。 – Cheeso 2012-10-30 17:58:45

+0

我不得不打电话给那里:(在我的测试中,当我排队列出要添加的文件并试图一次添加它们时,无论出于何种原因,只需一次调用Save()方法,该文件保持不变,我很清楚我在那里所做的巨大的性能打击,但不幸的是我无法以合适的方式工作。 – 2012-11-05 19:15:35

0

以下是一些简单的代码,它将从输入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()会在实际的zip数据上打开一个数据流,该数据会随着读取和解压缩进行。您可以在任何时间,每个ZipFile中只打开其中一个。 getInStreamReturner每次通过循环创建一个新函数,从而创建一个闭包以保留zipEntryName的值以供在ZipFile.Save时参考。

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

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


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