-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZipExamples.vb
256 lines (231 loc) · 8.5 KB
/
ZipExamples.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
Imports Microsoft.VisualBasic
#Region "#usings"
Imports DevExpress.Compression
#End Region ' #usings
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Reflection
Imports System.Windows.Forms
Namespace CompressionLibraryExamples
Friend Class ZipExamples
Private startupPath As String
Private sourceFiles() As String
Public Sub New(ByVal startupPath As String, ByVal sourceFiles() As String)
Me.startupPath = startupPath
Me.sourceFiles = sourceFiles
End Sub
#Region "#archivedirectory"
Public Sub ArchiveDirectory()
Dim path As String = Me.startupPath
Using archive As New ZipArchive()
archive.AddDirectory(path)
archive.Save("Documents\ArchiveDirectory.zip")
End Using
End Sub
#End Region ' #archivedirectory
#Region "#archivefiles"
Public Sub ArchiveFiles()
Dim sourcefiles() As String = Me.sourceFiles
Using archive As New ZipArchive()
For Each file As String In sourcefiles
archive.AddFile(file, "/")
Next file
archive.Save("Documents\ArchiveFiles.zip")
End Using
End Sub
#End Region ' #archivefiles
#Region "#archivedirectoryhandlingerrors"
Public Sub ArchiveDirectoryWithError()
Dim path As String = Me.startupPath
Using archive As New ZipArchive()
AddHandler archive.Error, AddressOf archive_Error
archive.AddDirectory(path)
archive.Save("Documents\ArchiveDirectoryWithError.zip")
End Using
End Sub
Private Sub archive_Error(ByVal sender As Object, ByVal args As DevExpress.Compression.ErrorEventArgs)
Dim errorMessage As String
Dim e As Exception = args.GetException()
If String.IsNullOrEmpty(args.ItemName) Then
errorMessage = e.Message
Else
errorMessage = String.Format("Item: {0}" & Constants.vbLf + Constants.vbLf & "Description:" & Constants.vbLf & "{1}", args.ItemName, e.Message)
End If
Dim descriptionMessage As String = "Click Cancel to abort operation. Click OK to skip the item and continue."
Dim message As String = String.Format("{0}" & Constants.vbLf & "{1}", errorMessage, descriptionMessage)
If MessageBox.Show(message, "Error", MessageBoxButtons.OKCancel) = DialogResult.Cancel Then
args.CanContinue = False
End If
End Sub
#End Region ' #archivedirectoryhandlingerrors
#Region "#filterarchivefiles"
'INSTANT VB TODO TASK: There is no VB.NET equivalent to 'volatile':
'ORIGINAL LINE: volatile bool stopArchiving = False;
Private stopArchiving As Boolean = False
Public Sub FilterArchiveFiles()
Dim sourcefiles() As String = Me.sourceFiles
Using archive As New ZipArchive()
AddHandler archive.ItemAdding, AddressOf archive_ItemAdding
For Each file As String In Me.sourceFiles
archive.AddFile(file, "/")
Next file
archive.Save("Documents\FilterArchiveFiles.zip")
End Using
End Sub
Private Sub archive_ItemAdding(ByVal sender As Object, ByVal args As ZipItemAddingEventArgs)
If args.Item.CreationTime.Date <> DateTime.Today Then
args.Action = ZipItemAddingAction.Cancel
End If
If stopArchiving Then
args.Action = ZipItemAddingAction.Stop
End If
End Sub
#End Region ' #filterarchivefiles
#Region "#archivefilesbatch"
Public Sub ArchiveFilesBatch()
Dim path As String = Me.startupPath
Using archive As New ZipArchive()
Dim files =
From file In System.IO.Directory.EnumerateFiles(path, "*.xml", System.IO.SearchOption.AllDirectories), line In System.IO.File.ReadLines(file)
Where line.Contains("DevExpress")
Select file
archive.AddFiles(files)
archive.Save("Documents\ArchiveFilesBatch.zip")
End Using
End Sub
#End Region ' #archivefilesbatch
#Region "#cancelarchiveprogress"
'INSTANT VB TODO TASK: There is no VB.NET equivalent to 'volatile':
'ORIGINAL LINE: volatile bool stopProgress = False;
Private stopProgress As Boolean = False
Public Sub CancelArchiveProgress()
Dim sourcefiles() As String = Me.sourceFiles
Using archive As New ZipArchive()
AddHandler archive.Progress, AddressOf archive_Progress
For Each file As String In Me.sourceFiles
archive.AddFile(file, "/")
Next file
archive.Save("Documents\CancelArchiveProgress.zip")
End Using
End Sub
Private Sub archive_Progress(ByVal sender As Object, ByVal args As ProgressEventArgs)
args.CanContinue = Not Me.stopProgress
End Sub
#End Region ' #cancelarchiveprogress
#Region "#protectpassword"
Public Sub ProtectPassword()
Dim sourcefiles() As String = Me.sourceFiles
Dim password As String = "123"
Using archive As New ZipArchive()
For Each file As String In Me.sourceFiles
Dim zipFI As ZipFileItem = archive.AddFile(file, "/")
zipFI.EncryptionType = EncryptionType.Aes128
zipFI.Password = password
Next file
archive.Save("Documents\ProtectPassword.zip")
End Using
End Sub
#End Region ' #protectpassword
#Region "#addcomment"
Public Sub ArchiveWithComment()
Dim path As String = Me.startupPath
Using archive As New ZipArchive()
For Each file As String In System.IO.Directory.EnumerateFiles(path)
Dim zipFI As ZipFileItem = archive.AddFile(file, "/")
zipFI.Comment = "Archived by " & Environment.UserName
Next file
archive.Save("Documents\ArchiveWithComment.zip")
End Using
End Sub
#End Region ' #addcomment
#Region "#archivestream"
Public Sub ArchiveStream()
Using myStream As Stream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes("DevExpress"))
Using myZippedStream As Stream = New FileStream("Documents\ArchiveStream.zip", System.IO.FileMode.Create)
Using archive As New ZipArchive()
archive.AddStream("myStream", myStream)
archive.Save(myZippedStream)
End Using
End Using
End Using
End Sub
#End Region ' #archivestream
#Region "#archivebytearray"
Public Sub ArchiveByteArray()
Dim myByteArray() As Byte = Enumerable.Repeat(CByte(&H78), 10000).ToArray()
Using myZippedStream As Stream = New FileStream("Documents\ArchiveByteArray.zip", FileMode.Create)
Using archive As New ZipArchive()
archive.AddByteArray("myByteArray", myByteArray)
archive.Save(myZippedStream)
End Using
End Using
End Sub
#End Region ' #archivebytearray
#Region "#unziparchive"
Public Sub UnzipArchive()
Dim pathToZipArchive As String = "Documents\Example.zip"
Dim pathToExtract As String = "Documents\!Extracted"
Using archive As ZipArchive = ZipArchive.Read(pathToZipArchive)
For Each item As ZipItem In archive
item.Extract(pathToExtract)
Next item
End Using
End Sub
#End Region ' #unziparchive
#Region "#unziparchiveconflict"
Public Sub UnzipArchiveConflict()
Dim pathToZipArchive As String = "Documents\Example.zip"
Dim pathToExtract As String = "Documents\!Extracted"
Using archive As ZipArchive = ZipArchive.Read(pathToZipArchive)
archive.OptionsBehavior.AllowFileOverwrite = AllowFileOverwriteMode.Custom
AddHandler archive.AllowFileOverwrite, AddressOf archive_AllowFileOverwrite
For Each item As ZipItem In archive
item.Extract(pathToExtract)
Next item
End Using
End Sub
Private Sub archive_AllowFileOverwrite(ByVal sender As Object, ByVal e As AllowFileOverwriteEventArgs)
Dim fi As New FileInfo(e.TargetFilePath)
If e.ZipItem.LastWriteTime < fi.LastWriteTime Then
e.Cancel = True
End If
End Sub
#End Region ' #unziparchiveconflict
#Region "#archivetext"
Public Sub ArchiveText()
Using archive As New ZipArchive()
archive.AddText("Text_DE.txt", "Komprimieren großer Dateien mühelos")
archive.Save("Documents\ArchiveText.zip")
End Using
End Sub
#End Region ' #archivetext
#Region "#addfiletoarchive"
Public Sub AddFileToArchive()
Dim stream As New MemoryStream()
Dim sourcefiles() As String = Me.sourceFiles
Dim pathToZipArchive As String = "Documents\Example.zip"
Using fs As FileStream = File.Open(pathToZipArchive, FileMode.Open)
fs.CopyTo(stream)
fs.Close()
End Using
stream.Seek(0, SeekOrigin.Begin)
Using archive As ZipArchive = ZipArchive.Read(stream, System.Text.Encoding.Default, False)
For Each sfile As String In sourcefiles
archive.AddFile(sfile, "/")
Next sfile
archive.Save(pathToZipArchive)
End Using
End Sub
#End Region ' #addfiletoarchive
#Region "#Invoke_Methods"
Public Function GetMethods() As MethodInfo()
Return Me.GetType().GetMethods(BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
End Function
Public Sub InvokeMethod(ByVal methodName As String, ByVal args As List(Of Object))
Me.GetType().GetMethod(methodName).Invoke(Me, args.ToArray())
End Sub
#End Region '#Invoke_Methods
End Class
End Namespace