Selasa, 25 November 2008

Source Code - Menggunakan 7z dengan Visual Basic.NET

1. Option Strict On
2.
3. Imports System.ComponentModel
4.
5. Public Class SevenZip
6.
7. Private Shared ZipPath As String = AppController.RootPath & "7z.exe"
8.
9. Public Event OnZipComplete(ByVal ZipFileName As String)
10.
11.
12. Public Shared Function Extract(ByVal filename As String, ByVal zipname As String) As Process
13. Dim p As New Process()
14. p.StartInfo.FileName = ZipPath
15. p.StartInfo.Arguments = "x -y -o""" & filename & """ """ & zipname & """"
16. p.StartInfo.CreateNoWindow = True
17. p.Start()
18. Return p
19. End Function
20.
21.
22. Public Shared Function Compress(ByVal filename As String, ByVal zipname As String) As Process
23. Dim p As New Process()
24. p.StartInfo.FileName = ZipPath
25. p.StartInfo.Arguments = "a -r -tzip """ & zipname & """ """ & filename & """"
26. p.StartInfo.CreateNoWindow = True
27. p.Start()
28. Return p
29. End Function
30.
31. Public Shared Function Compress(ByVal filename As String, ByVal zipnames() As String) As Process
32. Dim p As New Process()
33. p.StartInfo.FileName = ZipPath
34.
35. Dim args As String = "a -r -tzip "
36. Dim s As String
37.
38. For Each s In zipnames
39. args &= """" & s & """ "
40. Next
41.
42. p.StartInfo.Arguments = args & filename & """"
43. p.StartInfo.CreateNoWindow = True
44. p.Start()
45. Return p
46. End Function
47.
48.
49. Private Structure FileCopy
50. Public Source As String
51. Public Destination As String
52.
53. Public Sub New(ByVal source As String, ByVal dest As String)
54. Me.Source = source
55. Me.Destination = dest
56. End Sub
57. End Structure
58.
59.
60. Public Sub CompressInNewThread(ByVal filename As String, ByVal zipname As String)
61. Dim fc As New FileCopy(filename, zipname)
62.
63. 'run zip compress in another thread
64. bw = New System.ComponentModel.BackgroundWorker()
65. bw.RunWorkerAsync(fc)
66. End Sub
67.
68.
69. Private WithEvents bw As BackgroundWorker
70.
71. Private Sub StartZip(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
72. Dim fc As FileCopy = DirectCast(e.Argument, FileCopy)
73.
74. Dim p As New Process()
75. p.StartInfo.FileName = ZipPath
76. p.StartInfo.Arguments = "a -r -tzip """ & fc.Destination & """ """ & fc.Source & """"
77. p.Start()
78. p.WaitForExit()
79.
80. 'return the zip filepath
81. e.Result = fc.Destination
82. End Sub
83.
84. Private Sub EndZip(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
85. RaiseEvent OnZipComplete(Convert.ToString(e.Result))
86. End Sub
87.
88. End Class

Tidak ada komentar: