2012-04-26 35 views
0

這裏兩個zip文件合併成1是代碼嘗試使用C#和Ionic.dll

ZipFile zipnew = ZipFile.Read(strPath); 
if (!File.Exists(path)) 
{ 
    using (ZipFile zip = new ZipFile()) 
    { 
     zip.Save(path); 
    } 
} 
    string tmpname = fpath + "\\abtemp"; 
    ZipFile zipold = ZipFile.Read(path); 
    foreach (ZipEntry zenew in zipnew) 
    { 
     string flna = zenew.FileName.ToString(); 
     string tfn = '@' + flna.Replace("\\", "/"); 
     Stream fstream = File.Open(tmpname, FileMode.OpenOrCreate, FileAccess.Write); 
     zenew.Extract(fstream); 
     string l = fstream.Length.ToString(); 
     fstream.Close(); 

     using (StreamReader sr = new StreamReader(tmpname)) 
     { 
      var zn = zipold.UpdateEntry(flna, sr.BaseStream); 
      sr.Close(); 
      sr.Dispose(); 
      fstream.Dispose(); 
     } 
    } 

    zipnew.Dispose(); 
    File.Delete(tmpname); 
    File.Delete(strPath); 

的問題是:我沒有得到任何錯誤,也沒有文件合併到zipold從zipnew。 Zipold是一個空白的zip文件

回答

1

你的代碼對我來說不是100%清楚,變量tfn似乎沒有被使用,我並不完全遵循所有的dispose/deletes。
但在光明的一面,我確實讓你的代碼工作,主要問題是你沒有調用zipold的保存方法。

string path = "d:\\zipold.zip"; 
    ZipFile zipnew = ZipFile.Read("d:\\zipnew.zip"); 
    if (!File.Exists(path)) 
    { 
     using (ZipFile zip = new ZipFile()) 
     { 
      zip.Save(path); 
     } 
    } 
    string tmpname = "d:" + "\\temp.dat"; 
    ZipFile zipold = ZipFile.Read(path); 
    foreach (ZipEntry zenew in zipnew) 
    { 
     string flna = zenew.FileName.ToString(); 
     //string tfn = '\\' + flna.Replace("\\", "/"); useless line 
     Stream fstream = File.Open(tmpname, FileMode.OpenOrCreate, FileAccess.Write); 
     zenew.Extract(fstream); 
     string l = fstream.Length.ToString(); 
     fstream.Close(); 
     using (StreamReader sr = new StreamReader(tmpname)) 
     { 
      var zn = zipold.UpdateEntry(flna, sr.BaseStream); 
      zipold.Save(); 
      sr.Close(); 
      sr.Dispose(); 
      fstream.Dispose(); 
     } 

    } 
    zipnew.Dispose(); 
+0

謝謝,這做到了。 – Joe 2012-04-28 12:42:53

-1
static void Main(string[] args) 
    { 
     try 
     { 
      using (ZipFile zip1 = new ZipFile()) 
      { 
       zip1.AddFile(@"SCAN0002.PDF"); 
       zip1.AddFile(@"SCAN0003.PDF"); 
       zip1.Save("SCAN0002.ZIP"); 
      } 

      using (ZipFile zip2 = new ZipFile()) 
      { 
       zip2.AddFile(@"SCAN0004.PDF"); 
       zip2.AddFile(@"SCAN0005.PDF"); 
       zip2.AddFile(@"SCAN0006.PDF"); 
       zip2.Save("SCAN0003.ZIP"); 
      } 

      ZipFile z3 = new ZipFile().Read2(File.ReadAllBytes("SCAN0002.ZIP")); 
      ZipFile z4 = new ZipFile().Read2(File.ReadAllBytes("SCAN0003.ZIP")); 
      using (ZipFile zip3 = new ZipFile()) 
      { 
       zip3.Marge(z3).Marge(z4); 
       zip3.Save("SCAN0004.ZIP"); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 

和擴展類

public static class ZipFileExt 
{ 
    public static ZipFile Read2(this ZipFile item, byte[] data) 
    { 
     return ZipFile.Read(new MemoryStream(data)); 
    } 
    public static ZipFile Marge(this ZipFile item, ZipFile file) 
    { 
     foreach (var entry in file) 
      item.AddEntry(entry.FileName, entry.Extract2Byte()); 
     return item; 
    } 
    public static byte[] Extract2Byte(this ZipEntry entry) 
    { 
     using (var ms = new MemoryStream()) 
     { 
      entry.Extract(ms); 
      return ms.ToArray(); 
     } 
    } 
} 

\ P/

http://www.youtube.com/watch?v=Y7dRBmMsevk&list=RD02xcFzsvnMmXY