11using System ;
2+ using System . IO ;
23using NUnit . Framework ;
34
45namespace ICSharpCode . SharpZipLib . Tests . TestSupport
@@ -8,6 +9,7 @@ namespace ICSharpCode.SharpZipLib.Tests.TestSupport
89 /// </summary>
910 public static class Utils
1011 {
12+ static Random random = new Random ( ) ;
1113
1214 static void Compare ( byte [ ] a , byte [ ] b )
1315 {
@@ -25,5 +27,97 @@ static void Compare(byte[] a, byte[] b)
2527 }
2628 }
2729
30+ public static TempFile GetDummyFile ( int size = - 1 )
31+ {
32+ var tempFile = new TempFile ( ) ;
33+ if ( size < 0 )
34+ {
35+ File . WriteAllText ( tempFile . Filename , DateTime . UtcNow . Ticks . ToString ( "x16" ) ) ;
36+ }
37+ else if ( size > 0 )
38+ {
39+ var bytes = Array . CreateInstance ( typeof ( byte ) , size ) as byte [ ] ;
40+ random . NextBytes ( bytes ) ;
41+ File . WriteAllBytes ( tempFile . Filename , bytes ) ;
42+ }
43+ return tempFile ;
44+ }
45+
46+ public class TempFile : IDisposable
47+ {
48+ public string Filename { get ; internal set ; }
49+
50+ public TempFile ( )
51+ {
52+ Filename = Path . GetTempFileName ( ) ;
53+ }
54+
55+ #region IDisposable Support
56+ private bool disposed = false ; // To detect redundant calls
57+
58+
59+ protected virtual void Dispose ( bool disposing )
60+ {
61+ if ( ! disposed )
62+ {
63+ if ( disposing && File . Exists ( Filename ) )
64+ {
65+ try
66+ {
67+ File . Delete ( Filename ) ;
68+ }
69+ catch { }
70+ }
71+
72+ disposed = true ;
73+ }
74+ }
75+
76+ public void Dispose ( )
77+ => Dispose ( true ) ;
78+
79+ #endregion
80+
81+ }
82+
83+ public class TempDir : IDisposable
84+ {
85+ public string Fullpath { get ; internal set ; }
86+
87+ public TempDir ( )
88+ {
89+ Fullpath = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ;
90+ Directory . CreateDirectory ( Fullpath ) ;
91+ }
92+
93+ #region IDisposable Support
94+ private bool disposed = false ; // To detect redundant calls
95+
96+
97+ protected virtual void Dispose ( bool disposing )
98+ {
99+ if ( ! disposed )
100+ {
101+ if ( disposing && Directory . Exists ( Fullpath ) )
102+ {
103+ try
104+ {
105+ Directory . Delete ( Fullpath , true ) ;
106+ }
107+ catch { }
108+ }
109+
110+ disposed = true ;
111+ }
112+ }
113+
114+ public void Dispose ( )
115+ => Dispose ( true ) ;
116+
117+ #endregion
118+
119+ }
28120 }
121+
122+
29123}
0 commit comments