Skip to content

Commit eeaba58

Browse files
committed
Further tweaks to FastZip exception handling and unit tests.
1 parent e0dcb45 commit eeaba58

File tree

3 files changed

+61
-68
lines changed

3 files changed

+61
-68
lines changed

src/Core/FileSystemScanner.cs

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -286,58 +286,6 @@ public bool ContinueRunning
286286
public delegate void FileFailureHandler(object sender, ScanFailureEventArgs e);
287287
#endregion
288288

289-
/// <summary>
290-
/// An exception encountered dutring scanning.
291-
/// </summary>
292-
#if !NETCF_1_0 && !NETCF_2_0
293-
[Serializable]
294-
#endif
295-
public class ScanException : SharpZipBaseException
296-
{
297-
/// <summary>
298-
/// Initializes a new instance of the <see cref="ScanException"/> class.
299-
/// </summary>
300-
public ScanException()
301-
{
302-
}
303-
304-
/// <summary>
305-
/// Initializes a new instance of the <see cref="ScanException"/> class.
306-
/// </summary>
307-
/// <param name="message">The message.</param>
308-
public ScanException(string message)
309-
: base(message)
310-
{
311-
}
312-
313-
/// <summary>
314-
/// Initializes a new instance of the <see cref="ScanException"/> class.
315-
/// </summary>
316-
/// <param name="message">The message.</param>
317-
/// <param name="innerException">The inner exception.</param>
318-
public ScanException(string message, Exception innerException)
319-
: base(message, innerException)
320-
{
321-
}
322-
323-
#if !NETCF_1_0 && !NETCF_2_0
324-
/// <summary>
325-
/// Initializes a new instance of the <see cref="ScanException"/> class.
326-
/// </summary>
327-
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
328-
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
329-
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
330-
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
331-
public ScanException(
332-
System.Runtime.Serialization.SerializationInfo info,
333-
System.Runtime.Serialization.StreamingContext context
334-
)
335-
: base(info, context)
336-
{
337-
}
338-
#endif
339-
}
340-
341289
/// <summary>
342290
/// FileSystemScanner provides facilities scanning of files and directories.
343291
/// </summary>
@@ -417,36 +365,35 @@ public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter)
417365
/// </summary>
418366
/// <param name="directory">The directory name.</param>
419367
/// <param name="e">The exception detected.</param>
420-
void OnDirectoryFailure(string directory, Exception e)
368+
bool OnDirectoryFailure(string directory, Exception e)
421369
{
422370
DirectoryFailureHandler handler = DirectoryFailure;
423-
if (handler == null) {
424-
alive_ = false;
425-
throw new ScanException("Directory scan failure", e);
426-
}
427-
else {
371+
bool result = (handler != null);
372+
if ( result ) {
428373
ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e);
429374
handler(this, args);
430375
alive_ = args.ContinueRunning;
431376
}
377+
return result;
432378
}
433379

434380
/// <summary>
435381
/// Raise the FileFailure event.
436382
/// </summary>
437383
/// <param name="file">The file name.</param>
438384
/// <param name="e">The exception detected.</param>
439-
void OnFileFailure(string file, Exception e)
385+
bool OnFileFailure(string file, Exception e)
440386
{
441387
FileFailureHandler handler = FileFailure;
442-
if ( handler == null ) {
443-
alive_ = false;
444-
throw new ScanException("File failure", e);
445-
} else {
388+
389+
bool result = (handler != null);
390+
391+
if ( result ){
446392
ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
447393
FileFailure(this, args);
448394
alive_ = args.ContinueRunning;
449395
}
396+
return result;
450397
}
451398

452399
/// <summary>
@@ -534,13 +481,17 @@ void ScanDir(string directory, bool recurse)
534481
}
535482
}
536483
catch (Exception e) {
537-
OnFileFailure(fileName, e);
484+
if (!OnFileFailure(fileName, e)) {
485+
throw;
486+
}
538487
}
539488
}
540489
}
541490
}
542491
catch (Exception e) {
543-
OnDirectoryFailure(directory, e);
492+
if (!OnDirectoryFailure(directory, e)) {
493+
throw;
494+
}
544495
}
545496

546497
if ( alive_ && recurse ) {
@@ -556,7 +507,9 @@ void ScanDir(string directory, bool recurse)
556507
}
557508
}
558509
catch (Exception e) {
559-
OnDirectoryFailure(directory, e);
510+
if (!OnDirectoryFailure(directory, e)) {
511+
throw;
512+
}
560513
}
561514
}
562515
}

src/Zip/FastZip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ public bool OnDirectoryFailure(string directory, Exception e)
101101
/// <returns>A boolean indicating if execution should continue or not.</returns>
102102
public bool OnFileFailure(string file, Exception e)
103103
{
104-
bool result = false;
105104
FileFailureHandler handler = FileFailure;
105+
bool result = (handler != null);
106106

107-
if ( handler != null ) {
107+
if ( result ) {
108108
ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
109109
handler(this, args);
110110
result = args.ContinueRunning;

tests/Zip/ZipTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,46 @@ public void Encryption()
26902690
}
26912691
}
26922692

2693+
[Test]
2694+
[Category("Zip")]
2695+
[ExpectedException(typeof(DirectoryNotFoundException))]
2696+
public void CreateExceptions()
2697+
{
2698+
FastZip fastZip = new FastZip();
2699+
string tempFilePath = GetTempFilePath();
2700+
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
2701+
2702+
string addFile = Path.Combine(tempFilePath, "test.zip");
2703+
try
2704+
{
2705+
fastZip.CreateZip(addFile, @"z:\doesnt exist", false, null);
2706+
}
2707+
finally
2708+
{
2709+
File.Delete(addFile);
2710+
}
2711+
}
2712+
2713+
[Test]
2714+
[Category("Zip")]
2715+
[ExpectedException(typeof(FileNotFoundException))]
2716+
public void ExtractExceptions()
2717+
{
2718+
FastZip fastZip = new FastZip();
2719+
string tempFilePath = GetTempFilePath();
2720+
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
2721+
2722+
string addFile = Path.Combine(tempFilePath, "test.zip");
2723+
try
2724+
{
2725+
fastZip.ExtractZip(addFile, @"z:\doesnt exist", null);
2726+
}
2727+
finally
2728+
{
2729+
File.Delete(addFile);
2730+
}
2731+
}
2732+
26932733
[Test]
26942734
[Category("Zip")]
26952735
public void NonAsciiPasswords()

0 commit comments

Comments
 (0)