CreateZipFileFromDirectory - the process cannot access the file

1 Answer 546 Views
ZipLibrary
Richard
Top achievements
Rank 3
Iron
Iron
Iron
Richard asked on 29 Nov 2021, 11:06 AM

Good morning,

I have a process which successfully generates a number of PDFs in a folder, and then compresses that folder into an unencrypted zip file.

I have used the code from a previous question:

https://www.telerik.com/forums/zip-content-of-a-folder-into-an-encrypted-zip-file

So my code is as follows:

public static void CreateZipFileFromDirectory(string sourceDirectoryName, string destinationArchiveFileName)

        {

            char[] directorySeparatorChar = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };

 

            ZipArchiveMode archiveMode = System.IO.File.Exists(destinationArchiveFileName) ? ZipArchiveMode.Update : ZipArchiveMode.Create;

 

            if (!string.IsNullOrEmpty(sourceDirectoryName))

            {

                using FileStream archiveStream = System.IO.File.Open(destinationArchiveFileName, FileMode.OpenOrCreate);

                using ZipArchive archive = new ZipArchive(archiveStream, archiveMode, leaveOpen: false, entryNameEncoding: null);

                foreach (string fileName in Directory.GetFiles(sourceDirectoryName))

                {

                    using FileStream file = System.IO.File.OpenRead(fileName);

                    int length = fileName.Length - sourceDirectoryName.Length;

                    string entryName = fileName.Substring(sourceDirectoryName.Length, length);

                    entryName = entryName.TrimStart(directorySeparatorChar);

 

                    using ZipArchiveEntry entry = archive.CreateEntry(entryName);

                    using Stream entryStream = entry.Open();

                    file.CopyTo(entryStream);

                }

            }

        }

This creates the zip file and adds the first PDF successfully, but gives an error when trying to add the second PDF

"The process cannot access the file 'filename.zip' because it is being used by another process."

I have tried setting the leaveOpen parameter for the ZipArchive to true and to false but that gives me the same error.

Any ideas what I've missed?

Richard

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimitar
Telerik team
answered on 01 Dec 2021, 09:10 AM

Hi Richard,

I have created a sample project that uses your code and it successfully creates a zip file from a directory. Could please check the attached project and let me know what I need to add in order to reproduce the observed behavior.

Thank you in advance for your patience and cooperation.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Richard
Top achievements
Rank 3
Iron
Iron
Iron
commented on 01 Dec 2021, 11:24 AM

Hi Dimitar,

Thank you for your answer.

I managed to work out the source of my problem.  I was creating the zip file in the same folder as my PDFs, so the code was trying to add the zip file to itself!

I've managed to prevent this happening by excluding zip files from the file list:

foreach (string fileName in Directory.GetFiles(sourceDirectoryName).Where(name => !name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)))

Thanks again for looking at this - you confirming that the function worked (when supplied with different source and destination locations) helped.

Kind regards,

Richard

Dimitar
Telerik team
commented on 02 Dec 2021, 06:54 AM

Hi Richard,

I am glad that this works now.

Do not hesitate to contact us if you have other questions.
Tags
ZipLibrary
Asked by
Richard
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or