如何在vb.net 2005中压缩文件(任何文件或文件夹)?
答案 0 :(得分: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
或者,在组中添加文件:
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
或,用于压缩整个目录或文件夹的代码:
Using zip As ZipFile = New ZipFile
zip.AddDirectory(directory)
zip.Save(targetZip)
End Using
提取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
使用进度条提取:
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
DotNetZip具有读取,保存或提取的进度事件,因此您可以为ASP.NET或Windows窗体中的进度条供电。它使用受密码保护的zip文件,Unicode,ZIP64和自解压缩档案。它生成的zip文件与所有其他zip工具兼容 - WinZip,WinRAR,Windows资源管理器,Pkunzip等。有一个很好的帮助文件(online version here),有大量的代码示例。还有samples available for download。
答案 1 :(得分:1)
答案 2 :(得分:0)
我不知道如何在VB.NET中编程。但是,搜索显示了一个有趣的链接:Zip Compression VB.NET Examples。我希望它对你有用。
答案 3 :(得分:0)
您可以使用ICSharCode's SharpZipLib库。
答案 4 :(得分:0)
您可以使用我们的Rebex ZIP组件。
以下是您要求的一些操作示例:
在一行代码中简单压缩文件:
' add content of the local directory C:\Data\ '
' to the directory \Data-2010 (within the ZIP archive) '
' (ZIP archive C:\archive.zip doesn't have to exist)
ZipArchive.Add("C:\archive.zip", "C:\Data\*", "\Data-2010")
在一行代码中简单解压缩:
' extract all *.TXT files from the directory \Data-2010 (within the ZIP file) '
' to the existing local directory C:\Data '
ZipArchive.Extract("C:\archive.zip", "\Data-2010\*.html", "C:\Data")
可以找到更多样本here。
答案 5 :(得分:0)
封壳,wa-la完成两行
Dim zipcmd as String = "zip -r C:\directory\of\my\folder C:\directory\of\my\zip"
Shell("cmd.exe /c" + zipcmd1, AppWinStyle.Hide, True)