C# 尝试使用c和Ionic.dll将两个zip文件合并为1
这是密码C# 尝试使用c和Ionic.dll将两个zip文件合并为1,c#,dotnetzip,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 (ZipEnt
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似乎没有被使用,我对所有的dispose/deletes都不太了解。但好的一面是,我确实让您的代码正常工作了,主要问题是您没有调用zipold的save方法
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();
并扩展类
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/
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();
}
}
}