Add a file into ZipArchive if it used by another process

0 votes
asked Jan 31, 2013 by passm (270 points)
edited Feb 1, 2013

Hello, We are going to purchase Total Pack componentes, but we have an issue of zip archiving.

Is there any way to add a FileSet into ZipArchive if any file of the FileSet is used by another process? Now we get an exception: Cannot add file (...) the process cannot access the file...

Thank you in advance.

Applies to: Rebex ZIP

1 Answer

0 votes
answered Feb 1, 2013 by Lukas Matyska (61,230 points)
edited Feb 1, 2013
Best answer

Hello, in case the file is already opened by another process in write mode (FileAccess.Write) you have to open that file from another process with FileShare.Write.

When a file is going to be added into a ZipArchive it is opened with FileShare.Read. Unfortunately there is no option to tell the ZipArchive object to open files with FileShare.ReadWrite. However you can do it simply using this workaround:

// initialize ZipArchive
using (ZipArchive zip = new ZipArchive("C:/"))
    // initialize FileSet
    FileSet set = new FileSet("C:/data");

    // get all local items matching the FileSet
    LocalItemCollection items = set.GetLocalItems();
    foreach (LocalItem item in items)
        // skip items other than files
        if (!item.IsFile)

        // open each file with FileShare.ReadWrite
        using (Stream file = File.Open(item.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            // add the stream into ZipArchive
            zip.AddFile(file, item.Path);

Is this workaround sufficient for you? Actually, this workaround eliminates the bonus of ProgressChanged event. If you would like to use ProgressChanged event to display total progress, please let us know and we will add an option into ZipArchive to be able to specify FileShare.

commented Feb 1, 2013 by passm (270 points)
edited Feb 1, 2013


It seems this workaround will work for us. Currently we do not need a total progress by this event. Anyway we can invoke a custom event in foreach code. Thank you for the quick answer!