/
ZipFileManager.cs
356 lines (275 loc) · 9.97 KB
/
ZipFileManager.cs
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Collections.Generic;
using System.Text;
namespace PDX.BTS.DataMaintenance.MaintTools
{
public class ZipFileManager : System.IDisposable
{
/// <summary>
/// A public event for sending process messages back to the calling application.
/// </summary>
public event SendProcessMessage ProcessMessage;
/// <summary>
/// A public event for sending error messages back to the calling application.
/// </summary>
public event SendErrorMessage ErrorMessage;
/// <summary>
/// Class Constructor.
/// </summary>
public ZipFileManager()
{
try
{
// Instantiate the Component that will hold the Managed Resources used by this method.
_component = new System.ComponentModel.Component();
}
catch
{
// Exit this method.
return;
}
// Exit the constructor method.
return;
} // ZipFileManager()
public bool ZipDirectory(string SourceDirectory, string ZipFilePath, bool OverWrite = false)
{
try
{
// Make sure the specified Source Directory Exists.
if (!System.IO.Directory.Exists(SourceDirectory))
{
// Let the user know that the directory does not exist.
if (ErrorMessage != null)
{
ErrorMessage("The specified source directory - " + SourceDirectory + " - does not exist. Aborting the MaintTools.ZipFileManager.ZipDirectory() Method!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
// Make sure the specified output Directory Exists.
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(ZipFilePath)))
{
// Attempt to create the directory.
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(ZipFilePath));
}
// Determine if the specified Output File already exists.
string outputFilePath = ZipFilePath;
if (System.IO.File.Exists(ZipFilePath))
{
// If the user wants the old file overwritten, delete it.
if (OverWrite)
{
System.IO.File.Delete(ZipFilePath);
}
else
{
// Try to find a file name to use.
string outputBasePath = outputFilePath.Substring(0, (outputFilePath.Length - 4));
string extension = outputFilePath.Substring((outputFilePath.Length - 4));
int i = 0;
outputFilePath = "";
while ((i < 100) && (outputFilePath.Length == 0))
{
if (!System.IO.File.Exists(outputBasePath + "(" + i.ToString() + ")" + extension))
{
outputFilePath = outputBasePath + "(" + i.ToString() + ")" + extension;
}
// Increment the counter.
i++;
}
}
}
// Retrieve the Directory Name to be included in the Zip File.
string directoryName = SourceDirectory;
while (directoryName.IndexOf(@"\") != -1)
{
// Drop the Left Most character from the Directory Name.
directoryName = directoryName.Substring(1);
}
// Attempt to zip the files in the directory.
using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile())
{
zipFile.AddDirectory(SourceDirectory, directoryName);
zipFile.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
zipFile.Save(ZipFilePath);
}
}
catch (System.Exception caught)
{
// Let the User know that this method failed.
if (ErrorMessage != null)
{
ErrorMessage("The MaintTools.ZipFileManager.ZipDirectory() Method failed with error message: " + caught.Message);
}
// Return FALSE to the calling method to indicate that this process failed.
return false;
}
// If the process made it to here, it was successful so return TRUE to the calling method.
return true;
}
public bool? ExtractAllFilesFromZipPackage(string ZipFilePath, string OutputDirectory)
{
try
{
// Extract the Files from the Zip Package.
using (Ionic.Zip.ZipFile inputZipFile = Ionic.Zip.ZipFile.Read(ZipFilePath))
{
foreach (Ionic.Zip.ZipEntry currentEntry in inputZipFile)
{
currentEntry.Extract(OutputDirectory);
}
}
}
catch(System.Exception caught)
{
// Let the User know that this method failed.
if (ErrorMessage != null)
{
ProcessMessage("The MaintTools.ZipFileManager.ExtractAllFilesFromZipPackage() Method failed with error message: " + caught.Message);
}
// Return NULL to the calling method to indicate that this process failed.
return null;
}
// If the process made it to here, it was successful so return TRUE to the calling method.
return true;
} // ExtractAllFilesFromZipPackage()
public bool? ExtractAllFilesFromZipPackageOverwrite(string ZipFilePath, string OutputDirectory)
{
try
{
// Extract the Files from the Zip Package.
using (Ionic.Zip.ZipFile inputZipFile = Ionic.Zip.ZipFile.Read(ZipFilePath))
{
foreach (Ionic.Zip.ZipEntry currentEntry in inputZipFile)
{
if (System.IO.File.Exists(System.IO.Path.Combine(OutputDirectory, currentEntry.FileName)))
{
System.IO.File.Delete(System.IO.Path.Combine(OutputDirectory, currentEntry.FileName));
}
currentEntry.Extract(OutputDirectory);
}
}
}
catch (System.Exception caught)
{
// Let the User know that this method failed.
if (ErrorMessage != null)
{
ProcessMessage("The MaintTools.ZipFileManager.ExtractAllFilesFromZipPackageOverwrite() Method failed with error message: " + caught.Message);
}
// Return NULL to the calling method to indicate that this process failed.
return null;
}
// If the process made it to here, it was successful so return TRUE to the calling method.
return true;
} // ExtractAllFilesFromZipPackageOverwrite()
public System.Collections.Specialized.StringCollection RetrieveListOfFilesInZipFile(string ZipFilePath)
{
System.Collections.Specialized.StringCollection fileList = null;
try
{
// Instantiate the List that will be used to store the file names so that they can be sent to the calling method.
fileList = new System.Collections.Specialized.StringCollection();
// Retrieve the List the Files from the Zip Package.
using (Ionic.Zip.ZipFile inputZipFile = Ionic.Zip.ZipFile.Read(ZipFilePath))
{
foreach (Ionic.Zip.ZipEntry currentEntry in inputZipFile)
{
fileList.Add(currentEntry.FileName);
}
}
// Return the list of file names to the calling method.
return fileList;
}
catch (System.Exception caught)
{
// Let the User know that this method failed.
if (ErrorMessage != null)
{
ProcessMessage("The MaintTools.ZipFileManager.RetrieveListOfFilesInZipFile() Method failed with error message: " + caught.Message);
}
// Return NULL Pointer to the calling method to indicate that this process failed.
return null;
}
finally
{
// If the File List was instantiated, close it.
if (fileList != null)
{
fileList = null;
}
}
} // RetrieveListOfFilesInZipFile()
#region Class Cleanup
/// <summary>
/// Public Dispose Method called by Calling Application to clean up after this method.
/// </summary>
public void Dispose()
{
try
{
// Call the method to do the cleanup. Indicate that the user called the Dispose Method.
Dispose(true);
// Tell the Garbace Collector to not go through cleanup since this method was called.
GC.SuppressFinalize(this);
}
catch
{
// Exit this method.
return;
}
} // Dispose()
/// <summary>
/// Method to do the work of cleaning up.
/// </summary>
/// <param name="Disposing">
/// Indicator of wether the Dispose method was called by the calling application or if the
/// pointer to this method was just set to NULL.
/// </param>
protected void Dispose(System.Boolean Disposing)
{
try
{
// If the Class has not already been Disposed (cleaned up after) clean up.
if (!_isDisposed)
{
// If the User called the Dispose Method, clean up managed resources.
if (Disposing)
{
// Dispose of Managed Resources.
_component.Dispose();
}
// Clean up the Unmanaged resources.
}
// Set the Disposed Indicator Variable to TRUE.
_isDisposed = true;
}
catch
{
// Exit this method.
return;
}
} // Dispose()
/// <summary>
/// Class Destructor
/// </summary>
~ZipFileManager()
{
try
{
// Call the Dispose Method to clean up any objects that were instantiated by this method.
Dispose(false);
}
catch
{
// Exit this method.
return;
}
} // ~FeatureClassUtilities()
#endregion Class Cleanup
// Indicator of whether or not the Class has been Disposed of by the calling application.
private System.Boolean _isDisposed = false;
// A Component Object that will be used to hold all Managed Resources for disposal.
private System.ComponentModel.Component _component = null;
} // CLASS: ZipFileManager
} // NAMESPACE: PDX.BTS.DataMaintenance.MaintTools