解压缩zip文件并覆盖(在同一目录-C#中)
我正在开始一些C#的东西,我想提取并强制覆盖zip存档中的所有文件。我知道Stack中还有很多其他解决方案,但没有一个适合我:/ 我尝试过这种方法:解压缩zip文件并覆盖(在同一目录-C#中),c#,zip,extract,updates,archive,C#,Zip,Extract,Updates,Archive,我正在开始一些C#的东西,我想提取并强制覆盖zip存档中的所有文件。我知道Stack中还有很多其他解决方案,但没有一个适合我:/ 我尝试过这种方法: try { string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip"); Console.WriteLine("Zip's path: " + zipPath); string extractP
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
}
这个提取器工作正常,但不允许我在提取的同时覆盖文件,它返回错误和异常。。。我试着看看,但没有成功
有人知道它是怎么工作的吗?试试这样的方法。远离我的开发框,所以这可能需要一些调整,只是从内存中编写它 编辑:正如有人提到的,您可以使用具有覆盖选项的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
}
编辑,在事件中目录被解压缩(同样,可能需要调整,我没有测试它):
如果文件/文件夹处于打开状态或是其他人拥有该文件/文件夹,则无法写入该文件/文件夹。这是Windows的一个问题,恰好适用于解压方法。@jdweng感谢您的回复!我正在尝试提取只覆盖无人打开的已关闭文件!如果新文件和旧文件是相同的,那么您如何知道它们是否被覆盖?@jdweng它们怎么可能是相同的?让我们按照我的例子:我有一个文件夹,其中有一个名为“random.txt”的文本文件,还有一个“update.zip”存档,其中包含另一个“random.txt”。这两个文件“random.txt”的内容不同,即使名称相同。我需要提取zip存档中的,并替换存档中的。你是对的。Extract ToDictor无法覆盖现有的文件异常。所以你必须使用ExtractToFile方法,覆盖参数设置为true。。。哇!回答得真好,兄弟!它起作用了!现在,我只需要找出如何删除拉链一旦提取!真的很有帮助!没有那么难,只是一个简单的“File.Delete(File_name);”就足够了!你的作品看起来不错,但在文件夹方面似乎有问题:如果我试图提取一个文件夹,然后替换它,它就不起作用了@Zenek是否要用文件夹的所有内容替换文件夹?如果是的话,那完全是另一个问题。你完全正确。可能我的问题发错了。我的意思是,我需要提取和替换目录中的每种类型的文件,即使它们位于子文件夹中!
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
}