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
.