首先,在项目中添加.NET引用vjslib,即引用VISUAL J#的类库,然后在项目开始标明:
Imports System
Imports System.Collections[推荐]VB.NET调用Java.io.zip进行压缩,懒人的好办法
Imports java.util
Imports java.util.zip
示例代码如下:
Public zipListView As New System.Windows.Forms.ListView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Str() As String = {"C:\boot.ini", "C:\MmisErrs.Log"}
Dim Zstr As String = "C:\test.zip"
'1-Create a Zip file
'Call CreateZip(Zstr, Str)
'2-Display files in a Zip file
'Call DisplayEntries(Zstr)
'3-Create an empty Zip file
'Call CreateEmptyZip(Zstr)
'4-Add files to a Zip file
'Call UpdateZipFile(Zstr, Str)
'5-Extract a Zip file
'Call ExtractZip(Zstr)
End Sub
Sub CopyStream(ByVal Ifrom As java.io.InputStream, ByVal Ito As java.io.OutputStream)
Dim buffer(8191) As SByte
Dim got As New Integer
got = Ifrom.read(buffer, 0, buffer.Length)
While got > 0
Ito.write(buffer, 0, got)
got = Ifrom.read(buffer, 0, buffer.Length)
End While
End Sub
Sub CopyEntries(ByVal Zfrom As ZipFile, ByVal Zto As ZipOutputStream)
Dim Enum1 As Enumeration = Zfrom.entries
Dim Ze As ZipEntry
While Enum1.hasMoreElements
Ze = Enum1.nextElement
Dim s As java.io.InputStream = Zfrom.getInputStream(Ze)
Try
Zto.putNextEntry(Ze)
Try
CopyStream(s, Zto)
Finally
Zto.closeEntry()
End Try
Finally
s.close()
End Try
End While
End Sub
Sub DisplayEntries(ByVal Zfile As String)
'
'ZipListView Initialization
'
zipListView.Parent = Me
zipListView.Dock = System.Windows.Forms.DockStyle.Fill
zipListView.Name = "zipListView"
zipListView.View = System.Windows.Forms.View.Details
Dim ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Dim ColumnHeader2 = New System.Windows.Forms.ColumnHeader
Dim ColumnHeader3 = New System.Windows.Forms.ColumnHeader
Dim ColumnHeader4 = New System.Windows.Forms.ColumnHeader
Dim ColumnHeader5 = New System.Windows.Forms.ColumnHeader
Dim ColumnHeader6 = New System.Windows.Forms.ColumnHeader
zipListView.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {ColumnHeader1, ColumnHeader2, ColumnHeader3, ColumnHeader4, ColumnHeader5, ColumnHeader6})
'
'Add items to ziplistview
'
Try
Dim Zipe As New ZipFile(Zfile)
Dim Enum1 As Enumeration
Dim Ze As ZipEntry
Enum1 = Zipe.entries
While Enum1.hasMoreElements()
Ze = Enum1.nextElement[推荐]VB.NET调用Java.io.zip进行压缩,懒人的好办法
Dim LstIt As New ListViewItem
LstIt.Text = Ze.getName
LstIt.SubItems.Add(Ze.getSize.ToString)
LstIt.SubItems.Add(Ze.getCompressedSize.ToString)
LstIt.SubItems.Add(Ze.isDirectory)
LstIt.SubItems.Add(Ze.getTime)
LstIt.SubItems.Add(Ze.getMethod.ToString)
zipListView.Items.Add(LstIt)
End While
Zipe.close()
Catch ex As Exception
MsgBox("Error occurs while trying to display Zip file '" & Zfile & "'!")
End Try
End Sub
Sub ExtractZip(ByVal Zfile As String)
Try
Dim Zipe As New ZipFile(Zfile)
Dim Enum1 As Enumeration
Dim Ze As ZipEntry
Enum1 = Zipe.entries
While Enum1.hasMoreElements()
Ze = Enum1.nextElement
Dim F1 As java.io.InputStream = Zipe.getInputStream(Ze)
If Not Ze.isDirectory Then
Dim strPath As String = System.IO.Path.Combine("C:\ZipT\", System.IO.Path.GetDirectoryName(Ze.getName))
If Not System.IO.Directory.Exists(strPath) Then
System.IO.Directory.CreateDirectory(strPath)
End If
Dim F2 As New java.io.FileOutputStream(System.IO.Path.Combine(strPath, System.IO.Path.GetFileName(Ze.getName)))
CopyStream(F1, F2)
End If
End While
Zipe.close()
Catch ex As Exception
MsgBox("Error occurs while trying to extract Zip file '" & Zfile & "'!")
End Try
End Sub
Sub CreateZip(ByVal Zfile As String, ByVal Files() As String)
CreateEmptyZip(Zfile)
UpdateZipFile(Zfile, Files)
End Sub
Sub CreateEmptyZip(ByVal Zfile As String)
Dim Z1 As New ZipOutputStream(New java.io.FileOutputStream(Zfile))
Z1.close()
End Sub
Sub UpdateZipFile(ByVal Zfile As String, ByVal newFiles() As String)
Dim file As New ZipFile(Zfile)
Dim tmp As String = System.IO.Path.GetTempFileName()
Dim Zto As New ZipOutputStream(New java.io.FileOutputStream(tmp))
Try
Call CopyEntries(file, Zto)
If Not Microsoft.VisualBasic.IsDBNull(newFiles) Then
For Each f As String In newFiles
Dim z As New ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length))
z.setMethod(ZipEntry.DEFLATED)
Zto.putNextEntry(z)
Try
Dim s As New java.io.FileInputStream(f)
Try
CopyStream(s, Zto)
Finally
s.close()
End Try
Finally
Zto.closeEntry()
End Try
Next
End If
Finally
Zto.close()
file.close()
End Try
System.IO.File.Copy(tmp, Zfile, True)
System.IO.File.Delete(tmp)
End Sub
[推荐]VB.NET调用Java.io.zip进行压缩,懒人的好办法
我本人自认比较愚笨,在MSDN的网页上看到C#的示例后,想了N天,才把它转成我这样的“C盲”用户爱用的VB.NET语言,如有兴趣,可到[url]http://www.microsoft.com/china/msdn/library/langtool/vcsharp/miszipcompression.mspx[/url] 去看C#代码讲解;有不明白的地方,可来信[email]yelianfeng@126.com[/email]共同探讨,希望能对广大VB同仁有所助益。