VB.Net 4.6 使用 Zipfile

VB.Net 4.6 using Zipfile

我已经在这个网站上搜索了一段时间,但似乎找不到解决方案。我正在尝试使用 ZipFile 从我的目录中压缩一些 PDF,但是每当我键入 Using zip As ZipFile = New ZipFile 时,我都会收到错误消息:

'Using' operand of type 'System.IO.Compression.ZipFile' must implement 'System.IDisposable'

'System.IO.Compression.ZipFile' has no constructors.

在搜索该站点后,我发现我必须添加一些我添加的参考:System.IO.CompressionSystem.IO.Compression.Filesystem 然后我确保我使用的是正确的 .Net 框架,并且我我在 VS 2013 中使用 .NET 4.6。我不知道该怎么做,我只想从一个文件夹中压缩几个文件,但我不能使用 ZipFile。非常感谢任何关于在哪里查看的信息。

我使用的代码是:

Dim ZipToCreate As String = "ex1.zip"
Dim DirectoryToZip As String = "c:\temp"
Using zip As ZipFile = New ZipFile
    Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
    Dim filename As String
    For Each filename In filenames
        zip.AddFile(filename)
    Next
    zip.Save(ZipToCreate)
End Using

来自这个例子 -> how to Zip files in vb.net 2005

错误清楚地表明您不能将 Using 与未实现 IDisposable 的 class 一起使用,并且您无法创建 [=13= 的实例] 因为它没有构造函数。如果您阅读 MSDN documentation,您会看到 ZipFile class 只有静态方法。他们要将 DirectoryToZip 中的所有文件压缩到一个名为 ZipToCreate 的 zip 文件中是:

ZipFile.CreateFromDirectory(DirectoryToZip, ZipToCreate)

您混淆了 2 个不同的 "ZipFile" API。

您从中复制的示例代码要求您引用 DotNetZip 库以使用 ZipFile class,它不是 .NET Framework 的一部分。

.NET Framework 本身也有一个 ZipFile class,但有一个不同的 API,这就是您的代码当前所指的那个。

选择您要使用的库并确保引用正确的库并阅读相应的文档以正确编码。