是否可以在 C# 中将文件夹压缩在一起?

Posted

技术标签:

【中文标题】是否可以在 C# 中将文件夹压缩在一起?【英文标题】:Is it possible to zip folders together in c#? 【发布时间】:2017-06-12 15:08:07 【问题描述】:

例如,如果我有一个名为 2016 的文件夹,并且该文件夹中有 Jan-Dec 文件夹和其他文件,有没有办法将这些文件夹一起压缩到 2016.zip 中?我尝试过的所有内容都将文件夹中的所有文件压缩在一起,但我只想将文件夹压缩在一起,而不是文件夹中的其他文件。

基本上,它是 2016.zip,其中 2016.zip 中包含 Jan-Dec 文件夹,每个文件夹中都有各自的文件,而不是 zip 中的空文件夹。

以下代码抓取以 00 开头的 FILES,但不抓取文件夹。

// Where the files are located
string strStartPath = txtTargetFolder.Text;

// Where the zip file will be placed
string strZipPath = @"C:\Users\smelmo\Desktop\testFinish\" + strFileNameRoot + "_" + txtDateRange1.Text.Replace(@"/", "_") + "_" + txtDateRange2.Text.Replace(@"/", "_") + ".zip";

if(File.Exists(strZipPath))

    File.Delete(strZipPath);


using (ZipArchive archive = ZipFile.Open(strZipPath, ZipArchiveMode.Create))

    foreach (FileInfo file in new DirectoryInfo(strStartPath).GetFiles())
    
                if (file.Name.StartsWith("00"))
                
                    var entry = archive.CreateEntryFromFile(Path.Combine(file.Directory.ToString(), file.Name), file.Name);
                
    

我也尝试过使用typeof(),但它没有抓取文件夹或任何东西。

using (ZipArchive archive = ZipFile.Open(strZipPath, ZipArchiveMode.Create))
    
        foreach (FileInfo file in new DirectoryInfo(strStartPath).GetFiles())
        
                Type t = file.GetType();
                if (t.Equals(typeof(Directory)))
                
                    var entry = archive.CreateEntryFromFile(Path.Combine(file.Directory.ToString(), file.Name), file.Name);
                            
    

编辑:为清楚起见添加了细节

【问题讨论】:

澄清一下,你想要一个空文件夹的压缩包,对吧? @CodyG.No,Jan-Dec 文件夹中会有文件。我想将 Jan-Dec 文件夹压缩在一起。所以打开 2016.zip 会有 Jan-Dec 文件夹,然后这些文件夹有各自的文件。 你为什么不试试DirectoryInfo(strStartPath).GetDirectories.GetFiles() 不会返回目录... 我会在驱动器上创建文件结构,然后使用: ZipFile.CreateFromDirectory(strStartPath, strZipPath);其中将包括您的文件夹。 【参考方案1】:

具有以下文件夹结构:

此代码(已更新):

using System.IO;
using System.IO.Compression;

class Program

    static void Main(string[] args)
    
        string sourceFolder = @"c:\temp";
        string zipFilePath = Path.Combine(sourceFolder, "test.zip");

        // TODO: Check if the archive exists maybe?

        using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
        
            foreach (var directoryName in Directory.GetDirectories(sourceFolder))
            
                foreach (var filePath in Directory.GetFiles(directoryName))
                
                    var dirInfo = new DirectoryInfo(directoryName);
                    var fileName = Path.GetFileName(filePath);

                    var entry = archive.CreateEntry($"dirInfo.Name\\fileName");
                
            
        
    

生成这个 zip:

【讨论】:

但如果我希望 zip 成为文件夹而不是 .txt 文件,我是否只需取出最后一个 foreach 并为每个 directoryName 创建条目? 我不明白你的意思。您只需要 1 月至 12 月的空文件夹的 zip 吗? 我也尝试使用确切的代码(修改sourceFolder)并且zip是空的。 不,我需要 zip 来包含 Jan-Dec 文件夹,每个文件夹中都有各自的文件。 好的,我更新了代码,也测试了。它现在生成一个带有相对路径的 zip。我想这就是你要找的。​​span>

以上是关于是否可以在 C# 中将文件夹压缩在一起?的主要内容,如果未能解决你的问题,请参考以下文章

如何以最大输出大小压缩多个文件,同时将文件对保持在一起

如何在 C# 中将 SVG 文件转换为 EMF 文件

我可以在 Scala 中将两个以上的列表压缩在一起吗?

如何在 C# 中将文件导出到 excel 2007+

如何在 C# 中将 DLLImport 与结构一起用作参数?

使用 System.IO.Packaging 在 c# 中解压缩 zip 文件