如何在 VB.NET 中压缩文件/目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30043835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:05:47  来源:igfitidea点击:

How to zip files/directories in VB.NET?

c#.netvb.netzip

提问by OfficeAddinDev

I would like to zip a folder containing files and subfolders in VB.NET. My solution targets .NET 4.0 Client Profile.

我想在 VB.NET 中压缩一个包含文件和子文件夹的文件夹。我的解决方案针对 .NET 4.0 Client Profile。

I see that there is a ZipFile class for .NET 4.5, and System.IO.Packing for .NET 4.0 (but not Client Profile). So, those won't help. I note that there is also a GZipStream class, but I never see .gz files floating around, so not sure if that's a good approach. I would prefer a basic .zip file that I know my users can work with.

我看到 .NET 4.5 有一个 ZipFile 类,.NET 4.0 有一个 System.IO.Packing 类(但不是客户端配置文件)。所以,那些都无济于事。我注意到还有一个 GZipStream 类,但我从来没有看到 .gz 文件四处浮动,所以不确定这是否是一个好方法。我更喜欢我知道我的用户可以使用的基本 .zip 文件。

There are a handful of third-party solutions, such as http://icsharpcode.github.io/SharpZipLib/, but I assume they are far more bloated than the 10-20 lines of code I am looking for. Maybe that's the right answer...I don't know.

有一些第三方解决方案,例如http://icharpcode.github.io/SharpZipLib/,但我认为它们比我正在寻找的 10-20 行代码要臃肿得多。也许这是正确的答案...我不知道。

Just hoping for a few lines of VB.NET code to zip some files in a solution targeting .NET 4.0 CP.

只希望几行 VB.NET 代码可以在针对 .NET 4.0 CP 的解决方案中压缩一些文件。

回答by Naitik Patel

Create ZIP from "source" folder.

从“源”文件夹创建 ZIP。

Imports System.IO.Compression    
ZipFile.CreateFromDirectory("source","destination.zip",CompressionLevel.Optimal,False)

Extract ZIP to "destination" folder.

将 ZIP 解压缩到“目标”文件夹。

ZipFile.ExtractToDirectory("destination.zip","destination")

回答by an odder guest

Unable to comment in the comments section due to lack of reputation (due to being new here).

由于缺乏声誉(由于是新来的),无法在评论部分发表评论。

On the two answers indicating that using System.IO.Compressioncan be used that were commented as not working in NET 4.0 CP, this should be noted, as the original answers did not include an often overlooked fact.

在表示可以使用 System.IO.Compression的两个答案中,它们被评论为在 NET 4.0 CP 中不起作用,应该注意这一点,因为原始答案不包括经常被忽视的事实。

A reference needs to be added to the project in order to activate "System.IO.Compression". This is not obvious to many new VS users, and unless the refrence is added, it indeed seems like "System.IO.Compression" does not work in NET 4.0.

需要在项目中添加引用以激活“System.IO.Compression”。这对许多新的 VS 用户来说并不明显,除非添加引用,否则“System.IO.Compression”确实在 NET 4.0 中不起作用。

In this case, the reference that needs to be added is System.IO.Compression.Filesystem(v4.0.0.0). One may also wish to add the reference: System.IO.Compression.

在这种情况下,需要添加的引用是System.IO.Compression.Filesystem(v4.0.0.0)。还可以添加参考:System.IO.Compression

The "Add Reference..." dialog is under the "Project" menu in VS.

“添加引用...”对话框位于 VS 的“项目”菜单下。

回答by Fnk

Same challenges as "an odder guest". That is can't commment due to low rep as new user and have to add a new answer. (Frequent reader though). Running on vb.Net 4.6 and got no errors on the suggestions above, but no output zip file was generated. Then after some googling added references System.IO.Compression.Filesystemand System.IO.Compressionand ended up with the following one-liner:

与“奇怪的客人”相同的挑战。由于作为新用户的代表较低,因此无法发表评论,并且必须添加新答案。(虽然经常阅读)。在 vb.Net 4.6 上运行,上面的建议没有错误,但没有生成输出 zip 文件。然后经过一些谷歌搜索添加了System.IO.Compression.FilesystemSystem.IO.Compression 的引用,并最终得到了以下单行:

System.IO.Compression.ZipFile.CreateFromDirectory(FilePath1, FilePath3_ZippedFile)

Where FilePath1 is the path to the source folder (c:\whateverfolder) and FilePath3_ZippedFile is the path to the resulting output zip-file(c:\anotherfolder\MyZipFile.zip).

其中 FilePath1 是源文件夹 (c:\whateverfolder) 的路径,FilePath3_ZippedFile 是生成的输出 zip 文件 (c:\anotherfolder\MyZipFile.zip) 的路径。

Sorry if you think my input is redundant, but I know for newcommers like my self we need the answers feed with tee spoons.

抱歉,如果您认为我的意见是多余的,但我知道对于像我这样的新人来说,我们需要用 T 形勺子提供答案。

Word of caution:If you try to create the zip-file in the source folder you might have issues. My code then only included one of the files from the source folder in the zip-file. (No error messages). Changing the target location to another folder solved this without other edits to the code.

注意事项:如果您尝试在源文件夹中创建 zip 文件,您可能会遇到问题。然后,我的代码仅包含 zip 文件中源文件夹中的文件之一。(没有错误信息)。将目标位置更改为另一个文件夹即可解决此问题,而无需对代码进行其他编辑。

回答by Fnk

Simply you can do it using Ionic.Zip.Dll

只需使用Ionic.Zip.Dll即可完成

Here is the function for this,accepting two variables source path and destination path

这是这个函数,接受两个变量源路径和目标路径

public static void ExecuteBackupAsZip(string SourcePath, string DestinationPath)
        {
            using (var zip = new Ionic.Zip.ZipFile())
            {
                try
                {
                    zip.AddDirectory(SourcePath);
                    zip.Save(DestinationPath);
                }
                catch { Console.WriteLine("Failed to execute backup"); }
            }
        }

回答by BinaryEvolved

The MSDN Website has a class that can help you out here called ZipFile

MSDN 网站有一个类可以帮助您在这里称为ZipFile

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

namespace ConsoleApplication {
  class Program {
    static void Main(string[] args) {
      string startPath = @"c:\example\start";
      string zipPath = @"c:\example\result.zip";
      string extractPath = @"c:\example\extract";
      'Creates Zip File to directory as specified (startPath). And puts it in a specifed folder (zipPath)
            ZipFile.CreateFromDirectory(startPath, zipPath);
              '
      Extracts Zip File to directory as specified(extractpath)
      ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
  }
}

Hope this helps!

希望这可以帮助!