用SharpZipLib压缩和解压缩文件
第一步:添加引用文件:ICSharpCode.SharpZipLib.dll
第二步:导入名称空间:Imports ICSharpCode.SharpZipLib.Zip
第三步:a、压缩函数
'<summary>
' Create a zip archive.
' </summary>
' <param name="filename">The filename.</param>
' <param name="directory">The directory to zip.</param>
Public Shared Sub PackFiles(ByVal filename As String, ByVal directory As String)
Try
Dim fz As FastZip = New FastZip()
fz.CreateEmptyDirectories = True
fz.CreateZip(filename, directory, True, "")
fz = Nothing
Catch
Throw
End Try
End Sub
b、解压缩文件
'<summary>
'Unpacks the files.
'</summary>
'<param name="file">The file.</param>
'<returns>if succeed return true,otherwise false.</returns>
Public Shared Function UnpackFiles(ByVal file As String, ByVal dir As String) As Boolean
Try
'
If Not Directory.Exists(dir) Then
Directory.CreateDirectory(dir)
End If
Dim s As ZipInputStream = New ZipInputStream(IO.File.OpenRead(file))
Dim theEntry As ZipEntry
theEntry = s.GetNextEntry()
While Not IsNothing(theEntry)
Dim directoryName As String = Path.GetDirectoryName(theEntry.Name)
Dim fileName As String = Path.GetFileName(theEntry.Name)
If directoryName <> String.Empty Then
Directory.CreateDirectory(dir + directoryName)
End If


If fileName <> String.Empty Then
Dim StreamWriter As FileStream = IO.File.Create(dir + theEntry.Name)
Dim Size As Integer = 2048
Dim data(2048) As Byte
While (True)
Size = s.Read(data, 0, data.Length)
If Size > 0 Then
StreamWriter.Write(data, 0, Size)
Else
Exit While
End If
End While
StreamWriter.Close()
End If
'
theEntry = s.GetNextEntry()
End While
s.Close()
Return True
Catch ex As Exception
Throw
End Try
End Function
实例图:
点击下载


浙公网安备 33010602011771号