Permalink
Browse files

Added progress handling to FastZip.cs

Fixed up some samples, including extending FastZip sample to show progress handling.
  • Loading branch information...
1 parent 01396ec commit b3f2991498ebf1e42ec7157d4c555af09cf9ccb4 @jfreilly jfreilly committed Aug 24, 2007
View
@@ -1,9 +1,10 @@

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
+# SharpDevelop 2.2.1.2660
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpZipLibTests", "tests\SharpZipLibTests.csproj", "{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.SharpZLib", "src\ICSharpCode.SharpZLib.csproj", "{0AAD2BF5-F6C9-4180-9C50-9200726052DD}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.SharpZLib", "src\ICSharpCode.SharpZLib.csproj", "{0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,10 +16,10 @@ Global
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Release|Any CPU.Build.0 = Release|Any CPU
- {0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {0AAD2BF5-F6C9-4180-9C50-9200726052DD}.Release|Any CPU.Build.0 = Release|Any CPU
+ {0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {0E7413FF-EB9E-4714-ACF2-BE3A6A7B2FFD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
@@ -1,3 +1,21 @@
+Changes since 0.85.2
+
++ Extended unit tests in many areas.
++ Fixed GZip reading of streams larger then 2^32.
++ Windows date time now handled in ExtraData.
++ Extensions to extra data handling.
++ Encrypting stored entries no longer consumes arbitrary amounts of memory.
++ DeflatorOutputStream now uses CryptoTransform
++ ZipInputStream detection of invalid sizes improved.
++ ZipHelperStream reading end of stream values now correctly handled
++ ZipHelperStream now supports reading/writing data descriptors.
++ ZipFile testing data now handles data descriptors.
++ ZipFile adding entries now correctly handles data descriptors.
++ ZipFile now defaults to dynamic use of Zip64.
++ ZipEntryFactory class made public and bugs fixed.
++ ZipEntry DateTime handling cleaned up - Zero is treated as fixed low value and high values checked and handled.
++ Added progress event handling to FastZip.
+
Changes since 0.85.1
+ Minor tweaks for CF, ZipEntryFactory and ZipFile.
View
@@ -1,4 +1,4 @@
-@echo "mkDistribution v1.1"
+@echo "mkDistribution v1.2"
if exist current (
rmdir /s /q current
@@ -19,17 +19,17 @@ mkdir current\net-20
nant -t:net-2.0 -D:build.output.dir=current\net-20 -buildfile:sharpZLib.build build
@echo todo generate documentation and the rest of the distribution image.
-samples\cs\bin\sz -rc current\SharpZipLib.zip current\*.dll
-mkdir current\source
-copy doc\readme.rtf current\source
-copy doc\Changes.txt current\source
-copy doc\Copying.txt current\source
-rem copy doc\SharpZipLib.chm current\source
-copy *.bat current\source
-copy *.build current\source
+mkdir current\doc
+copy doc\readme.rtf current\doc
+copy doc\Changes.txt current\doc
+copy doc\Copying.txt current\doc
+copy SharpZipLib.chm current\doc
+copy installGAC.bat current
+copy uninstallGAC.bat current
+samples\cs\bin\sz -rc current\SharpZipLib.zip current\*.dll
-REM Compress source to SharpZipLib_SourceSamples.zip
-REM Build CHM file
-REM Build Bin Zip files
+@echo TODO Compress source to SharpZipLib_SourceSamples.zip
+@echo TODO Build CHM file
+@echo TODO Build Bin Zip files
View
@@ -93,11 +93,17 @@ void ListDir(object Sender, DirectoryEventArgs e)
void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
{
FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
- scanner.ProcessDirectory += new ProcessDirectoryDelegate(ListDir);
- scanner.ProcessFile += new ProcessFileDelegate(ListFile);
+ scanner.ProcessDirectory += new ProcessDirectoryHandler(ListDir);
+ scanner.ProcessFile += new ProcessFileHandler(ListFile);
scanner.Scan(directory, recurse);
}
-
+
+ void ShowProgress(object sender, ProgressEventArgs e)
+ {
+ // Very ugly but this is a sample!
+ Console.WriteLine("{0}%", e.PercentComplete);
+ }
+
void ProcessFile(object sender, ScanEventArgs e)
{
Console.WriteLine(e.Name);
@@ -127,6 +133,8 @@ void Run(string[] args)
bool verbose = false;
bool restoreDates = false;
bool restoreAttributes = false;
+ bool progress = false;
+ TimeSpan interval = TimeSpan.FromSeconds(1);
bool createEmptyDirs = false;
FastZip.Overwrite overwrite = FastZip.Overwrite.Always;
@@ -190,6 +198,12 @@ void Run(string[] args)
break;
+ case "p":
+ case "progress":
+ progress = true;
+ verbose = true;
+ break;
+
case "r":
case "recurse":
recurse = true;
@@ -199,7 +213,13 @@ void Run(string[] args)
case "verbose":
verbose = true;
break;
-
+
+ case "i":
+ if (optArg.Length > 0) {
+ interval = TimeSpan.FromSeconds(int.Parse(optArg));
+ }
+ break;
+
case "file":
if ( NameFilter.IsValidFilterExpression(optArg) ) {
fileFilter = optArg;
@@ -274,21 +294,27 @@ void Run(string[] args)
if ( verbose ) {
events = new FastZipEvents();
- events.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
- events.ProcessFile = new ProcessFileDelegate(ProcessFile);
+ events.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
+ events.ProcessFile = new ProcessFileHandler(ProcessFile);
+
+ if (progress)
+ {
+ events.Progress = new ProgressHandler(ShowProgress);
+ events.ProgressInterval = interval;
+ }
}
- FastZip sz = new FastZip(events);
- sz.CreateEmptyDirectories = createEmptyDirs;
- sz.RestoreAttributesOnExtract = restoreAttributes;
- sz.RestoreDateTimeOnExtract = restoreDates;
+ FastZip fastZip = new FastZip(events);
+ fastZip.CreateEmptyDirectories = createEmptyDirs;
+ fastZip.RestoreAttributesOnExtract = restoreAttributes;
+ fastZip.RestoreDateTimeOnExtract = restoreDates;
switch ( op ) {
case Operation.Create:
if ( argCount == 2 ) {
Console.WriteLine("Creating Zip");
- sz.CreateZip(arg1, arg2, recurse, fileFilter, dirFilter);
+ fastZip.CreateZip(arg1, arg2, recurse, fileFilter, dirFilter);
}
else
Console.WriteLine("Invalid arguments");
@@ -297,7 +323,7 @@ void Run(string[] args)
case Operation.Extract:
if ( argCount == 2 ) {
Console.WriteLine("Extracting Zip");
- sz.ExtractZip(arg1, arg2, overwrite, confirmOverwrite, fileFilter, dirFilter, recurse);
+ fastZip.ExtractZip(arg1, arg2, overwrite, confirmOverwrite, fileFilter, dirFilter, recurse);
}
else
Console.WriteLine("zipfile and target directory not specified");
@@ -317,17 +343,19 @@ void Run(string[] args)
case Operation.Unknown:
Console.WriteLine(
- "FastZip v0.4\n"
+ "FastZip v0.5\n"
+ " Usage: FastZip {options} operation args\n"
+ "Operation Options: (only one permitted)\n"
+ " -x zipfile targetdir : Extract files from Zip\n"
+ " -c zipfile sourcedir : Create zip file\n"
+ " -l zipfile|dir : List elements\n"
+ "\n"
+ "Behavioural options:\n"
- + " -file={fileFilter}\n"
+ " -dir={dirFilter}\n"
+ + " -file={fileFilter}\n"
+ " -e Process empty directories\n"
+ + " -i Progress interval in seconds\n"
+ + " -p Show file progress\n"
+ " -r Recurse directories\n"
+ " -v Verbose output\n"
+ " -oa Restore file attributes on extract\n"
View
@@ -658,8 +658,8 @@ void Create(ArrayList fileSpecs)
zf.NameTransform = new ZipNameTransform(path);
FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
- scanner.ProcessFile = new ProcessFileDelegate(ProcessFile);
- scanner.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
+ scanner.ProcessFile = new ProcessFileHandler(ProcessFile);
+ scanner.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
scanner.Scan(path, recursive_);
}
@@ -1047,8 +1047,8 @@ void Add(ArrayList fileSpecs)
zipFile.NameTransform = new ZipNameTransform(path);
FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
- scanner.ProcessFile = new ProcessFileDelegate(ProcessFile);
- scanner.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
+ scanner.ProcessFile = new ProcessFileHandler(ProcessFile);
+ scanner.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
scanner.Scan(path, recursive_);
}
zipFile.CommitUpdate();
@@ -80,6 +80,70 @@ public bool ContinueRunning
}
/// <summary>
+ /// Event arguments during processing of a single file or directory.
+ /// </summary>
+ public class ProgressEventArgs : EventArgs
+ {
+ #region Constructors
+ /// <summary>
+ /// Initialise a new instance of <see cref="ScanEventArgs"/>
+ /// </summary>
+ /// <param name="name"></param>
+ /// <param name="processed">The number of bytes processed so far</param>
+ /// <param name="target">The total number of bytes to process, 0 if not known</param>
+ public ProgressEventArgs(string name, long processed, long target)
+ {
+ name_ = name;
+ processed_ = processed;
+ target_ = target;
+ }
+ #endregion
+
+ /// <summary>
+ /// The name for this event if known.
+ /// </summary>
+ public string Name
+ {
+ get { return name_; }
+ }
+
+ /// <summary>
+ /// Get set a value indicating wether scanning should continue or not.
+ /// </summary>
+ public bool ContinueRunning
+ {
+ get { return continueRunning_; }
+ set { continueRunning_ = value; }
+ }
+
+ /// <summary>
+ /// Get a percentage representing the how complete the operation is.
+ /// </summary>
+ /// <value>0 to 100 percent; 0 if target is not known.</value>
+ public float PercentComplete
+ {
+ get
+ {
+ if (target_ <= 0)
+ {
+ return 0;
+ }
+ else
+ {
+ return ((float)processed_ / (float)target_) * 100.0f;
+ }
+ }
+ }
+
+ #region Instance Fields
+ string name_;
+ long processed_;
+ long target_;
+ bool continueRunning_ = true;
+ #endregion
+ }
+
+ /// <summary>
/// Event arguments for directories.
/// </summary>
public class DirectoryEventArgs : ScanEventArgs
@@ -172,21 +236,36 @@ public bool ContinueRunning
/// <summary>
/// Delegate invoked before starting to process a file.
/// </summary>
+ /// <param name="sender">The source of the event</param>
+ /// <param name="e">The event arguments.</param>
public delegate void ProcessFileHandler(object sender, ScanEventArgs e);
/// <summary>
+ /// Delegate invoked during processing of a file or directory
+ /// </summary>
+ /// <param name="sender">The source of the event</param>
+ /// <param name="e">The event arguments.</param>
+ public delegate void ProgressHandler(object sender, ProgressEventArgs e);
+
+ /// <summary>
/// Delegate invoked when a file has been completely processed.
/// </summary>
+ /// <param name="sender">The source of the event</param>
+ /// <param name="e">The event arguments.</param>
public delegate void CompletedFileHandler(object sender, ScanEventArgs e);
/// <summary>
/// Delegate invoked when a directory failure is detected.
/// </summary>
+ /// <param name="sender">The source of the event</param>
+ /// <param name="e">The event arguments.</param>
public delegate void DirectoryFailureHandler(object sender, ScanFailureEventArgs e);
/// <summary>
/// Delegate invoked when a file failure is detected.
/// </summary>
+ /// <param name="sender">The source of the event</param>
+ /// <param name="e">The event arguments.</param>
public delegate void FileFailureHandler(object sender, ScanFailureEventArgs e);
#endregion
@@ -269,7 +348,7 @@ public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter)
/// </summary>
/// <param name="directory">The directory name.</param>
/// <param name="e">The exception detected.</param>
- public void OnDirectoryFailure(string directory, Exception e)
+ void OnDirectoryFailure(string directory, Exception e)
{
if ( DirectoryFailure == null ) {
alive_ = false;
@@ -285,7 +364,7 @@ public void OnDirectoryFailure(string directory, Exception e)
/// </summary>
/// <param name="file">The file name.</param>
/// <param name="e">The exception detected.</param>
- public void OnFileFailure(string file, Exception e)
+ void OnFileFailure(string file, Exception e)
{
if ( FileFailure == null ) {
alive_ = false;
@@ -309,6 +388,10 @@ void OnProcessFile(string file)
}
}
+ /// <summary>
+ /// Raise the complete file event
+ /// </summary>
+ /// <param name="file">The file name</param>
void OnCompleteFile(string file)
{
if (CompletedFile != null)
@@ -370,8 +453,7 @@ void ScanDir(string directory, bool recurse)
}
}
}
- catch (Exception e)
- {
+ catch (Exception e) {
OnFileFailure(fileName, e);
}
}
Oops, something went wrong.

0 comments on commit b3f2991

Please sign in to comment.