Che*_*eso 16
DotNetZip是一个易于使用的免费开源库,用于处理VB.NET和其他.NET语言中的ZIP文件.
一些示例VB.NET代码,用于创建zip文件,一次添加一个文件:
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
Run Code Online (Sandbox Code Playgroud)
或者,在组中添加文件:
Dim ZipToCreate As String = "ex1.zip"
Dim DirectoryToZip As String = "c:\temp"
Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
Using zip As ZipFile = New ZipFile
zip.AddFiles(filenames, "temp")
zip.Save(ZipToCreate)
End Using
Run Code Online (Sandbox Code Playgroud)
或者,压缩整个目录或文件夹的代码:
Using zip As ZipFile = New ZipFile
zip.AddDirectory(directory)
zip.Save(targetZip)
End Using
Run Code Online (Sandbox Code Playgroud)
提取zip文件的代码:
Dim ZipFileToExtract As String = "c:\foo.zip"
Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
Dim e As ZipEntry
For Each e In zip
' can conditionally extract here, '
' based on name, size, date, whatever.'
e.Extract
Next
End Using
Run Code Online (Sandbox Code Playgroud)
使用进度条提取:
Imports Ionic.Zip
Module SimpleUnzip
Public Sub Unzip(ByVal ZipToUnpack As String, ByVal ExtractDirectory As String)
Try
Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
Form1.ProgressBar1.Maximum = zip.Entries.Count
Dim entry As ZipEntry
For Each entry In zip
Form1.Label1.Text = entry.FileName
entry.Extract(ExtractDirectory, ExtractExistingFileAction.OverwriteSilently)
Form1.ProgressBar1.Value = Form1.ProgressBar1.Value + 1
' sleep because it's too fast otherwise.
System.Threading.Thread.Sleep(50)
Next
Form1.ProgressBar1.Value = 0
Form1.Label1.Text = "Done"
End Using
Catch ex1 As Exception
Form1.Label1.Text = ("Exception: " & ex1.ToString())
End Try
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
DotNetZip具有读取,保存或提取的进度事件,因此您可以在ASP.NET或Windows窗体中为进度条供电.它使用受密码保护的zip文件,Unicode,ZIP64和自解压缩档案.它生成的zip文件与所有其他zip工具兼容 - WinZip,WinRAR,Windows资源管理器,Pkunzip等.这里有一个很好的帮助文件(在线版本),有很多代码示例.也有样品可供下载.