1.SharpZipLib第三方插件(开源免费)
1.1官方代码和文档URL:GitHub - icsharpcode/SharpZipLib: #ziplib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform.
1.2关于版本:v0.86.0.518版本支持.netframework4.0,
v1.0.0-rc1以上只支持.netframework4.5以上。
1.3各个版本的信息和dll:Releases · icsharpcode/SharpZipLib · GitHub
1.4.压缩的代码实例:
Private Sub fileToZip(ByVal sourceDir As String, ByVal targetName As String)
'sourceDir="D:\test" 被压缩的文件夹路径。
If sourceDir.Length = 0 Then
MessageBox.Show("Please specify a directory")
Return
Else
If Not Directory.Exists(sourceDir) Then
MessageBox.Show(sourceDir, "Directory not found")
Return
End If
End If
'targetName="D:\test\A.zip"指定压缩后的路径和文件夹。
If targetName.Length = 0 Then
MessageBox.Show("No name specified", "Zip file name error")
Return
End If
Dim astrFileNames() As String = Directory.GetFiles(sourceDir)
Dim strmZipOutputStream As ZipOutputStream
strmZipOutputStream = New ZipOutputStream(File.Create(targetName))
Try
REM Compression Level: 0-9
REM 0: no(Compression)
REM 9: maximum compression
strmZipOutputStream.SetLevel(9)
Dim strFile As String
Dim abyBuffer(4096) As Byte
For Each strFile In astrFileNames
Dim strmFile As FileStream = File.OpenRead(strFile)
Try
Dim objZipEntry As ZipEntry = New ZipEntry(Path.GetFileName(strFile))
objZipEntry.DateTime = DateTime.Now
objZipEntry.Size = strmFile.Length
strmZipOutputStream.PutNextEntry(objZipEntry)
StreamUtils.Copy(strmFile, strmZipOutputStream, abyBuffer)
Finally
strmFile.Close()
End Try
Next
strmZipOutputStream.Finish()
Finally
strmZipOutputStream.Close()
End Try
End Sub
1.5解压的代码实例:
Private Sub zipToFile(ByVal folderName As String, ByVal unZipDir As String)
'folderName = "D:\test\A.zip" 压缩包的文件夹路径。
If folderName.Length = 0 Then
MessageBox.Show("Please specify a directory")
Return
End If
'unZipDir="D:\test"解压后的路径。
If Not unZipDir.EndsWith("\\") Then
unZipDir += "\\"
End If
If Not Directory.Exists(unZipDir) Then
Directory.CreateDirectory(unZipDir)
End If
If unZipDir.Length = 0 Then
MessageBox.Show("No name specified", "Zip file name error")
Return
End If
Dim strmZipInputStream As ZipInputStream = New ZipInputStream(File.OpenRead(folderName))
Try
Dim objEntry As ZipEntry = strmZipInputStream.GetNextEntry()
While IsNothing(objEntry) = False
Dim directoryName As String = Path.GetDirectoryName(objEntry.Name)
Dim fileName As String = Path.GetFileName(objEntry.Name)
If Not directoryName.EndsWith("\\") Then
directoryName += "\\"
End If
Dim zipDir As String = unZipDir + directoryName
If directoryName.Length > 0 AndAlso Not Directory.Exists(zipDir) Then
Directory.CreateDirectory(zipDir)
End If
If objEntry.IsFile AndAlso fileName.Length > 0 Then
Using streamWriter As FileStream = File.Create(unZipDir + objEntry.Name)
Dim nSize As Integer = 2048
Dim abyData(2048) As Byte
While True
nSize = strmZipInputStream.Read(abyData, 0, abyData.Length)
If nSize > 0 Then
streamWriter.Write(abyData, 0, nSize)
Else
Exit While
End If
End While
End Using
End If
objEntry = strmZipInputStream.GetNextEntry()
End While
Finally
strmZipInputStream.Close()
End Try
End Sub
2.System.IO.Packaging压缩和解压
2.1System.IO.Packaging的优缺点:
优点:从.netframework中可以直接应用,不用添加第三方DLL。属于WindowsBase.dll。
缺点:使用Packaging压缩文件会在zip文件自动生成[Content_Type].xml,用来描述zip文件解压支持的文件格式。
2.2压缩代码实例(解压代码可以百度):
Private Sub fileToZip(ByVal folderName As String, ByVal compressedFileName As String) ', ByVal sDirectory As String)
'folderName="D:\test" 被压缩的文件夹路径。
If folderName.Substring(folderName.Length - 1) = "\" Then
folderName = folderName.Remove(folderName.Length - 1)
End If
If Directory.Exists(folderName) = False Then
Return
End If
Try
'compressedFileName="D:\test\A.zip"指定压缩后的路径和文件夹。
Using package As Packaging.Package = System.IO.Packaging.Package.Open(compressedFileName, FileMode.Create)
For Each fileName As String In Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories)
Dim pathInPackage As String = ""
pathInPackage = Path.GetDirectoryName(fileName).Replace(folderName, String.Empty) & "/" & Path.GetFileName(fileName)
Dim partUriDocument As Uri = Packaging.PackUriHelper.CreatePartUri(New Uri(pathInPackage, UriKind.Relative))
Dim packagePartDocument As Packaging.PackagePart = package.CreatePart(partUriDocument, "", Packaging.CompressionOption.Maximum)
Using fileStream As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)
fileStream.CopyTo(packagePartDocument.GetStream())
End Using
Next
End Using
Catch ex As Exception
End Try
End Sub
2.3 关于[Content_Type].xml文件,
如果zip文件不包含[Content_Type].xml文件,或者[Content_Type].xml文件不包含所对应扩展名的描述(手动添加的[Content_Type].xml也是可以),将无法解压zip包文件。
附件: 支持.net framework 4.0版的SharpZipLib.dll
如果资源学习了,可以点赞支持!