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



这是代码

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文件

你的代码对我来说不是 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();
    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

最新更新