[英]ZipFile.CreateFromDirectory throws System.IO.IOException : The process cannot access the file X because it is being used by another process
Actually I'm trying to create zip file of a directory and but the ZipFile.CreateFromDirectory()
giving below Exception.实际上,我正在尝试创建目录的 zip 文件,但是
ZipFile.CreateFromDirectory()
给出了以下异常。
System.IO.IOException : The process cannot access the file PATH_TO_CREATE_ZIP/file.zip' because it is being used by another process.
System.IO.IOException:进程无法访问文件 PATH_TO_CREATE_ZIP/file.zip',因为它正被另一个进程使用。
Following is the Code Snippet for it.以下是它的代码片段。 :
:
public void createZipFile(string zipPath, string archiveFileName)
{
string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;
if (Directory.Exists(DirectoryToBeArchive + ".zip"))
{
File.Delete(DirectoryToBeArchive);
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
}
else
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
Directory.Delete(DirectoryToBeArchive);
}
Help Would be Much Appreciated.帮助将不胜感激。 Thanks in Advance.
提前致谢。 :)
:)
It only makes sense you get this exception. 您收到此异常才有意义。 Let's investigate your code step by step:
让我们逐步研究您的代码:
createZipFile("C:\\Temp", "myZipFile");
public void createZipFile(string zipPath, string archiveFileName)
{
//DirectoryToBeArchive = "C:\\Temp\\myZipFile"
string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;
//Some logical error here, you probably meant to use File.Exists()
//Basically, as you can't find a directory with name C:\\Temp\\myZipFile.zip, you always jump into else
if (Directory.Exists(DirectoryToBeArchive + ".zip"))
{
File.Delete(DirectoryToBeArchive);
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
}
else
//It will try to overwrite your existing "DirectoryToBeArchive".zip file
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
//This won't work as well btw, as there probably is no directory
//with name C:\\Temp\\myZipFile
Directory.Delete(DirectoryToBeArchive);
}
Though, even if you delete the file, you will probably hit same error. 但是,即使删除文件,也可能会遇到相同的错误。 The thing is when you try zipping the folder
C:\\\\Temp
into the file C:\\\\Temp\\\\myZipFile.zip
you will also try zipping the file itself. 问题是,当您尝试将文件夹
C:\\\\Temp
压缩到文件C:\\\\Temp\\\\myZipFile.zip
您还将尝试压缩文件本身。 That's actually where you get the file is being used error. 那实际上是您得到文件正在使用错误的地方。
So, 所以,
Replace Directory.Exists() with File.Exists() 用File.Exists()替换Directory.Exists()
Zip in another folder 在另一个文件夹中压缩
Just a friendly warning, I'd be cautious with Directory.Delete() if I were you :) 只是一个友好的警告,如果我是你,我会对Directory.Delete()保持谨慎:)
Correct Code : 正确的代码:
this piece of code after little correction worked for me.. 经过一点点纠正的这段代码对我有用。
string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;
if (File.Exists(DirectoryToBeArchive + ".zip"))
{
File.Delete(DirectoryToBeArchive + ".zip");
ZipFile.CreateFromDirectory(DirectoryToBeArchive, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
}
else
ZipFile.CreateFromDirectory(DirectoryToBeArchive, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
Directory.Delete(DirectoryToBeArchive , true);
My issue was, the output folder and zipping folder was the same.我的问题是,输出文件夹和压缩文件夹是一样的。
Moved to separate folders, now working fine.移动到单独的文件夹,现在工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.