温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:X3BLOG 单用户1.0 build80707(ACCESS)源代码
当前文件路径:x3blogAccessBuild80707/SharpZipLib/Zip/FastZip.cs

1// SimpleZip.cs 2
// 3
// Copyright 2005 John Reilly 4
// 5
// This program is free software; you can redistribute it and/or 6
// modify it under the terms of the GNU General Public License 7
// as published by the Free Software Foundation; either version 2 8
// of the License, or (at your option) any later version. 9
// 10
// This program is distributed in the hope that it will be useful, 11
// but WITHOUT ANY WARRANTY; without even the implied warranty of 12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13
// GNU General Public License for more details. 14
// 15
// You should have received a copy of the GNU General Public License 16
// along with this program; if not, write to the Free Software 17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18
// 19
// Linking this library statically or dynamically with other modules is 20
// making a combined work based on this library. Thus, the terms and 21
// conditions of the GNU General Public License cover the whole 22
// combination. 23
// 24
// As a special exception, the copyright holders of this library give you 25
// permission to link this library with independent modules to produce an 26
// executable, regardless of the license terms of these independent 27
// modules, and to copy and distribute the resulting executable under 28
// terms of your choice, provided that you also meet, for each linked 29
// independent module, the terms and conditions of the license of that 30
// module. An independent module is a module which is not derived from 31
// or based on this library. If you modify this library, you may extend 32
// this exception to your version of the library, but you are not 33
// obligated to do so. If you do not wish to do so, delete this 34
// exception statement from your version. 35
36
37
using System; 38
using System.IO; 39
using ICSharpCode.SharpZipLib.Core; 40
41
namespace ICSharpCode.SharpZipLib.Zip 42
{ 43
/// <summary> 44
/// FastZipEvents supports all events applicable to <see cref="FastZip">FastZip</see> operations. 45
/// </summary> 46
public class FastZipEvents 47
{ 48
/// <summary> 49
/// Delegate to invoke when processing directories. 50
/// </summary> 51
public ProcessDirectoryDelegate ProcessDirectory; 52
53
/// <summary> 54
/// Delegate to invoke when processing files. 55
/// </summary> 56
public ProcessFileDelegate ProcessFile; 57
58
/// <summary> 59
/// Delegate to invoke when processing directory failures. 60
/// </summary> 61
public DirectoryFailureDelegate DirectoryFailure; 62
63
/// <summary> 64
/// Delegate to invoke when processing file failures. 65
/// </summary> 66
public FileFailureDelegate FileFailure; 67
68
/// <summary> 69
/// Raise the directory failure event. 70
/// </summary> 71
/// <param name="directory">The directory.</param> 72
/// <param name="e">The exception for this event.</param> 73
public void OnDirectoryFailure(string directory, Exception e) 74
{ 75
if ( DirectoryFailure != null ) { 76
ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e); 77
DirectoryFailure(this, args); 78
} 79
} 80
81
/// <summary> 82
/// Raises the file failure event. 83
/// </summary> 84
/// <param name="file">The file for this event.</param> 85
/// <param name="e">The exception for this event.</param> 86
public void OnFileFailure(string file, Exception e) 87
{ 88
if ( FileFailure != null ) { 89
ScanFailureEventArgs args = new ScanFailureEventArgs(file, e); 90
FileFailure(this, args); 91
} 92
} 93
94
/// <summary> 95
/// Raises the ProcessFileEvent. 96
/// </summary> 97
/// <param name="file">The file for this event.</param> 98
public void OnProcessFile(string file) 99
{ 100
if ( ProcessFile != null ) { 101
ScanEventArgs args = new ScanEventArgs(file); 102
ProcessFile(this, args); 103
} 104
} 105
106
/// <summary> 107
/// Raises the ProcessDirectoryEvent. 108
/// </summary> 109
/// <param name="directory">The directory for this event.</param> 110
/// <param name="hasMatchingFiles">Flag indicating if directory has matching files as determined by the current filter.</param> 111
public void OnProcessDirectory(string directory, bool hasMatchingFiles) 112
{ 113
if ( ProcessDirectory != null ) { 114
DirectoryEventArgs args = new DirectoryEventArgs(directory, hasMatchingFiles); 115
ProcessDirectory(this, args); 116
} 117
} 118
119
} 120
121
/// <summary> 122
/// FastZip provides facilities for creating and extracting zip files. 123
/// Only relative paths are supported. 124
/// </summary> 125
public class FastZip 126
{ 127
/// <summary> 128
/// Initialize a default instance of FastZip. 129
/// </summary> 130
public FastZip() 131
{ 132
this.events = null; 133
} 134
135
/// <summary> 136
/// Initialise a new instance of <see cref="FastZip"/> 137
/// </summary> 138
/// <param name="events"></param> 139
public FastZip(FastZipEvents events) 140
{ 141
this.events = events; 142
} 143
144
/// <summary> 145
/// Defines the desired handling when overwriting files. 146
/// </summary> 147
public enum Overwrite { 148
/// <summary> 149
/// Prompt the user to confirm overwriting 150
/// </summary> 151
Prompt, 152
/// <summary> 153
/// Never overwrite files. 154
/// </summary> 155
Never, 156
/// <summary> 157
/// Always overwrite files. 158
/// </summary> 159
Always 160
} 161
162
/// <summary> 163
/// Get/set a value indicating wether empty directories should be created. 164
/// </summary> 165
public bool CreateEmptyDirectories 166
{ 167
get { return createEmptyDirectories; } 168
set { createEmptyDirectories = value; } 169
} 170
171
/// <summary> 172
/// Delegate called when confirming overwriting of files. 173
/// </summary> 174
public delegate bool ConfirmOverwriteDelegate(string fileName); 175
176
/// <summary> 177
/// Create a zip file. 178
/// </summary> 179
/// <param name="zipFileName">The name of the zip file to create.</param> 180
/// <param name="sourceDirectory">The directory to source files from.</param> 181
/// <param name="recurse">True to recurse directories, false for no recursion.</param> 182
/// <param name="fileFilter">The file filter to apply.</param> 183
/// <param name="directoryFilter">The directory filter to apply.</param> 184
public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter) 185
{ 186
NameTransform = new ZipNameTransform(true, sourceDirectory); 187
this.sourceDirectory = sourceDirectory; 188
189
outputStream = new ZipOutputStream(File.Create(zipFileName)); 190
try { 191
FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter); 192
scanner.ProcessFile += new ProcessFileDelegate(ProcessFile); 193
if ( this.CreateEmptyDirectories ) { 194
scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory); 195
} 196
scanner.Scan(sourceDirectory, recurse); 197
} 198
finally { 199
outputStream.Close(); 200
} 201
} 202
203
/// <summary> 204
/// Create a zip file/archive. 205
/// </summary> 206
/// <param name="zipFileName">The name of the zip file to create.</param> 207
/// <param name="sourceDirectory">The directory to obtain files and directories from.</param> 208
/// <param name="recurse">True to recurse directories, false for no recursion.</param> 209
/// <param name="fileFilter">The file filter to apply.</param> 210
public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter) 211
{ 212
CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, null); 213
} 214
215
/// <summary> 216
/// Extract the contents of a zip file. 217
/// </summary> 218
/// <param name="zipFileName">The zip file to extract from.</param> 219
/// <param name="targetDirectory">The directory to save extracted information in.</param> 220
/// <param name="fileFilter">A filter to apply to files.</param> 221
public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter) 222
{ 223
ExtractZip(zipFileName, targetDirectory, Overwrite.Always, null, fileFilter, null); 224
} 225
226
/// <summary> 227
/// Exatract the contents of a zip file. 228
/// </summary> 229
/// <param name="zipFileName">The zip file to extract from.</param> 230
/// <param name="targetDirectory">The directory to save extracted information in.</param> 231
/// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> 232
/// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> 233
/// <param name="fileFilter">A filter to apply to files.</param> 234
/// <param name="directoryFilter">A filter to apply to directories.</param> 235
public void ExtractZip(string zipFileName, string targetDirectory, 236
Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, 237
string fileFilter, string directoryFilter) 238
{ 239
if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) { 240
throw new ArgumentNullException("confirmDelegate"); 241
} 242
this.overwrite = overwrite; 243
this.confirmDelegate = confirmDelegate; 244
this.targetDirectory = targetDirectory; 245
this.fileFilter = new NameFilter(fileFilter); 246
this.directoryFilter = new NameFilter(directoryFilter); 247
248
inputStream = new ZipInputStream(File.OpenRead(zipFileName)); 249
250
try { 251
252
if (password != null) { 253
inputStream.Password = password; 254
} 255
256
ZipEntry entry; 257
while ( (entry = inputStream.GetNextEntry()) != null ) { 258
if ( this.directoryFilter.IsMatch(Path.GetDirectoryName(entry.Name)) && this.fileFilter.IsMatch(entry.Name) ) { 259
ExtractEntry(entry); 260
} 261
} 262
} 263




