Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# ZipFile.CreateFromDirectory - the process cannot access the file "path_to_the_zip_file_created.zip" because it is being used by another process

Tags:

c#

.net

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.

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.

I have checked the antivirus logs and it does not block the application, nor does it lock the zip file that is created.

like image 499
DJD Avatar asked Oct 16 '13 04:10

DJD


People also ask

What is C in coding language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.


2 Answers

//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.

like image 74
Emanuele Greco Avatar answered Sep 28 '22 00:09

Emanuele Greco


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:

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);             }         }     } } 
like image 41
StriplingWarrior Avatar answered Sep 28 '22 02:09

StriplingWarrior