1 '-----------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET SDK Code Samples.
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
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.
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
14 '-----------------------------------------------------------------------
16 Imports System
.Drawing
17 Imports System
.Collections
18 Imports System
.ComponentModel
19 Imports System
.Windows
.Forms
21 Imports System
.Collections
.Generic
22 Imports System
.Threading
26 '/ Main form for the application
29 Partial
Public Class CompressionForm
30 Inherits System
.Windows
.Forms
.Form
32 '/ Required designer variable.
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
41 statusMessage
= Nothing
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
)
55 EnableExtractRemoveButtons(False)
57 cFileDialog
= New CustomFileDialog()
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
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
100 If Not (archive Is
Nothing) Then
103 archive
= New ZipFile(name
, method
, mode
)
106 If statusMessage
.Length
<> 0 Then
107 DisplayStatusMessage()
108 ChangeTitle(name
, archive
.CompressionMethod())
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
122 opt
= MessageBoxOptions
.DefaultDesktopOnly
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
136 If Not (archive Is
Nothing) Then
139 archive
= New ZipFile(name
)
142 If statusMessage
.Length
<> 0 Then
143 DisplayStatusMessage()
144 ChangeTitle(name
, archive
.CompressionMethod())
150 End Sub 'openToolStripMenuItem_Click
153 Private Sub closeToolStripMenuItem_Click(ByVal sender
As Object, ByVal e
As System
.EventArgs
) Handles closeMenuStripButton
.Click
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
163 For Each index
In fileListView
.SelectedIndices
164 archive
.Remove(index
)
166 If fileListView
.SelectedIndices
.Count
= 0 Then
167 statusMessage
= String.Empty
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
184 For Each name
In names
185 Dim index
As Integer = archive
.CheckFileExists(name
)
187 statusMessage
= ZipConstants
.FileExistsError
189 DisplayStatusMessage()
191 fileListView
.Items(index
).Selected
= True
195 DisplayStatusMessage()
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();
207 For Each index
In fileListView
.SelectedIndices
208 archive
.Extract(index
, "C:\temp\zipTemp")
211 If fileListView
.SelectedIndices
.Count
= 0 Then
212 statusMessage
= String.Empty
214 DisplayStatusMessage()
216 End Sub 'extractToolStripMenuItem_Click
219 Private Sub extractAllToolStripMenuItem_Click(ByVal sender
As Object, ByVal e
As EventArgs
) Handles extractAllMenuStripButton
.Click
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
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
))
250 If (entry
.Size
= 0) Then
253 ratio
= System
.Convert
.ToInt32((System
.Convert
.ToDouble(entry
.Size
- entry
.CompressedSize
) / entry
.Size
* 100))
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
)
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
280 If method
= ZipConstants
.DEFLATE
Then
281 methodName
= ZipConstants
.DeflateName
283 Me.Text
= ZipConstants
.Title
+ "-" + name
.Substring(index
+ 1) + " (" + methodName
+ ")"
289 Me.Text
= ZipConstants
.Title
290 EnableExtractRemoveButtons(False)
291 EnableControls(False)
292 fileListView
.Items
.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
306 End Function 'GetFolderName
309 Private Sub CreateDir()
310 If System
.IO
.Directory
.Exists("C:\temp\zipTemp") Then
313 System
.IO
.Directory
.CreateDirectory("C:\temp\zipTemp")
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)
322 EnableExtractRemoveButtons(False)
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
339 Dim info
As ListViewHitTestInfo
= fileListView
.HitTest(e
.X
, e
.Y
)
340 If info
.Item Is
Nothing Then
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