bickes0724
asked on
Trying to zip a folder in C# using System.IO.Packaging
I can get single files to zip into a specified folder just fine, but I want to benchmark a couple different options (using DotNetZip, System.IO.Packaging, and System.IO.Compression.GZip Stream). It would make it a lot easier to be able to add a folder.
My program keeps failing at this line:
using (Package zip = System.IO.Packaging.Packag e.Open(zip Filename, FileMode.OpenOrCreate))
With this Error:
Access to the path '**Folder Path**' is denied.
Part of my code is below:
My program keeps failing at this line:
using (Package zip = System.IO.Packaging.Packag
With this Error:
Access to the path '**Folder Path**' is denied.
Part of my code is below:
string[] filePaths = Directory.GetFiles(fileToAdd, "*", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
// FAILS ON THE NEXT LINE
// Error: "Access to the path '** Folder Path**' is denied."
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.ReadWrite))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
dest.Close();
}
fileStream.Close();
}
}
}
private static void CopyStream(FileStream inputStream, Stream outputStream)
{
long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
long bytesWritten = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesWritten += bufferSize;
}
}
I don't understand your zip process. You have a loop for each file, and you open and close the .zip file. Also, what file path are you using with 'zipFileName', and should you have access within the security context for the running application?
ASKER
Here is more code and the paths you asked for. I am not sure why I am closing the connections in the previous code. Honestly, I haven't been able to debug that far yet because of the error. Below is code that actually works as expected.
private void Button_Click(object sender, RoutedEventArgs e)
{
// AddFileToZip(@"C:\Users\bi ckes\Deskt op\NewZipF older.zip" , @"C:\Users\bickes\Desktop\ TestZip", true);
AddFileToZip(@"C:\Users\bi ckes\Deskt op\NewZipF older.zip" , @"C:\Users\bickes\Desktop\ FM.Portal. SetFacilit ies\FM.Por tal.SetFac ilities.sl n", false);
AddFileToZip(@"C:\Users\bi ckes\Deskt op\NewZipF older.zip" , @"C:\Users\bickes\Desktop\ FM.Portal. SetFacilit ies\FM.Por tal.SetFac ilities.cs proj", false);
}
private const long BUFFER_SIZE = 4096;
private static void AddFileToZip(string zipFilename, string fileToAdd, bool IsFolder)
{
if (IsFolder == false)
{
using (Package zip = System.IO.Packaging.Packag e.Open(zip Filename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd );
Uri uri = PackUriHelper.CreatePartUr i(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
dest.Close();
}
fileStream.Close();
}
}
return;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// AddFileToZip(@"C:\Users\bi
AddFileToZip(@"C:\Users\bi
AddFileToZip(@"C:\Users\bi
}
private const long BUFFER_SIZE = 4096;
private static void AddFileToZip(string zipFilename, string fileToAdd, bool IsFolder)
{
if (IsFolder == false)
{
using (Package zip = System.IO.Packaging.Packag
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd
Uri uri = PackUriHelper.CreatePartUr
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
dest.Close();
}
fileStream.Close();
}
}
return;
}
1) What kind of application is this? Windows Forms? Windows Service?
2) Where does it run from?
3) What is the operating system (it looks like Vista or Windows 7)?
4) For Vista or Windows 7, do you have UAC turned on?
2) Where does it run from?
3) What is the operating system (it looks like Vista or Windows 7)?
4) For Vista or Windows 7, do you have UAC turned on?
ASKER
I went a different route. Thank you for all your help. I wasn't very clear when I was explaining myself yesterday. I have posted my working code below to help anyone else...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.