我有一个类,我已经从C#转换为VB.NET(Framework 4.5)
我正在尝试使用新的压缩功能。我想我已经按照MSDN的所有说明进行了操作,但它仍然没有编译。
ZipFile,ZipArchive和ZipArchiveEntry都会出现“Type is not defined”
我在参考文献
中导入了名称空间“System”,“System.IO”和“System.IO.Compression”我还需要做些什么才能让它发挥作用?
Imports System
Imports System.IO
Imports System.IO.Compression
Imports System.Collections.Generic
Imports System.Linq
Public Class clsZip1
' Used to specify what our overwrite policy is for files we are extracting.
Public Enum Overwrite
Always = 0
IfNewer = 1
Never = -1
End Enum
' Used to identify what we will do if we are trying to create a zip file and it already exists.
Public Enum ArchiveAction
eMerge = 0
eReplace = 1
eError = 2
eIgnore = 3
End Enum
' Unzips the specified file to the given folder in a safe manner.
' This plans for missing paths and existing files and handles them gracefully.
Public Sub ExtractToDirectory(ByVal sourceArchiveFileName As String,
ByVal destinationDirectoryName As String,
Optional ByVal overwriteMethod As Overwrite = Overwrite.IfNewer)
'Opens the zip file up to be read
Using archive As ZipArchive = ZipFile.OpenRead(sourceArchiveFileName)
'Loops through each file in the zip file
For Each file As ZipArchiveEntry In archive.Entries
ImprovedExtractToFile(file, destinationDirectoryName, overwriteMethod)
Next
End Using
End Sub
' Safely extracts a single file from a zip file
Public Sub ExtractToFile(ByVal file As ZipArchiveEntry,
ByVal destinationPath As String,
Optional ByVal overwriteMethod As Overwrite = Overwrite.IfNewer)
'Gets the complete path for the destination file, including any
'relative paths that were in the zip file
Dim destinationFileName As String = Path.Combine(destinationPath, file.FullName)
'Gets just the new path, minus the file name so we can create the
'directory if it does not exist
Dim destinationFilePath As String = Path.GetDirectoryName(destinationFileName)
'Creates the directory (if it doesn't exist) for the new path
Directory.CreateDirectory(destinationFilePath)
'Determines what to do with the file based upon the
'method of overwriting chosen
Select Case overwriteMethod
Case Overwrite.Always
'Just put the file in and overwrite anything that is found
file.ExtractToFile(destinationFileName, True)
Case Overwrite.IfNewer
'Checks to see if the file exists, and if so, if it should
'be overwritten
If (Not file.Exists(destinationFileName) Or file.GetLastWriteTime(destinationFileName) < file.LastWriteTime) Then
'Either the file didn't exist or this file is newer, so
'we will extract it and overwrite any existing file
file.ExtractToFile(destinationFileName, True)
End If
Case Overwrite.Never
'Put the file in if it is new but ignores the
'file if it already exists
If (Not file.Exists(destinationFileName)) Then
file.ExtractToFile(destinationFileName)
End If
End Select
End Sub
' Allows you to add files to an archive, whether the archive already exists or not
Public Sub AddToArchive(ByVal archiveFullName As String,
ByVal files As List(Of String),
Optional ByVal action As ArchiveAction = ArchiveAction.eReplace,
Optional ByVal fileOverwrite As Overwrite = Overwrite.IfNewer,
Optional ByVal compression As CompressionLevel = CompressionLevel.Optimal)
'Identifies the mode we will be using - the default is Create
Dim mode As ZipArchiveMode = ZipArchiveMode.Create
'Determines if the zip file even exists
Dim archiveExists As Boolean = File.Exists(archiveFullName)
'Figures out what to do based upon our specified overwrite method
Select Case action
Case ArchiveAction.eMerge
'Sets the mode to update if the file exists, otherwise
'the default of Create is fine
If (archiveExists) Then mode = ZipArchiveMode.Update
Case ArchiveAction.eReplace
'Deletes the file if it exists. Either way, the default
'mode of Create is fine
If (archiveExists) Then File.Delete(archiveFullName)
Case ArchiveAction.eError
'Throws an error if the file exists
If (archiveExists) Then Throw New IOException(String.Format("The zip file {0} already exists.", archiveFullName))
Case ArchiveAction.eIgnore
'Closes the method silently and does nothing
If (archiveExists) Then Return
Case Else
End Select
'Opens the zip file in the mode we specified
Using zipFile As ZipArchive = zipFile.Open(archiveFullName, mode)
'This is a bit of a hack and should be refactored - I am
'doing a similar foreach loop for both modes, but for Create
'I am doing very little work while Update gets a lot of
'code. This also does not handle any other mode (of
'which there currently wouldn't be one since we don't
'use Read here).
If (mode = ZipArchiveMode.Create) Then
For Each file As String In files
'Adds the file to the archive
zipFile.CreateEntryFromFile(file, Path.GetFileName(file), compression)
Next
Else
For Each file As String In files
Dim fileInZip = (From f In zipFile.Entries
Where f.Name = Path.GetFileName(file)
Select f).FirstOrDefault()
Select Case fileOverwrite
Case Overwrite.Always
'Deletes the file if it is found
If (fileInZip IsNot Nothing) Then
fileInZip.Delete()
End If
'Adds the file to the archive
zipFile.CreateEntryFromFile(file, Path.GetFileName(file), compression)
Case Overwrite.IfNewer
'This is a bit trickier - we only delete the file if it is
'newer, but if it is newer or if the file isn't already in
'the zip file, we will write it to the zip file
If (fileInZip IsNot Nothing) Then
'Deletes the file only if it is older than our file.
'Note that the file will be ignored if the existing file
'in the archive is newer.
If (fileInZip.LastWriteTime < file.GetLastWriteTime(file)) Then
fileInZip.Delete()
'Adds the file to the archive
zipFile.CreateEntryFromFile(file, Path.GetFileName(file), compression)
Else
'The file wasn't already in the zip file so add it to the archive
zipFile.CreateEntryFromFile(file, Path.GetFileName(file), compression)
End If
End If
Case Overwrite.Never
'Don't do anything - this is a decision that you need to
'consider, however, since this will mean that no file will
'be writte. You could write a second copy to the zip with
'the same name (not sure that is wise, however).
Case Else
End Select
Next
End If
End Using
End Sub
End Class
答案 0 :(得分:0)
您需要添加对System.IO.Compression.dll的引用。我只有VS2010在工作,所以我无法对此进行测试,但我猜它只适用于Framework 4.5,因为它不在这台PC上。
打开&#39;我的项目&#39;,转到参考标签,点击添加...&#39;下拉并选择参考&#39;。 在“添加引用”对话框中,使用.NET标记,按“组件名称”对列表进行排序,然后查找“System.IO.Compression”。
如果不存在,请尝试在硬盘上搜索dll并使用相同的参考对话框手动添加。希望有这个特定图书馆经验的人可以澄清使用它的任何异常情况。