Unzip-Form1.vb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. Imports System
  2. Imports System.IO
  3. Imports Ionic.Zip
  4. Imports System.ComponentModel
  5. Public Class Form1
  6. Private _backgroundWorker1 As System.ComponentModel.BackgroundWorker
  7. Private _operationCanceled As Boolean
  8. Private nFilesCompleted As Integer
  9. Private totalEntriesToProcess As Integer
  10. Private _appCuKey As Microsoft.Win32.RegistryKey
  11. Private AppRegyPath As String = "Software\Ionic\VBunZip"
  12. Private rvn_ZipFile As String = "zipfile"
  13. Private rvn_ExtractDir As String = "extractdir"
  14. Private Delegate Sub ZipProgress(ByVal e As ExtractProgressEventArgs)
  15. Private Sub btnZipBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnZipBrowse.Click
  16. Dim openFileDialog1 As New OpenFileDialog
  17. If (String.IsNullOrEmpty(tbZipToOpen.Text)) Then
  18. openFileDialog1.InitialDirectory = "c:\"
  19. Else
  20. openFileDialog1.InitialDirectory = IIf(File.Exists(Me.tbZipToOpen.Text), Path.GetDirectoryName(Me.tbZipToOpen.Text), Me.tbZipToOpen.Text)
  21. End If
  22. openFileDialog1.Filter = "zip files|*.zip|EXE files|*.exe|All Files|*.*"
  23. openFileDialog1.FilterIndex = 1
  24. openFileDialog1.RestoreDirectory = True
  25. If (openFileDialog1.ShowDialog = DialogResult.OK) Then
  26. Me.tbZipToOpen.Text = openFileDialog1.FileName
  27. If File.Exists(Me.tbZipToOpen.Text) Then
  28. Me.btnUnzip_Click(sender, e)
  29. End If
  30. End If
  31. End Sub
  32. Private Sub btnUnzip_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUnzip.Click
  33. If Not File.Exists(Me.tbZipToOpen.Text) Then
  34. MessageBox.Show("That file does not exist", "Cannot Unzip", MessageBoxButtons.OK)
  35. End If
  36. If Not String.IsNullOrEmpty(tbZipToOpen.Text) And _
  37. Not String.IsNullOrEmpty(tbExtractDir.Text) Then
  38. If Not Directory.Exists(tbExtractDir.Text) Then
  39. Directory.CreateDirectory(tbExtractDir.Text)
  40. End If
  41. nFilesCompleted = 0
  42. _operationCanceled = False
  43. btnCancel.Enabled = True
  44. btnUnzip.Enabled = False
  45. btnZipBrowse.Enabled = False
  46. btnExtractDirBrowse.Enabled = False
  47. tbZipToOpen.Enabled = False
  48. tbExtractDir.Enabled = False
  49. KickoffExtract()
  50. End If
  51. End Sub
  52. Private Sub KickoffExtract()
  53. lblStatus.Text = "Extracting..."
  54. Dim args(2) As String
  55. args(0) = tbZipToOpen.Text
  56. args(1) = tbExtractDir.Text
  57. _backgroundWorker1 = New System.ComponentModel.BackgroundWorker()
  58. _backgroundWorker1.WorkerSupportsCancellation = False
  59. _backgroundWorker1.WorkerReportsProgress = False
  60. AddHandler Me._backgroundWorker1.DoWork, New DoWorkEventHandler(AddressOf Me.UnzipFile)
  61. _backgroundWorker1.RunWorkerAsync(args)
  62. End Sub
  63. Private Sub btnExtractDirBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExtractDirBrowse.Click
  64. Dim dlg As New FolderBrowserDialog
  65. dlg.Description = "Select a folder to zip up:"
  66. dlg.ShowNewFolderButton = False
  67. 'dlg.ShowEditBox = True
  68. dlg.SelectedPath = Me.tbExtractDir.Text
  69. 'dlg.ShowFullPathInEditBox = True
  70. If (dlg.ShowDialog = DialogResult.OK) Then
  71. tbExtractDir.Text = dlg.SelectedPath
  72. End If
  73. End Sub
  74. Private Sub UnzipFile(ByVal sender As Object, ByVal e As DoWorkEventArgs)
  75. Dim extractCancelled As Boolean = False
  76. Dim args() As String = e.Argument
  77. Dim zipToRead As String = args(0)
  78. Dim extractDir As String = args(1)
  79. Try
  80. Using zip As ZipFile = ZipFile.Read(zipToRead)
  81. totalEntriesToProcess = zip.Entries.Count
  82. SetProgressBarMax(zip.Entries.Count)
  83. AddHandler zip.ExtractProgress, New EventHandler(Of ExtractProgressEventArgs)(AddressOf Me.zip_ExtractProgress)
  84. zip.ExtractAll(extractDir, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
  85. End Using
  86. Catch ex1 As Exception
  87. MessageBox.Show(String.Format("There's been a problem extracting that zip file. {0}", ex1.Message), "Error Extracting", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
  88. End Try
  89. ResetUI()
  90. End Sub
  91. Private Sub ResetUI()
  92. If btnCancel.InvokeRequired Then
  93. btnCancel.Invoke(New Action(AddressOf ResetUI), New Object() {})
  94. Else
  95. btnUnzip.Enabled = True
  96. btnZipBrowse.Enabled = True
  97. btnExtractDirBrowse.Enabled = True
  98. btnCancel.Enabled = False
  99. tbZipToOpen.Enabled = True
  100. tbExtractDir.Enabled = True
  101. ProgressBar1.Maximum = 1
  102. ProgressBar1.Value = 0
  103. Me.btnUnzip.Focus()
  104. End If
  105. End Sub
  106. Private Sub SetProgressBarMax(ByVal n As Integer)
  107. If ProgressBar1.InvokeRequired Then
  108. ProgressBar1.Invoke(New Action(Of Integer)(AddressOf SetProgressBarMax), New Object() {n})
  109. Else
  110. ProgressBar1.Value = 0
  111. ProgressBar1.Maximum = n
  112. ProgressBar1.Step = 1
  113. End If
  114. End Sub
  115. Private Sub zip_ExtractProgress(ByVal sender As Object, ByVal e As ExtractProgressEventArgs)
  116. If _operationCanceled Then
  117. e.Cancel = True
  118. Return
  119. End If
  120. If (e.EventType = Ionic.Zip.ZipProgressEventType.Extracting_AfterExtractEntry) Then
  121. StepEntryProgress(e)
  122. ElseIf (e.EventType = ZipProgressEventType.Extracting_BeforeExtractAll) Then
  123. '' do nothing
  124. End If
  125. End Sub
  126. Private Sub StepEntryProgress(ByVal e As ExtractProgressEventArgs)
  127. If ProgressBar1.InvokeRequired Then
  128. ProgressBar1.Invoke(New ZipProgress(AddressOf StepEntryProgress), New Object() {e})
  129. Else
  130. ProgressBar1.PerformStep()
  131. System.Threading.Thread.Sleep(100)
  132. 'set a label with status information
  133. nFilesCompleted = nFilesCompleted + 1
  134. lblStatus.Text = String.Format("{0} of {1} files...({2})", nFilesCompleted, totalEntriesToProcess, e.CurrentEntry.FileName)
  135. Me.Update()
  136. End If
  137. End Sub
  138. 'Private Sub StepArchiveProgress(ByVal e As ZipProgressEventArgs)
  139. ' If ProgressBar1.InvokeRequired Then
  140. ' ProgressBar1.Invoke(New ZipProgress(AddressOf StepArchiveProgress), New Object() {e})
  141. ' ElseIf Not _operationCanceled Then
  142. ' _nFilesCompleted = _nFilesCompleted + 1
  143. ' ProgressBar1.PerformStep()
  144. ' progressBar2.Value = progressBar2.Maximum = 1
  145. ' MyBase.Update()
  146. ' End If
  147. 'End Sub
  148. Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  149. _operationCanceled = True
  150. ProgressBar1.Maximum = 1
  151. ProgressBar1.Value = 0
  152. lblStatus.Text = "Cancelled..."
  153. End Sub
  154. Private Sub SaveFormToRegistry()
  155. If AppCuKey IsNot Nothing Then
  156. If Not String.IsNullOrEmpty(tbZipToOpen.Text) Then
  157. AppCuKey.SetValue(rvn_ZipFile, Me.tbZipToOpen.Text)
  158. End If
  159. If Not String.IsNullOrEmpty(tbExtractDir.Text) Then
  160. AppCuKey.SetValue(rvn_ExtractDir, tbExtractDir.Text)
  161. End If
  162. End If
  163. End Sub
  164. Private Sub LoadFormFromRegistry()
  165. If AppCuKey IsNot Nothing Then
  166. Dim s As String
  167. s = AppCuKey.GetValue(rvn_ZipFile)
  168. If Not String.IsNullOrEmpty(s) Then
  169. Me.tbZipToOpen.Text = s
  170. End If
  171. s = AppCuKey.GetValue(rvn_ExtractDir)
  172. If Not String.IsNullOrEmpty(s) Then
  173. tbExtractDir.Text = s
  174. End If
  175. End If
  176. End Sub
  177. Public ReadOnly Property AppCuKey() As Microsoft.Win32.RegistryKey
  178. Get
  179. If (_appCuKey Is Nothing) Then
  180. Me._appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(AppRegyPath, True)
  181. If (Me._appCuKey Is Nothing) Then
  182. Me._appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(AppRegyPath)
  183. End If
  184. End If
  185. Return _appCuKey
  186. End Get
  187. End Property
  188. Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  189. SaveFormToRegistry()
  190. End Sub
  191. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  192. LoadFormFromRegistry()
  193. End Sub
  194. End Class