我有一个程序需要提取.zip文件。(程序是用.NET Framework 4.7.2编写的。)在每个.zip中有不同的文件夹和文件。当程序提取-- .zip时,需要覆盖、旧文件夹和文件。
我使用的是:ZipFile.ExtractToDirectory(ExtractSource, ExtractDestination),唯一的问题是我不能用它覆盖文件。在寻找了很长一段时间之后,什么也没有起作用。
我想用:ZipFile.ExtractToDirectory(ExtractSource, ExtractDestination, true)。
其中,代码中的"True“布尔值会导致文件被覆盖,但不能在.NET框架中使用该代码。有没有类似于此的代码,我可以用它代替我的代码?
我找到了大部分的代码这里。
有人能帮我找到解决办法吗?谢谢!
发布于 2021-08-03 08:33:23
我能够使用库:.NET Framework提取和覆盖文件(在ICSharpCode.SharpZipLib中)
安装:https://www.nuget.org/packages/SharpZipLib/1.3.2
有用代码:https://github.com/icsharpcode/SharpZipLib/wiki/FastZip
发布于 2022-06-21 11:47:44
//This code will not need .Net core (I tested this on .Net framework 4.8)
string zipPath = "zip_path_of_choice";
string extractPath = "destination_dir_of_choice";
try
{
ZipArchive archive = ZipFile.OpenRead(zipPath);
foreach (ZipArchiveEntry entry in archive.Entries)
{
if(entry.FullName.EndsWith("/") || entry.FullName.EndsWith("\\"))
{
string entryFullName = entry.FullName.Replace('/', '\\');
string entryFullpath = Path.Combine(extractPath, entryFullName);
if (!Directory.Exists(entryFullpath))
Directory.CreateDirectory(entryFullpath);
}
else
{
string entryFullName = entry.FullName.Replace('/', '\\');
entry.ExtractToFile(Path.Combine(extractPath, entryFullName), true);
}
}
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message);
}https://stackoverflow.com/questions/68621340
复制相似问题