尝试使用c#和Ionic.dll将两个zip文件合并为1

时间:2012-04-26 16:18:58

标签: c# dotnetzip

这是代码

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

问题是:我没有收到任何错误,并且没有文件从zipnew合并到zipold中。 Zipold是一个空白的zip文件

2 个答案:

答案 0 :(得分:1)

你的代码对我来说并不是100%清楚,变量tfn似乎没有被使用,而且我并没有完全跟随所有处理/删除。
但是在光明的一面我确实让你的代码工作,主要的问题是你没有调用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();

答案 1 :(得分:-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