简体   繁体   English

如何在 c# 和 zip 文件中启用“压缩共享文件”选项?

[英]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."我正在尝试 zip C# 中一个目录中的所有文件。但是,一个文件被另一个进程使用并导致异常,如“(那个)文件正在被另一个进程使用。”

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.问题是,如果启用“压缩共享文件”选项,我可以在 7Zip GUI 中手动 zip 所有文件(包括另一个进程使用的文件)。

How can I set the same "Compress Shared Files" option from C#?如何设置与 C# 相同的“压缩共享文件”选项?

We can zip the files using DotNetZip third party library even if that files used by another process.我们可以使用 DotNetZip 第三方库 zip 文件,即使该文件被另一个进程使用。

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

                zip.Save(memoryStream);
            }

Do.netZip Do.netZip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM