c# ZipFile.CreateFromDirectory - 进程无法访问文件“path_to_the_zip_file_created.zip”,因为它正被另一个进程使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19395128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# ZipFile.CreateFromDirectory - the process cannot access the file "path_to_the_zip_file_created.zip" because it is being used by another process
提问by DJD
Basic Code:
基本代码:
string startPath = @"C:\intel\logs";
string zipPath = @"C:\intel\logs-" + DateTime.Now.ToString("yyyy_dd_M-HH_mm_ss") + ".zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
Error: the process cannot access the file "path_to_the_zip_file_created.zip" because it is being used by another process.
错误:进程无法访问文件“path_to_the_zip_file_created.zip”,因为它正被另一个进程使用。
The above setup works fine on windows 7 where I have Visual Studio installed but I get the above error message when running on Windows Server 2008R2.
上述设置在安装了 Visual Studio 的 Windows 7 上运行良好,但在 Windows Server 2008R2 上运行时出现上述错误消息。
I have checked the antivirus logs and it does not block the application, nor does it lock the zip file that is created.
我检查了防病毒日志,它不会阻止应用程序,也不会锁定创建的 zip 文件。
回答by Kyle Johnson
I had the exact same problem. The workaround is to copy the folder you are zipping to another folder and point CreateFromDirectory there. Don't ask me why this works but it does.
我有同样的问题。解决方法是将您正在压缩的文件夹复制到另一个文件夹并在那里指向 CreateFromDirectory。不要问我为什么这行得通,但确实如此。
Directory.CreateDirectory(<new directory path>);
File.Copy(<copy contents into new folder>);
ZipFile.CreateFromDirectory(<new folder path>, <zipPath>);
回答by Emanuele Greco
//WRONG
ZipFile.CreateFromDirectory("C:\somefolder", "C:\somefolder\somefile.zip");
//RIGHT
ZipFile.CreateFromDirectory("C:\somefolder", "C:\someotherfolder\somefile.zip");
I use to do the same error: zipping a file into the same folder that I'm zipping.
This causes an error, of course.
我曾经犯过同样的错误:将文件压缩到我正在压缩的同一个文件夹中。
这当然会导致错误。
回答by Saad Farooq
The other answers, provide the correct reason, but I had a little problem in understanding them at the first sight. I cannot comment, so putting this as an Answer. Might be easy for someone who visits this later on.
其他答案提供了正确的原因,但我在第一眼理解它们时遇到了一些问题。我无法发表评论,因此将其作为答案。对于稍后访问它的人来说可能很容易。
If the path of the Zip file that is being created, is the same as the path that is given to the ZipFile.CreateFromDirectory, the ZipFile creates the desired zip file and starts adding the files from the directory to it. And will Eventually, try to add the desired zip file in the zip as well, as it is in the same directory. This is just not possible and not required, because the desired zipfile is being used by CreateFromDirectory method.
如果正在创建的 Zip 文件的路径与提供给 ZipFile.CreateFromDirectory 的路径相同,则 ZipFile 将创建所需的 zip 文件并开始将文件从目录添加到其中。并且将最终尝试在 zip 中添加所需的 zip 文件,因为它在同一目录中。这是不可能的,也不是必需的,因为 CreateFromDirectory 方法正在使用所需的 zipfile。
回答by StriplingWarrior
I came across this while because I was trying to zip the folder where my log files were being actively written by a running application. Kyle Johnson's answer could work, but it adds the overhead of copying the folder and the necessity of cleaning up the copy afterwards. Here's some code that will create the zip even if log files are being written to:
我遇到这个是因为我试图压缩正在运行的应用程序正在积极写入我的日志文件的文件夹。凯尔约翰逊的回答可能有效,但它增加了复制文件夹的开销以及事后清理副本的必要性。即使正在写入日志文件,以下代码也会创建 zip:
void SafelyCreateZipFromDirectory(string sourceDirectoryName, string zipFilePath)
{
using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
foreach (var file in Directory.GetFiles(sourceDirectoryName))
{
var entryName = Path.GetFileName(file);
var entry = archive.CreateEntry(entryName);
entry.LastWriteTime = File.GetLastWriteTime(file);
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var stream = entry.Open())
{
fs.CopyTo(stream, 81920);
}
}
}
}
回答by Eternal21
If you're getting this error because NLog is locking your log files, you can use the following workaround. Add 'keepFileOpen' attribute to your nlog tag inside NLog.config and set it to false:
如果您因为 NLog 锁定您的日志文件而收到此错误,您可以使用以下解决方法。将“keepFileOpen”属性添加到 NLog.config 中的 nlog 标记并将其设置为 false:
<nlog xmlns=.......
keepFileOpen="false"
....>
More details here.
更多细节在这里。
Note that this setting will have negative performance on NLog logging as indicated here.
注意该设置会对NLOG日志负的表现所指示的位置。