0

这是代码

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

4

2 回答 2

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();
于 2012-04-27T13:40:08.143 回答
-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

于 2013-06-29T12:53:42.157 回答