Skip to content

Commit b3f2991

Browse files
committed
Added progress handling to FastZip.cs
Fixed up some samples, including extending FastZip sample to show progress handling.
1 parent 01396ec commit b3f2991

12 files changed

+314
-80
lines changed

ICSharpCode.SharpZLib.sln

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

22
Microsoft Visual Studio Solution File, Format Version 9.00
33
# Visual Studio 2005
4+
# SharpDevelop 2.2.1.2660
45
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpZipLibTests", "tests\SharpZipLibTests.csproj", "{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}"
56
EndProject
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.SharpZLib", "src\ICSharpCode.SharpZLib.csproj", "{0AAD2BF5-F6C9-4180-9C50-9200726052DD}"
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.SharpZLib", "src\ICSharpCode.SharpZLib.csproj", "{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}"
78
EndProject
89
Global
910
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,10 +16,10 @@ Global
1516
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Debug|Any CPU.Build.0 = Debug|Any CPU
1617
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Release|Any CPU.ActiveCfg = Release|Any CPU
1718
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Release|Any CPU.Build.0 = Release|Any CPU
18-
{0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19-
{0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
20-
{0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
21-
{0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Release|Any CPU.Build.0 = Release|Any CPU
2223
EndGlobalSection
2324
GlobalSection(SolutionProperties) = preSolution
2425
HideSolutionNode = FALSE

doc/Changes.txt

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
Changes since 0.85.2
2+
3+
+ Extended unit tests in many areas.
4+
+ Fixed GZip reading of streams larger then 2^32.
5+
+ Windows date time now handled in ExtraData.
6+
+ Extensions to extra data handling.
7+
+ Encrypting stored entries no longer consumes arbitrary amounts of memory.
8+
+ DeflatorOutputStream now uses CryptoTransform
9+
+ ZipInputStream detection of invalid sizes improved.
10+
+ ZipHelperStream reading end of stream values now correctly handled
11+
+ ZipHelperStream now supports reading/writing data descriptors.
12+
+ ZipFile testing data now handles data descriptors.
13+
+ ZipFile adding entries now correctly handles data descriptors.
14+
+ ZipFile now defaults to dynamic use of Zip64.
15+
+ ZipEntryFactory class made public and bugs fixed.
16+
+ ZipEntry DateTime handling cleaned up - Zero is treated as fixed low value and high values checked and handled.
17+
+ Added progress event handling to FastZip.
18+
119
Changes since 0.85.1
220

321
+ Minor tweaks for CF, ZipEntryFactory and ZipFile.

mkDistribution.bat

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@echo "mkDistribution v1.1"
1+
@echo "mkDistribution v1.2"
22

33
if exist current (
44
rmdir /s /q current
@@ -19,17 +19,17 @@ mkdir current\net-20
1919
nant -t:net-2.0 -D:build.output.dir=current\net-20 -buildfile:sharpZLib.build build
2020

2121
@echo todo generate documentation and the rest of the distribution image.
22-
samples\cs\bin\sz -rc current\SharpZipLib.zip current\*.dll
2322

24-
mkdir current\source
25-
copy doc\readme.rtf current\source
26-
copy doc\Changes.txt current\source
27-
copy doc\Copying.txt current\source
28-
rem copy doc\SharpZipLib.chm current\source
29-
copy *.bat current\source
30-
copy *.build current\source
23+
mkdir current\doc
24+
copy doc\readme.rtf current\doc
25+
copy doc\Changes.txt current\doc
26+
copy doc\Copying.txt current\doc
27+
copy SharpZipLib.chm current\doc
28+
copy installGAC.bat current
29+
copy uninstallGAC.bat current
3130

31+
samples\cs\bin\sz -rc current\SharpZipLib.zip current\*.dll
3232

33-
REM Compress source to SharpZipLib_SourceSamples.zip
34-
REM Build CHM file
35-
REM Build Bin Zip files
33+
@echo TODO Compress source to SharpZipLib_SourceSamples.zip
34+
@echo TODO Build CHM file
35+
@echo TODO Build Bin Zip files

samples/cs/FastZip/Main.cs

+42-14
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,17 @@ void ListDir(object Sender, DirectoryEventArgs e)
9393
void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
9494
{
9595
FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
96-
scanner.ProcessDirectory += new ProcessDirectoryDelegate(ListDir);
97-
scanner.ProcessFile += new ProcessFileDelegate(ListFile);
96+
scanner.ProcessDirectory += new ProcessDirectoryHandler(ListDir);
97+
scanner.ProcessFile += new ProcessFileHandler(ListFile);
9898
scanner.Scan(directory, recurse);
9999
}
100-
100+
101+
void ShowProgress(object sender, ProgressEventArgs e)
102+
{
103+
// Very ugly but this is a sample!
104+
Console.WriteLine("{0}%", e.PercentComplete);
105+
}
106+
101107
void ProcessFile(object sender, ScanEventArgs e)
102108
{
103109
Console.WriteLine(e.Name);
@@ -127,6 +133,8 @@ void Run(string[] args)
127133
bool verbose = false;
128134
bool restoreDates = false;
129135
bool restoreAttributes = false;
136+
bool progress = false;
137+
TimeSpan interval = TimeSpan.FromSeconds(1);
130138

131139
bool createEmptyDirs = false;
132140
FastZip.Overwrite overwrite = FastZip.Overwrite.Always;
@@ -190,6 +198,12 @@ void Run(string[] args)
190198
break;
191199

192200

201+
case "p":
202+
case "progress":
203+
progress = true;
204+
verbose = true;
205+
break;
206+
193207
case "r":
194208
case "recurse":
195209
recurse = true;
@@ -199,7 +213,13 @@ void Run(string[] args)
199213
case "verbose":
200214
verbose = true;
201215
break;
202-
216+
217+
case "i":
218+
if (optArg.Length > 0) {
219+
interval = TimeSpan.FromSeconds(int.Parse(optArg));
220+
}
221+
break;
222+
203223
case "file":
204224
if ( NameFilter.IsValidFilterExpression(optArg) ) {
205225
fileFilter = optArg;
@@ -274,21 +294,27 @@ void Run(string[] args)
274294

275295
if ( verbose ) {
276296
events = new FastZipEvents();
277-
events.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
278-
events.ProcessFile = new ProcessFileDelegate(ProcessFile);
297+
events.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
298+
events.ProcessFile = new ProcessFileHandler(ProcessFile);
299+
300+
if (progress)
301+
{
302+
events.Progress = new ProgressHandler(ShowProgress);
303+
events.ProgressInterval = interval;
304+
}
279305
}
280306

281-
FastZip sz = new FastZip(events);
282-
sz.CreateEmptyDirectories = createEmptyDirs;
283-
sz.RestoreAttributesOnExtract = restoreAttributes;
284-
sz.RestoreDateTimeOnExtract = restoreDates;
307+
FastZip fastZip = new FastZip(events);
308+
fastZip.CreateEmptyDirectories = createEmptyDirs;
309+
fastZip.RestoreAttributesOnExtract = restoreAttributes;
310+
fastZip.RestoreDateTimeOnExtract = restoreDates;
285311

286312
switch ( op ) {
287313
case Operation.Create:
288314
if ( argCount == 2 ) {
289315
Console.WriteLine("Creating Zip");
290316

291-
sz.CreateZip(arg1, arg2, recurse, fileFilter, dirFilter);
317+
fastZip.CreateZip(arg1, arg2, recurse, fileFilter, dirFilter);
292318
}
293319
else
294320
Console.WriteLine("Invalid arguments");
@@ -297,7 +323,7 @@ void Run(string[] args)
297323
case Operation.Extract:
298324
if ( argCount == 2 ) {
299325
Console.WriteLine("Extracting Zip");
300-
sz.ExtractZip(arg1, arg2, overwrite, confirmOverwrite, fileFilter, dirFilter, recurse);
326+
fastZip.ExtractZip(arg1, arg2, overwrite, confirmOverwrite, fileFilter, dirFilter, recurse);
301327
}
302328
else
303329
Console.WriteLine("zipfile and target directory not specified");
@@ -317,17 +343,19 @@ void Run(string[] args)
317343

318344
case Operation.Unknown:
319345
Console.WriteLine(
320-
"FastZip v0.4\n"
346+
"FastZip v0.5\n"
321347
+ " Usage: FastZip {options} operation args\n"
322348
+ "Operation Options: (only one permitted)\n"
323349
+ " -x zipfile targetdir : Extract files from Zip\n"
324350
+ " -c zipfile sourcedir : Create zip file\n"
325351
+ " -l zipfile|dir : List elements\n"
326352
+ "\n"
327353
+ "Behavioural options:\n"
328-
+ " -file={fileFilter}\n"
329354
+ " -dir={dirFilter}\n"
355+
+ " -file={fileFilter}\n"
330356
+ " -e Process empty directories\n"
357+
+ " -i Progress interval in seconds\n"
358+
+ " -p Show file progress\n"
331359
+ " -r Recurse directories\n"
332360
+ " -v Verbose output\n"
333361
+ " -oa Restore file attributes on extract\n"

samples/cs/zf/zf.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,8 @@ void Create(ArrayList fileSpecs)
658658
zf.NameTransform = new ZipNameTransform(path);
659659

660660
FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
661-
scanner.ProcessFile = new ProcessFileDelegate(ProcessFile);
662-
scanner.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
661+
scanner.ProcessFile = new ProcessFileHandler(ProcessFile);
662+
scanner.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
663663
scanner.Scan(path, recursive_);
664664
}
665665

@@ -1047,8 +1047,8 @@ void Add(ArrayList fileSpecs)
10471047
zipFile.NameTransform = new ZipNameTransform(path);
10481048

10491049
FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
1050-
scanner.ProcessFile = new ProcessFileDelegate(ProcessFile);
1051-
scanner.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
1050+
scanner.ProcessFile = new ProcessFileHandler(ProcessFile);
1051+
scanner.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
10521052
scanner.Scan(path, recursive_);
10531053
}
10541054
zipFile.CommitUpdate();

src/Core/FileSystemScanner.cs

+86-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,70 @@ public bool ContinueRunning
7979
#endregion
8080
}
8181

82+
/// <summary>
83+
/// Event arguments during processing of a single file or directory.
84+
/// </summary>
85+
public class ProgressEventArgs : EventArgs
86+
{
87+
#region Constructors
88+
/// <summary>
89+
/// Initialise a new instance of <see cref="ScanEventArgs"/>
90+
/// </summary>
91+
/// <param name="name"></param>
92+
/// <param name="processed">The number of bytes processed so far</param>
93+
/// <param name="target">The total number of bytes to process, 0 if not known</param>
94+
public ProgressEventArgs(string name, long processed, long target)
95+
{
96+
name_ = name;
97+
processed_ = processed;
98+
target_ = target;
99+
}
100+
#endregion
101+
102+
/// <summary>
103+
/// The name for this event if known.
104+
/// </summary>
105+
public string Name
106+
{
107+
get { return name_; }
108+
}
109+
110+
/// <summary>
111+
/// Get set a value indicating wether scanning should continue or not.
112+
/// </summary>
113+
public bool ContinueRunning
114+
{
115+
get { return continueRunning_; }
116+
set { continueRunning_ = value; }
117+
}
118+
119+
/// <summary>
120+
/// Get a percentage representing the how complete the operation is.
121+
/// </summary>
122+
/// <value>0 to 100 percent; 0 if target is not known.</value>
123+
public float PercentComplete
124+
{
125+
get
126+
{
127+
if (target_ <= 0)
128+
{
129+
return 0;
130+
}
131+
else
132+
{
133+
return ((float)processed_ / (float)target_) * 100.0f;
134+
}
135+
}
136+
}
137+
138+
#region Instance Fields
139+
string name_;
140+
long processed_;
141+
long target_;
142+
bool continueRunning_ = true;
143+
#endregion
144+
}
145+
82146
/// <summary>
83147
/// Event arguments for directories.
84148
/// </summary>
@@ -172,21 +236,36 @@ public bool ContinueRunning
172236
/// <summary>
173237
/// Delegate invoked before starting to process a file.
174238
/// </summary>
239+
/// <param name="sender">The source of the event</param>
240+
/// <param name="e">The event arguments.</param>
175241
public delegate void ProcessFileHandler(object sender, ScanEventArgs e);
176242

243+
/// <summary>
244+
/// Delegate invoked during processing of a file or directory
245+
/// </summary>
246+
/// <param name="sender">The source of the event</param>
247+
/// <param name="e">The event arguments.</param>
248+
public delegate void ProgressHandler(object sender, ProgressEventArgs e);
249+
177250
/// <summary>
178251
/// Delegate invoked when a file has been completely processed.
179252
/// </summary>
253+
/// <param name="sender">The source of the event</param>
254+
/// <param name="e">The event arguments.</param>
180255
public delegate void CompletedFileHandler(object sender, ScanEventArgs e);
181256

182257
/// <summary>
183258
/// Delegate invoked when a directory failure is detected.
184259
/// </summary>
260+
/// <param name="sender">The source of the event</param>
261+
/// <param name="e">The event arguments.</param>
185262
public delegate void DirectoryFailureHandler(object sender, ScanFailureEventArgs e);
186263

187264
/// <summary>
188265
/// Delegate invoked when a file failure is detected.
189266
/// </summary>
267+
/// <param name="sender">The source of the event</param>
268+
/// <param name="e">The event arguments.</param>
190269
public delegate void FileFailureHandler(object sender, ScanFailureEventArgs e);
191270
#endregion
192271

@@ -269,7 +348,7 @@ public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter)
269348
/// </summary>
270349
/// <param name="directory">The directory name.</param>
271350
/// <param name="e">The exception detected.</param>
272-
public void OnDirectoryFailure(string directory, Exception e)
351+
void OnDirectoryFailure(string directory, Exception e)
273352
{
274353
if ( DirectoryFailure == null ) {
275354
alive_ = false;
@@ -285,7 +364,7 @@ public void OnDirectoryFailure(string directory, Exception e)
285364
/// </summary>
286365
/// <param name="file">The file name.</param>
287366
/// <param name="e">The exception detected.</param>
288-
public void OnFileFailure(string file, Exception e)
367+
void OnFileFailure(string file, Exception e)
289368
{
290369
if ( FileFailure == null ) {
291370
alive_ = false;
@@ -309,6 +388,10 @@ void OnProcessFile(string file)
309388
}
310389
}
311390

391+
/// <summary>
392+
/// Raise the complete file event
393+
/// </summary>
394+
/// <param name="file">The file name</param>
312395
void OnCompleteFile(string file)
313396
{
314397
if (CompletedFile != null)
@@ -370,8 +453,7 @@ void ScanDir(string directory, bool recurse)
370453
}
371454
}
372455
}
373-
catch (Exception e)
374-
{
456+
catch (Exception e) {
375457
OnFileFailure(fileName, e);
376458
}
377459
}

0 commit comments

Comments
 (0)