gpt4 book ai didi

c# - 提取 zip 文件并覆盖(在同一目录中 - C#)

转载 作者:行者123 更新时间:2023-12-05 03:47:19 25 4
gpt4 key购买 nike

我正在开始一些 C# 的东西,我想从 zip 存档中提取并强制覆盖所有文件。我知道 Stack 中还有许多其他解决方案,但对我来说没有任何用处:/

我试过这个方法:

try
{
string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
Console.WriteLine("Zip's path: " + zipPath);
string extractPath = Directory.GetCurrentDirectory();

ZipFile.ExtractToDirectory(zipPath, extractPath);
return (0); // 0 all fine
}
catch (Exception)
{
return (1); // 1 = extract error
}

这个提取器工作正常,但不允许我在提取的同时覆盖文件,并且它返回错误和异常...我试着看一下 MS-Documention , 没有成功 ...

有人知道它是如何工作的吗?

最佳答案

尝试这样的事情。远离我的开发箱,所以这可能需要一些调整,只需从内存中写入即可。

编辑:正如有人提到的,您可以使用具有覆盖选项的 ExtractToFile。 ExtractToDirectory 没有。

本质上,您解压缩到一个临时文件夹,然后检查解压缩文件的名称是否已存在于目标文件夹中。如果是,它会删除现有文件并将新解压缩的文件移动到目标文件夹。

    try
{
string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
Console.WriteLine("Zip's path: " + zipPath);
//Declare a temporary path to unzip your files
string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
string extractPath = Directory.GetCurrentDirectory();
ZipFile.ExtractToDirectory(zipPath, tempPath);

//build an array of the unzipped files
string[] files = Directory.GetFiles(tempPath);

foreach (string file in files)
{
FileInfo f = new FileInfo(file);
//Check if the file exists already, if so delete it and then move the new file to the extract folder
if (File.Exists(Path.Combine(extractPath,f.Name)))
{
File.Delete(Path.Combine(extractPath, f.Name));
File.Move(f.FullName, Path.Combine(extractPath, f.Name));
}
else
{
File.Move(f.FullName, Path.Combine(extractPath, f.Name));
}
}
//Delete the temporary directory.
Directory.Delete(tempPath);
return (0); // 0 all fine
}
catch (Exception)
{
return (1); // 1 = extract error
}

编辑,在解压缩事件目录时(同样,可能需要调整,我没有测试):

        try
{
string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
Console.WriteLine("Zip's path: " + zipPath);
//Declare a temporary path to unzip your files
string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
string extractPath = Directory.GetCurrentDirectory();
ZipFile.ExtractToDirectory(zipPath, tempPath);

//build an array of the unzipped directories:
string[] folders = Directory.GetDirectories(tempPath);

foreach (string folder in folders)
{
DirectoryInfo d = new DirectoryInfo(folder);
//If the directory doesn't already exist in the destination folder, move it to the destination.
if (!Directory.Exists(Path.Combine(extractPath,d.Name)))
{
Directory.Move(d.FullName, Path.Combine(extractPath, d.Name));
continue;
}
//If directory does exist, iterate through the files updating duplicates.
else
{
string[] subFiles = Directory.GetFiles(d.FullName);
foreach (string subFile in subFiles)
{
FileInfo f = new FileInfo(subFile);
//Check if the file exists already, if so delete it and then move the new file to the extract folder
if (File.Exists(Path.Combine(extractPath, d.Name, f.Name)))
{
File.Delete(Path.Combine(extractPath, d.Name, f.Name));
File.Move(f.FullName, Path.Combine(extractPath, d.Name, f.Name));
}
else
{
File.Move(f.FullName, Path.Combine(extractPath, d.Name, f.Name));
}
}
}
}

//build an array of the unzipped files in the parent directory
string[] files = Directory.GetFiles(tempPath);

foreach (string file in files)
{
FileInfo f = new FileInfo(file);
//Check if the file exists already, if so delete it and then move the new file to the extract folder
if (File.Exists(Path.Combine(extractPath,f.Name)))
{
File.Delete(Path.Combine(extractPath, f.Name));
File.Move(f.FullName, Path.Combine(extractPath, f.Name));
}
else
{
File.Move(f.FullName, Path.Combine(extractPath, f.Name));
}
}
Directory.Delete(tempPath);
return (0); // 0 all fine
}

关于c# - 提取 zip 文件并覆盖(在同一目录中 - C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64897828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com