ZipUp-Form1.vb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. Imports System.IO
  2. Imports Ionic.Zip
  3. Imports System.Threading
  4. Imports System.ComponentModel
  5. Public Class Form1
  6. Private _backgroundWorker1 As System.ComponentModel.BackgroundWorker
  7. Private _saveCanceled As Boolean
  8. Private _totalBytesAfterCompress As Long
  9. Private _totalBytesBeforeCompress As Long
  10. Private _nFilesCompleted As Integer
  11. Private _progress2MaxFactor As Integer
  12. Private _entriesToZip As Integer
  13. Private _appCuKey As Microsoft.Win32.RegistryKey
  14. Private AppRegyPath As String = "Software\Ionic\VBzipUp"
  15. Private rvn_ZipFile As String = "zipfile"
  16. Private rvn_DirToZip As String = "dirToZip"
  17. ' Delegates for invocation of UI from other threads
  18. Private Delegate Sub SaveEntryProgress(ByVal e As SaveProgressEventArgs)
  19. Private Delegate Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
  20. Private Sub btnDirBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDirBrowse.Click
  21. Dim folderName As String = Me.tbDirToZip.Text
  22. Dim dlg1 As New FolderBrowserDialog
  23. dlg1.SelectedPath = IIf(Directory.Exists(folderName), folderName, "c:\")
  24. dlg1.ShowNewFolderButton = False
  25. If (dlg1.ShowDialog = DialogResult.OK) Then
  26. 'Me._folderName = dlg1.get_SelectedPath
  27. Me.tbDirToZip.Text = folderName
  28. End If
  29. End Sub
  30. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZipUp.Click
  31. Me.KickoffZipup()
  32. End Sub
  33. Private Sub KickoffZipup()
  34. Dim folderName As String = Me.tbDirToZip.Text
  35. If (((Not folderName Is Nothing) AndAlso (folderName <> "")) AndAlso ((Not Me.tbZipToCreate.Text Is Nothing) AndAlso (Me.tbZipToCreate.Text <> ""))) Then
  36. If File.Exists(Me.tbZipToCreate.Text) Then
  37. If (MessageBox.Show(String.Format("The file you have specified ({0}) already exists. Do you want to overwrite this file?", _
  38. Me.tbZipToCreate.Text), "Confirmation is Required", _
  39. MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> DialogResult.Yes) Then
  40. Return
  41. End If
  42. File.Delete(Me.tbZipToCreate.Text)
  43. End If
  44. Me._saveCanceled = False
  45. Me._nFilesCompleted = 0
  46. Me._totalBytesAfterCompress = 0
  47. Me._totalBytesBeforeCompress = 0
  48. Me.btnZipUp.Enabled = False
  49. Me.btnZipUp.Text = "Zipping..."
  50. Me.btnCancel.Enabled = True
  51. Me.lblStatus.Text = "Zipping..."
  52. Dim options As New WorkerOptions
  53. options.ZipName = Me.tbZipToCreate.Text
  54. options.Folder = folderName
  55. _backgroundWorker1 = New System.ComponentModel.BackgroundWorker()
  56. _backgroundWorker1.WorkerSupportsCancellation = False
  57. _backgroundWorker1.WorkerReportsProgress = False
  58. AddHandler Me._backgroundWorker1.DoWork, New DoWorkEventHandler(AddressOf Me.DoSave)
  59. _backgroundWorker1.RunWorkerAsync(options)
  60. End If
  61. End Sub
  62. Private Sub DoSave(ByVal sender As Object, ByVal e As DoWorkEventArgs)
  63. Dim options As WorkerOptions = e.Argument
  64. Try
  65. Using zip1 As ZipFile = New ZipFile
  66. zip1.AddDirectory(options.Folder)
  67. Me._entriesToZip = zip1.EntryFileNames.Count
  68. Me.SetProgressBars()
  69. AddHandler zip1.SaveProgress, New EventHandler(Of SaveProgressEventArgs)(AddressOf Me.zip1_SaveProgress)
  70. zip1.Save(options.ZipName)
  71. End Using
  72. Catch exc1 As Exception
  73. MessageBox.Show(String.Format("Exception while zipping: {0}", exc1.Message))
  74. Me.btnCancel_Click(Nothing, Nothing)
  75. End Try
  76. End Sub
  77. Private Sub zip1_SaveProgress(ByVal sender As Object, ByVal e As SaveProgressEventArgs)
  78. If Me._saveCanceled Then
  79. e.Cancel = True
  80. Return
  81. End If
  82. Select Case e.EventType
  83. Case ZipProgressEventType.Saving_AfterWriteEntry
  84. Me.StepArchiveProgress(e)
  85. Exit Select
  86. Case ZipProgressEventType.Saving_Completed
  87. Me.SaveCompleted()
  88. Exit Select
  89. Case ZipProgressEventType.Saving_EntryBytesRead
  90. Me.StepEntryProgress(e)
  91. Exit Select
  92. End Select
  93. End Sub
  94. Private Sub StepArchiveProgress(ByVal e As SaveProgressEventArgs)
  95. If Me.progressBar1.InvokeRequired Then
  96. Me.progressBar1.Invoke(New SaveEntryProgress(AddressOf Me.StepArchiveProgress), New Object() {e})
  97. ElseIf Not Me._saveCanceled Then
  98. Me._nFilesCompleted += 1
  99. Me.progressBar1.PerformStep()
  100. Me._totalBytesAfterCompress = (Me._totalBytesAfterCompress + e.CurrentEntry.CompressedSize)
  101. Me._totalBytesBeforeCompress = (Me._totalBytesBeforeCompress + e.CurrentEntry.UncompressedSize)
  102. ' progressBar2 is the one dealing with the item being added to the archive
  103. ' if we got this event, then the add of that item (or file) is complete, so we
  104. ' update the progressBar2 appropriately.
  105. Me.progressBar2.Value = Me.progressBar2.Maximum = 1
  106. MyBase.Update()
  107. End If
  108. End Sub
  109. Private Sub SaveCompleted()
  110. If Me.lblStatus.InvokeRequired Then
  111. Me.lblStatus.Invoke(New MethodInvoker(AddressOf SaveCompleted))
  112. 'Me.lblStatus.Invoke(New MethodInvoker(Me, DirectCast(Me.SaveCompleted, IntPtr)))
  113. Else
  114. Me.lblStatus.Text = String.Format("Done, Compressed {0} files, {1:N0}% of original", Me._nFilesCompleted, ((100 * Me._totalBytesAfterCompress) / CDbl(Me._totalBytesBeforeCompress)))
  115. Me.ResetState()
  116. End If
  117. End Sub
  118. Private Sub StepEntryProgress(ByVal e As SaveProgressEventArgs)
  119. If Me.progressBar2.InvokeRequired Then
  120. Me.progressBar2.Invoke(New SaveEntryProgress(AddressOf Me.StepEntryProgress), New Object() {e})
  121. ElseIf Not Me._saveCanceled Then
  122. If (Me.progressBar2.Maximum = 1) Then
  123. Dim entryMax As Long = e.TotalBytesToTransfer
  124. Dim absoluteMax As Long = &H7FFFFFFF
  125. Me._progress2MaxFactor = 0
  126. Do While (entryMax > absoluteMax)
  127. entryMax = (entryMax / 2)
  128. Me._progress2MaxFactor += 1
  129. Loop
  130. If (CInt(entryMax) < 0) Then
  131. entryMax = (entryMax * -1)
  132. End If
  133. Me.progressBar2.Maximum = CInt(entryMax)
  134. Me.lblStatus.Text = String.Format("{0} of {1} files...({2})", (Me._nFilesCompleted + 1), Me._entriesToZip, e.CurrentEntry.FileName)
  135. End If
  136. Dim xferred As Integer = CInt((e.BytesTransferred >> Me._progress2MaxFactor))
  137. Me.progressBar2.Value = IIf((xferred >= Me.progressBar2.Maximum), Me.progressBar2.Maximum, xferred)
  138. MyBase.Update()
  139. End If
  140. End Sub
  141. Private Sub ResetState()
  142. Me.btnCancel.Enabled = False
  143. Me.btnZipUp.Enabled = True
  144. Me.btnZipUp.Text = "Zip it!"
  145. Me.progressBar1.Value = 0
  146. Me.progressBar2.Value = 0
  147. Me.Cursor = Cursors.Default
  148. End Sub
  149. Private Sub SetProgressBars()
  150. If Me.ProgressBar1.InvokeRequired Then
  151. 'Me.ProgressBar1.Invoke(New MethodInvoker(Me, DirectCast(Me.SetProgressBars, IntPtr)))
  152. Me.ProgressBar1.Invoke(New MethodInvoker(AddressOf SetProgressBars))
  153. Else
  154. Me.ProgressBar1.Value = 0
  155. Me.ProgressBar1.Maximum = Me._entriesToZip
  156. Me.ProgressBar1.Minimum = 0
  157. Me.ProgressBar1.Step = 1
  158. Me.ProgressBar2.Value = 0
  159. Me.ProgressBar2.Minimum = 0
  160. Me.ProgressBar2.Maximum = 1
  161. Me.ProgressBar2.Step = 2
  162. End If
  163. End Sub
  164. Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  165. If Me.lblStatus.InvokeRequired Then
  166. Me.lblStatus.Invoke(New ButtonClick(AddressOf Me.btnCancel_Click), New Object() {sender, e})
  167. Else
  168. Me._saveCanceled = True
  169. Me.lblStatus.Text = "Canceled..."
  170. Me.ResetState()
  171. End If
  172. End Sub
  173. Private Sub SaveFormToRegistry()
  174. If AppCuKey IsNot Nothing Then
  175. If Not String.IsNullOrEmpty(tbZipToCreate.Text) Then
  176. AppCuKey.SetValue(rvn_ZipFile, Me.tbZipToCreate.Text)
  177. End If
  178. If Not String.IsNullOrEmpty(tbDirToZip.Text) Then
  179. AppCuKey.SetValue(rvn_DirToZip, tbDirToZip.Text)
  180. End If
  181. End If
  182. End Sub
  183. Private Sub LoadFormFromRegistry()
  184. If AppCuKey IsNot Nothing Then
  185. Dim s As String
  186. s = AppCuKey.GetValue(rvn_ZipFile)
  187. If Not String.IsNullOrEmpty(s) Then
  188. Me.tbZipToCreate.Text = s
  189. End If
  190. s = AppCuKey.GetValue(rvn_DirToZip)
  191. If Not String.IsNullOrEmpty(s) Then
  192. tbDirToZip.Text = s
  193. End If
  194. End If
  195. End Sub
  196. Public ReadOnly Property AppCuKey() As Microsoft.Win32.RegistryKey
  197. Get
  198. If (_appCuKey Is Nothing) Then
  199. Me._appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(AppRegyPath, True)
  200. If (Me._appCuKey Is Nothing) Then
  201. Me._appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(AppRegyPath)
  202. End If
  203. End If
  204. Return _appCuKey
  205. End Get
  206. End Property
  207. Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  208. SaveFormToRegistry()
  209. End Sub
  210. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  211. LoadFormFromRegistry()
  212. End Sub
  213. End Class
  214. Public Class WorkerOptions
  215. ' Fields
  216. 'Public Comment As String
  217. 'Public CompressionLevel As CompressionLevel
  218. 'Public Encoding As String
  219. 'Public Encryption As EncryptionAlgorithm
  220. Public Folder As String
  221. 'Public Password As String
  222. 'Public Zip64 As Zip64Option
  223. 'Public ZipFlavor As Integer
  224. Public ZipName As String
  225. End Class