简体   繁体   English

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

[英]How to zip files/directories in VB.NET?

I would like to zip a folder containing files and subfolders in VB.NET. 我想在VB.NET中压缩包含文件和子文件夹的文件夹。 My solution targets .NET 4.0 Client Profile. 我的解决方案针对.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). 我看到.NET 4.5有一个ZipFile类,.NET 4.0有System.IO.Packing(但不是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. 我注意到还有一个GZipStream类,但我从来没有看到.gz文件浮动,所以不确定这是否是一个好方法。 I would prefer a basic .zip file that I know my users can work with. 我更喜欢我知道用户可以使用的基本.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. 有一些第三方解决方案,例如http://icsharpcode.github.io/SharpZipLib/ ,但我认为它们比我正在寻找的10-20行代码要臃肿得多。 Maybe that's the right answer...I don't know. 也许这是正确答案......我不知道。

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的解决方案中压缩一些文件。

Create ZIP from "source" folder. 从“source”文件夹创建ZIP。

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

Extract ZIP to "destination" folder. 将ZIP解压缩到“目标”文件夹。

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

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"); }
            }
        }

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! 希望这可以帮助!

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.Compression can 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". 需要将引用添加到项目中以激活“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. 这对于许多新的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). 在这种情况下,需要添加的引用是System.IO.Compression.Filesystem (v4.0.0.0)。 One may also wish to add the reference: System.IO.Compression . 人们可能还希望添加引用: System.IO.Compression

The "Add Reference..." dialog is under the "Project" menu in VS. “添加引用...”对话框位于VS的“项目”菜单下。

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. 在vb.Net 4.6上运行并且没有上述建议的错误,但没有生成输出zip文件。 Then after some googling added references System.IO.Compression.Filesystem and System.IO.Compression and ended up with the following one-liner: 然后在一些谷歌搜索后添加了参考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. 注意事项:如果您尝试在源文件夹中创建zip文件,则可能会出现问题。 My code then only included one of the files from the source folder in the zip-file. 然后我的代码只包含zip文件中源文件夹中的一个文件。 (No error messages). (没有错误消息)。 Changing the target location to another folder solved this without other edits to the code. 将目标位置更改为另一个文件夹解决了此问题,而无需对代码进行其他编辑。

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

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