I realize there are several variations of this question, but in reviewing them I don't see my exact use case and my result has a wrinkle.
What I'm doing is FTPing some BZ2 zipped files to my hard drive in a folder. Then I unzip each one for review. Finally, I want to email the files to someone but I want to put them into a Zip Archive first to make it easier. Below is the code.
private void buttonCreateZip_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(@"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\zip\\");
string startPath = @"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString();
string zipPath = @"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\zip\\" + comboBoxDirectory.SelectedItem.ToString() + ".zip";
File.SetAttributes(@"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\zip\\", FileAttributes.Normal);
File.SetAttributes(@"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\", FileAttributes.Normal);
ZipFile.CreateFromDirectory(startPath, zipPath);
}
I'm not combining the files individually but using an API that takes all the files in a target directory and zips them into an archive.
The wrinkle is that even though I get the exception
Process cannot access the file "C:..." because it is being used by another process"
it creates the ZIP archive anyway in the zip subdirectory I've created for this purpose. It almost looks like it's inside the library function CreateFromDirectory, but this is part of a standard library which I accessed by referencing:
System.IO.Compression.FileSystem.
The destinaton Directory of the zipped file:
@"C:\temp\logfiles\[Some Name]\Zip"
is included in the path of the source Directory, the base Directory of the compression operation:
@"C:\temp\logfiles\[Some Name].
ZipFile.CreateFromDirectory includes the SubDirectories tree structure of the base Directory and it's content when creating the compressed file, hence it tries to also compress the destination File that it's creating. Of course it can't access it, because it's (guess what) in use.
If you move the destination Directory outside the base Path, it won't raise any exception.
You could use the User Temp directory as a temporary destination for the zipped file and then move it to the destination Directory when it's complete.
The User Temp Directory is returned by Environment.GetEnvironmentVariable():
Environment.GetEnvironmentVariable("Temp", EnvironmentVariableTarget.User);
You also need to delete the Temp zip file and, in any case, verify that it doesn't already exist (a file with that name could be there for any reason and trying to overwrite it will cause an error).
An example of a possible method to create a ZipFile using the User Temp Directory:
string SourceFolder = @"C:\temp\logfiles\";
string DestinationFolder = @"C:\temp\logfiles\Zip";
string ZippedFileName = "ZippedFile.zip";
string UserTempFolder = Environment.GetEnvironmentVariable("Temp", EnvironmentVariableTarget.User);
string ZippedTempFile = Path.Combine(UserTempFolder, ZippedFileName);
if (File.Exists(ZippedTempFile)) { File.Delete(ZippedTempFile); }
ZipFile.CreateFromDirectory(SourceFolder, ZippedTempFile);
Directory.CreateDirectory(DestinationFolder);
File.Move(ZippedTempFile, Path.Combine(DestinationFolder, ZippedFileName));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With