'How to enable the "Compress Shared Files" options in c# while zip the files?

I am trying to zip all the files in one directory in C#. However, one file is used by another process and causing an exception like, "(that) file is in use by another process."

Here is the code:

var files = Directory.GetFiles("D:\\Doc").ToList();
              
using (var memoryStream = new MemoryStream())
{
    using (var archive = new System.IO.Compression.ZipArchive(memoryStream, ZipArchiveMode.Create))
    {
        foreach (var file in files)
        {
            archive.CreateEntryFromFile(file, System.IO.Path.GetFileName(file), CompressionLevel.Fastest);`

The thing is, I can manually zip all the files (including the file used by another process) in the 7Zip GUI iff I enable the "Compress Shared Files" option.

How can I set the same "Compress Shared Files" option from C# ?



Solution 1:[1]

We can zip the files using DotNetZip third party library even if that files used by another process .

using (var zip = new ZipFile())
            {
                zip.AddFiles(Directory.GetFiles(LogFilePath)), "Logs");

                zip.Save(memoryStream);
            }

DotnetZip

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1