【问题标题】:Create zip file from all files in folder从文件夹中的所有文件创建 zip 文件
【发布时间】:2016-09-27 19:00:53
【问题描述】:

我正在尝试从文件夹中的所有文件创建一个 zip 文件,但在网上找不到任何相关的 sn-p。我正在尝试做这样的事情:

DirectoryInfo dir = new DirectoryInfo("somedir path");
ZipFile zip = new ZipFile();
zip.AddFiles(dir.getfiles());
zip.SaveTo("some other path");

非常感谢任何帮助。

编辑:我只想压缩文件夹中的文件,而不是子文件夹。

【问题讨论】:

    标签: c# zip zipfile


    【解决方案1】:

    在您的项目中引用 System.IO.Compression 和 System.IO.Compression.FileSystem

    using System.IO.Compression;
    
    string startPath = @"c:\example\start";//folder to add
    string zipPath = @"c:\example\result.zip";//URL for your ZIP file
    ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
    string extractPath = @"c:\example\extract";//path to extract
    ZipFile.ExtractToDirectory(zipPath, extractPath);
    

    要仅使用文件,请使用:

    //Creates a new, blank zip file to work with - the file will be
    //finalized when the using statement completes
    using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
    {
        foreach (string file in Directory.GetFiles(myPath))
        {
            newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file));
        }              
    }
    

    【讨论】:

    • 感谢您的回复。此示例将所有文件和文件夹压缩到一个文件夹中。我只想压缩文件夹中的文件。
    • 是的,当然 - 当我回答这个问题并且在 OP 指定他们不希望包含子文件夹的一小时之前给我投反对票。咳咳……鉴于它是在编辑之前做出的,这是一个糟糕的答案吗?
    • 非常感谢您花时间和精力来帮助我。样本和标题都清楚地表明我只想压缩文件。我只是想我应该更清楚地说明它,这就是我添加编辑的原因。
    • 哦,哈哈,我没有意识到你是反对我的人,哈哈。不用担心。
    • 这是上面的 MSDN。 Zipmode.Create 应该创建一个新文件。我听说如果您尝试在要压缩的同一文件夹中创建 zip 文件,会出现一些问题 - 但本质上,ZipArchiveMode.Create 应该将文件放在您选择的目录中。无论如何,请查看msdn.microsoft.com/en-us/library/…,完成后请记住选择答案。谢谢!
    【解决方案2】:

    在您的项目中引用System.IO.CompressionSystem.IO.Compression.FileSystem,您的代码可能类似于:

    string startPath = @"some path";
    string zipPath = @"some other path";
    var files = Directory.GetFiles(startPath);
    
    using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open))
    {
        using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
        {
            foreach (var file in files)
            {
                archive.CreateEntryFromFile(file, file);
            }
        }
    }
    

    在某些文件夹中,虽然您可能会遇到权限问题。

    要使您的 zipfile 在 UNIX 系统上可移植,您应该注意:

    例如,可以使用 mod "644":

    var entry = newFile.CreateEntryFromFile(file, file);
    entry.ExternalAttributes |= (Convert.ToInt32("644", 8) << 16);
    

    【讨论】:

    【解决方案3】:

    这不需要循环。对于 VS2019 + .NET FW 4.7+ 做了这个...

    1. 在管理 Nuget 包浏览中找到 ZipFile,或使用

    https://www.nuget.org/packages/40-System.IO.Compression.FileSystem/

    1. 然后使用:

      使用 System.IO.Compression;

    例如,下面的代码片段将打包和解包一个目录(使用 false 以避免打包子目录)

        string zippedPath = "c:\\mydir";                   // folder to add
        string zipFileName = "c:\\temp\\therecipes.zip";   // zipfile to create
        string unzipPath = "c:\\unpackedmydir";            // URL for ZIP file unpack
        ZipFile.CreateFromDirectory(zippedPath, zipFileName, CompressionLevel.Fastest, true);
        ZipFile.ExtractToDirectory(zipFileName, unzipPath);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2013-10-25
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多