forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVB.htm
More file actions
319 lines (259 loc) · 9.51 KB
/
Copy pathVB.htm
File metadata and controls
319 lines (259 loc) · 9.51 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<html>
<head>
<title>VB.NET</title>
<style>
p,body,a,tr,td
{ font-family: Verdana, Arial, Helvetica; font-size: 10pt }
h1,h2,h3,h4,h5,h6
{ font-family: Verdana, Arial, Helvetica; font-weight: normal; font-style: normal; }
h1 { font-size: 20pt }
h2 { font-size: 18pt; font-weight:bold; color: navy }
h3 { font-size: 16pt; font-weight:bold; color: #483d8b }
h4 { font-size: 14pt; font-weight:bold; color:#C71585; margin-bottom:2px; }
</style>
</head>
<!-- @SortOrder 30 -->
<body>
<h1>DotNetZip - VB.NET Examples</h1>
<p>Here are a bunch of examples in VB.NET showing how to use the library.
</p>
<p>There are also a few complete, working example applications shipped in the source code distribution. </p>
<hr/>
<p>Add items to a zip file: </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create a Zip">
Try
Using zip As ZipFile = New ZipFile
zip.AddFile("c:\photos\personal\7440-N49th.png", "")
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "")
zip.AddFile("ReadMe.txt")
zip.Save("MyZipFile.zip")
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString)
End Try
</pre>
<hr/>
<p>Extract items from a zip file: </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Extract entries from a Zip">
Try
Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
Dim e As ZipEntry
For Each e In zip
e.Extract
Next
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString)
End Try
</pre>
<hr/>
<p>Extract all entries, and set the StatusMessageTextWriter so that verbose messages are generated:</p>
<pre lang="VB" numberLines="true" outlining="true"
title="ExtractAll, and use the status TextWriter">
Using zip As ZipFile = ZipFile.Read(FilePath)
zip.StatusMessageTextWriter= System.Console.Out
'Status Messages will be sent to the console during extraction
zip.ExtractAll()
End Using
</pre>
<hr/>
<p>Add a few files to a zip file, specifying different passwords for different items: </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create a Zip, entries get passwords">
Try
Using zip As New ZipFile
'the first entry is not protected by a password
zip.AddFile("c:\datafiles\ReadMe.txt", "")
zip.Password = "123456!"
zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
zip.Password= "!Secret1";
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "files\documents")
zip.Save("Secret.zip")
End Using
Catch ex1 As System.Exception
System.Console.Error.WriteLine("exception: {0}", ex1)
End Try
</pre>
<hr/>
<p>Add a few files to a zip file, using WinZip-compatible AES encryption on the entries: </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create a Zip, using AES encryption for the entries">
Try
Using zip As New ZipFile
zip.Password = "The.Silvertones.Box.Set!"
zip.Encryption = EncryptionAlgorithm.WinZipAes256
zip.AddFile("c:\datafiles\RawData-2008-12-20.csv", "")
zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
zip.AddFile("c:\Desktop\2005_Annual_Report.pdf", "files\documents")
zip.Save("AES-Encrypted-Secret.zip")
End Using
Catch ex1 As System.Exception
System.Console.Error.WriteLine("exception: {0}", ex1)
End Try
</pre>
<hr/>
<p>Extract entries using a password:</p>
<pre lang="VB" numberLines="true" outlining="true"
title="Extract entries using a password">
Using zip As new ZipFile(FilePath)
Dim e As ZipEntry
For Each e In zip
If (e.UsesEncryption)
e.ExtractWithPassword("Secret!")
Else
e.Extract
End If
Next
End Using
</pre>
<hr/>
<p>This example creates a zip using ZIP64 extensions. ZIP64 allows you to exceed 4gb in a zip, or 65535 entries in a zip. </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create a Zip that uses ZIP64 extensions">
Try
Using zip As ZipFile = New ZipFile
zip.UseZip64WhenSaving = Zip64Option.AsNecessary
zip.AddFile("c:\datafiles\RawData-2009-02-12.csv", "")
zip.AddFile("ReadMe.txt")
zip.Save(String.Format("backup-{0}.zip", DateTime.Now.ToString("yyyyMMMdd")))
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString)
End Try
</pre>
<hr/>
<p>Create a zip file, add a file, and also add an entry from a
string. When the zip is unzipped, the content from the string will be
inserted into the file "Readme.txt". </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create 3">
Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive."
Using zip1 As ZipFile = New ZipFile
zip1.AddEntry("Readme.txt", "This is the readme content...")
zip1.AddFile("MyDocuments\Resume.doc", "files")
zip1.Comment = ("This zip file was created at " & DateTime.Now.ToString("G"))
zip1.Save("Content.zip")
End Using
</pre>
<hr/>
<p>Create a zip file, and add an entry taking content from a stream, like a MemoryStream or a FileStream.
</p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create 4">
Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive."
Using zip1 As ZipFile = New ZipFile
zip1.AddEntry("Readme.txt", stream)
zip1.AddFile("MyDocuments\Resume.doc", "files")
zip1.Comment = ("This zip file was created at " & DateTime.Now.ToString("G"))
zip1.Save("Content.zip")
End Using
</pre>
<hr/>
<p>Read in a zip file, remove a few entries, save the file:</p>
<pre lang="VB" numberLines="true" outlining="true"
title="Modify a Zip">
Dim sw As New System.IO.StringWriter
Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip", sw)
Dim Threshold As New DateTime(2007, 7, 4)
' We cannot remove the entry from the list, within the context of
' an enumeration of said list.
' So we add the doomed entry to a list to be removed later.
' pass 1: mark the entries for removal
Dim MarkedEntries As New System.Collections.Generic.List(Of ZipEntry)
Dim e As ZipEntry
For Each e In zip
If (e.LastModified < Threshold) Then
MarkedEntries.Add(e)
End If
Next
' pass 2: actually remove the entry.
Dim zombie As ZipEntry
For Each zombie In MarkedEntries
zip.RemoveEntry(zombie)
Next
zip.Comment = "This archive has been updated."
zip.Save
End Using
</pre>
<hr/>
<p>Add a bunch of items, whether files or directories:</p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create a Zip, adding items">
Dim itempaths As String() = _
New String() { "c:\temp\Readme.txt", _
"MyProposal.docx", _
"SupportingFiles", _
"images\Image1.jpg" }
Try
Using zip As New ZipFile(ZipToCreate, Console.Out)
Dim i As Integer
For i = 1 To itempaths.Length - 1
' will add Files or Dirs, recursing and flattening subdirectories.
zip.AddItem(itempaths(i), "flat")
Next i
zip.Save
End Using
Catch ex1 As Exception
Console.Error.WriteLine("exception: {0}", ex1.ToString())
End Try
</pre>
<hr/>
<p>Create a self-extracting archive:</p>
<pre lang="VB" numberLines="true" outlining="true"
title="Create a self-extractor">
Dim DirectoryPath As String = "c:\Documents\Project7"
Using zip As New ZipFile()
zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath))
zip.Comment = "This will be embedded into a self-extracting console-based exe"
zip.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.ConsoleApplication)
End Using
</pre>
<hr/>
<p>Update some entries in a Zip file:</p>
<pre lang="VB" numberLines="true" outlining="true"
title="Update some entries in a Zip">
Using zip1 As New ZipFile
' the UpdateFile method works even if the entry does not yet exist.
' Really it should be called "AddOrUpdateFile"
zip1.UpdateFile("MyDocuments\Readme.txt", "")
zip1.UpdateFile("CustomerList.csv", "")
zip1.Comment = "This zip archive has been created."
zip1.Save("Content.zip")
End Using
Using zip2 As ZipFile = ZipFile.Read("Content.zip")
zip2.UpdateFile("Updates\Readme.txt", "")
zip2.Comment = "This zip archive has been updated: the Readme has been changed."
zip2.Save
End Using
</pre>
<hr/>
<p>Produce a zip file that contains embedded zip files. </p>
<pre lang="VB" numberLines="true" outlining="true"
title="Zip containing a Zip">
Public Sub Run()
Using s1 As Stream = ZipIntoMemory("c:\temp\dir1")
Using s2 As Stream = ZipIntoMemory("c:\temp\dir2")
Using zip1 as New ZipFile
zip1.AddEntry("test1.zip", s1)
zip1.AddEntry("test2.zip", s2)
' save to a file. Could also save to a stream here
zip1.Save("Tescher.zip")
End Using
End Using
End Using
End Sub
Public Function ZipIntoMemory(ByVal path As String) As Stream
Dim ms As New MemoryStream
Using zip1 as New ZipFile
zip1.AddDirectory(path, "Result")
zip1.Save(ms)
End Using
' move the stream position to the beginning
ms.Seek(0,SeekOrigin.Begin)
Return ms
End Function
</pre>
</body>
</html>