added samples
[windows-sources.git] / sdk / samples / FrameworkSamples / CLR / CompressionSample / vb / compression / compressionform.vb
bloba672aa5b26b76dc5523c651f8aeacca1102975c1
1 '-----------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET SDK Code Samples.
3 '
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
5 '
6 'This source code is intended only as a supplement to Microsoft
7 'Development Tools and/or on-line documentation. See these other
8 'materials for detailed information regarding Microsoft code samples.
9 '
10 'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
13 'PARTICULAR PURPOSE.
14 '-----------------------------------------------------------------------
15 Imports System
16 Imports System.Drawing
17 Imports System.Collections
18 Imports System.ComponentModel
19 Imports System.Windows.Forms
20 Imports System.Data
21 Imports System.Collections.Generic
22 Imports System.Threading
25 '/ <summary>
26 '/ Main form for the application
27 '/ </summary>
29 Partial Public Class CompressionForm
30 Inherits System.Windows.Forms.Form
31 '/ <summary>
32 '/ Required designer variable.
33 '/ </summary>
34 Private components As System.ComponentModel.IContainer = Nothing
35 Private cFileDialog As CustomFileDialog ' Dialog item for menu clicks
36 Private archive As ZipFile 'Archive for zipping operations
37 Public Shared statusMessage As String
38 'For the status message to fixed by
39 'other components
40 Public Sub New()
41 statusMessage = Nothing
43 InitializeComponent()
44 'Initialize the list view
45 fileListView.View = View.Details
46 fileListView.GridLines = True
47 fileListView.Columns.Add("Name", 150, HorizontalAlignment.Left)
48 fileListView.Columns.Add("Modified", 130, HorizontalAlignment.Left)
49 fileListView.Columns.Add("Size", 75, HorizontalAlignment.Right)
50 fileListView.Columns.Add("Ratio %", 50, HorizontalAlignment.Right)
51 fileListView.Columns.Add("Compressed size", 95, HorizontalAlignment.Right)
52 fileListView.Columns.Add("Path", 250, HorizontalAlignment.Left)
54 EnableControls(False)
55 EnableExtractRemoveButtons(False)
57 cFileDialog = New CustomFileDialog()
59 End Sub 'New
62 Private Sub EnableControls(ByVal value As Boolean)
63 addMenuStripButton.Enabled = value
64 addToolStripButton.Enabled = value
65 extractAllMenuStripButton.Enabled = value
67 End Sub 'EnableControls
70 Private Sub newGzipToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newGzipToolStripButton.Click, newGzipMenuToolStripButton.Click
72 NewArchive(ZipConstants.GZIP)
74 End Sub 'newGzipToolStripMenuItem_Click
76 Private Sub newDeflateToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newDeflateToolStripButton.Click, newDeflateMenuToolStripButton.Click
78 NewArchive(ZipConstants.DEFLATE)
80 End Sub 'newDeflateToolStripMenuItem_Click
83 Private Sub NewArchive(ByVal method As Byte)
84 Dim name As String = cFileDialog.NewMode()
85 If name Is Nothing Then
86 fileListView.Focus()
87 Return
88 End If
90 Dim mode As System.IO.FileMode = System.IO.FileMode.CreateNew
91 If System.IO.File.Exists(name) Then
92 If ShowOverwriteDialog() = MsgBoxResult.Yes Then
93 mode = System.IO.FileMode.Truncate
94 Else
95 fileListView.Focus()
96 Return
97 End If
98 End If
100 If Not (archive Is Nothing) Then
101 archive.Close()
102 End If
103 archive = New ZipFile(name, method, mode)
104 Clear()
106 If statusMessage.Length <> 0 Then
107 DisplayStatusMessage()
108 ChangeTitle(name, archive.CompressionMethod())
109 EnableControls(True)
110 Else
111 Clear()
112 End If
114 End Sub 'NewArchive
117 Private Function ShowOverwriteDialog() As DialogResult
118 Dim opt As MessageBoxOptions
119 If System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft = True Then
120 opt = MessageBoxOptions.RightAlign Or MessageBoxOptions.RtlReading
121 Else
122 opt = MessageBoxOptions.DefaultDesktopOnly
123 End If
124 Return MessageBox.Show(ZipConstants.FileReplace, ZipConstants.Replace, MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, opt)
126 End Function 'ShowOverwriteDialog
129 Private Sub openToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles openToolStripButton.Click, openMenuStripButton.Click
131 Dim name As String = cFileDialog.OpenMode()
132 If name Is Nothing Then
133 fileListView.Focus()
134 Return
135 End If
136 If Not (archive Is Nothing) Then
137 archive.Close()
138 End If
139 archive = New ZipFile(name)
140 Clear()
142 If statusMessage.Length <> 0 Then
143 DisplayStatusMessage()
144 ChangeTitle(name, archive.CompressionMethod())
145 EnableControls(True)
146 Else
147 Clear()
148 End If
150 End Sub 'openToolStripMenuItem_Click
153 Private Sub closeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles closeMenuStripButton.Click
155 Me.Close()
157 End Sub 'closeToolStripMenuItem_Click
159 Private Sub removeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles removeToolStripButton.Click, removeMenuStripButton.Click, removeContextMenuStripButton.Click
162 Dim index As Integer
163 For Each index In fileListView.SelectedIndices
164 archive.Remove(index)
165 Next index
166 If fileListView.SelectedIndices.Count = 0 Then
167 statusMessage = String.Empty
168 End If
169 RefreshListView()
170 DisplayStatusMessage()
172 End Sub 'removeToolStripMenuItem_Click
175 Private Sub addToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addToolStripButton.Click, addMenuStripButton.Click
178 Dim names As String() = cFileDialog.AddMode()
179 If names Is Nothing Then
180 fileListView.Focus()
181 Return
182 End If
183 Dim name As String
184 For Each name In names
185 Dim index As Integer = archive.CheckFileExists(name)
186 If index <> -1 Then
187 statusMessage = ZipConstants.FileExistsError
188 RefreshListView()
189 DisplayStatusMessage()
190 fileListView.Focus()
191 fileListView.Items(index).Selected = True
192 Else
193 archive.Add(name)
194 RefreshListView()
195 DisplayStatusMessage()
196 End If
197 Next name
199 End Sub 'addToolStripMenuItem_Click
202 Private Sub extractToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles extractToolStripButton.Click, extractMenuStripButton.Click
204 'string dir = GetFolderName();
205 CreateDir()
206 Dim index As Integer
207 For Each index In fileListView.SelectedIndices
208 archive.Extract(index, "C:\temp\zipTemp")
209 Next index
211 If fileListView.SelectedIndices.Count = 0 Then
212 statusMessage = String.Empty
213 End If
214 DisplayStatusMessage()
216 End Sub 'extractToolStripMenuItem_Click
219 Private Sub extractAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles extractAllMenuStripButton.Click
222 CreateDir()
223 archive.ExtractAll("C:\temp\zipTemp")
224 DisplayStatusMessage()
226 End Sub 'extractAllToolStripMenuItem_Click
229 Private Sub RefreshListView()
230 Dim entries As List(Of ZipEntry)
231 entries = archive.Entries
232 If entries Is Nothing Then
233 Return
234 End If
235 fileListView.Items.Clear()
237 EnableExtractRemoveButtons(False)
239 Dim entry As ZipEntry
240 For Each entry In entries
241 Dim index As Integer = entry.Name.LastIndexOf(ZipConstants.BackSlash)
242 entry.Name.Substring(0, index)
243 Dim lvi As New ListViewItem(entry.Name.Substring(index + 1))
244 lvi.SubItems.Add(entry.ModifiedDateTime.ToString(Thread.CurrentThread.CurrentUICulture))
246 lvi.SubItems.Add(entry.Size.ToString(Thread.CurrentThread.CurrentUICulture))
249 Dim ratio As Integer
250 If (entry.Size = 0) Then
251 ratio = 0
252 Else
253 ratio = System.Convert.ToInt32((System.Convert.ToDouble(entry.Size - entry.CompressedSize) / entry.Size * 100))
254 End If
256 ratio = IIf(ratio < 0, 0, ratio) 'TODO: For performance reasons this should be changed to nested IF statements
258 lvi.SubItems.Add(ratio.ToString(Thread.CurrentThread.CurrentUICulture))
260 lvi.SubItems.Add(entry.CompressedSize.ToString(Thread.CurrentThread.CurrentUICulture))
262 lvi.SubItems.Add(entry.Name.Substring(0, index))
263 fileListView.Items.Add(lvi)
264 Next entry
266 End Sub 'RefreshListView
268 Public Sub DisplayStatusMessage()
269 'mainStatusStripPanel.Text = statusMessage
271 End Sub 'DisplayStatusMessage
274 Private Sub ChangeTitle(ByVal name As String, ByVal method As Byte)
275 Dim index As Integer = name.LastIndexOf(ZipConstants.BackSlash)
276 Dim methodName As String = Nothing
277 If method = ZipConstants.GZIP Then
278 methodName = ZipConstants.GzipName
279 End If
280 If method = ZipConstants.DEFLATE Then
281 methodName = ZipConstants.DeflateName
282 End If
283 Me.Text = ZipConstants.Title + "-" + name.Substring(index + 1) + " (" + methodName + ")"
285 End Sub 'ChangeTitle
288 Private Sub Clear()
289 Me.Text = ZipConstants.Title
290 EnableExtractRemoveButtons(False)
291 EnableControls(False)
292 fileListView.Items.Clear()
293 RefreshListView()
295 End Sub 'Clear
298 Private Function GetFolderName() As String
299 Dim dir As String = String.Empty
300 Dim fbd As New FolderBrowserDialog()
301 If fbd.ShowDialog() = MsgBoxResult.OK Then
302 dir = fbd.SelectedPath
303 End If
304 Return dir
306 End Function 'GetFolderName
309 Private Sub CreateDir()
310 If System.IO.Directory.Exists("C:\temp\zipTemp") Then
311 Return
312 End If
313 System.IO.Directory.CreateDirectory("C:\temp\zipTemp")
315 End Sub 'CreateDir
318 Private Sub fileListView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles fileListView.SelectedIndexChanged
319 If fileListView.SelectedIndices.Count > 0 Then
320 EnableExtractRemoveButtons(True)
321 Else
322 EnableExtractRemoveButtons(False)
323 End If
325 End Sub 'fileListView_SelectedIndexChanged
328 Private Sub contextMenuStripButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles contextMenuStripButton.Click
331 extractToolStripMenuItem_Click(sender, e)
333 End Sub 'contextMenuStripButton_Click
335 Private Sub fileListView_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles fileListView.MouseUp
336 If e.Button <> System.Windows.Forms.MouseButtons.Right OrElse fileListView.SelectedIndices.Count = 0 Then
337 Return
338 End If
339 Dim info As ListViewHitTestInfo = fileListView.HitTest(e.X, e.Y)
340 If info.Item Is Nothing Then
341 Return
342 End If
343 fileContextMenuStrip.Show(fileListView, e.X, e.Y)
345 End Sub 'fileListView_MouseUp
348 Private Sub EnableExtractRemoveButtons(ByVal value As Boolean)
349 extractMenuStripButton.Enabled = value
350 extractToolStripButton.Enabled = value
351 removeToolStripButton.Enabled = value
352 removeMenuStripButton.Enabled = value
354 End Sub 'EnableExtractRemoveButtons
355 End Class 'CompressionSample