0 votes
by (270 points)
edited

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
by (73.6k points)
edited
 
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:/data.zip"))
{
    // initialize FileSet
    FileSet set = new FileSet("C:/data");
    set.Include("*.txt");

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

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

by (270 points)
edited

Hi,

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!

...