9
9
10
10
namespace HayDayDecoder
11
11
{
12
- class Decoder
12
+ public class Decoder
13
13
{
14
14
public Decoder ( ) { }
15
15
16
- public bool unzipDirectory ( string dirPath )
16
+ public IEnumerable < string > unzipDirectory ( string dirPath )
17
17
{
18
18
string [ ] fileList = Directory . GetFiles ( dirPath ) ;
19
19
foreach ( string file in fileList )
@@ -22,28 +22,31 @@ public bool unzipDirectory(string dirPath)
22
22
if ( fileInfo . Extension != ".csv" ) continue ;
23
23
24
24
// Unzip file
25
- if ( unzipFile ( file ) )
25
+ string result ;
26
+ var unzipStatus = unzipFile ( file , out result ) ;
27
+
28
+ if ( unzipStatus )
26
29
{
27
30
// Delete original file
28
31
File . Delete ( file ) ;
29
-
30
- System . Console . WriteLine ( "[Delete file]\t " + fileInfo . Name ) ;
32
+ result += "\n [Delete file]\t " + fileInfo . Name ;
31
33
}
34
+ yield return result ;
32
35
}
33
- return true ;
36
+ yield break ;
34
37
}
35
38
36
- public bool unzipFile ( string filePath )
39
+ public bool unzipFile ( string filePath , out string result )
37
40
{
38
41
FileInfo fileInfo = new FileInfo ( filePath ) ;
39
42
40
43
// Step 1. Fix file
41
44
fixFile ( filePath , fileInfo ) ;
42
45
43
46
// Step 2. Unzip file
47
+ string outputPath = fileInfo . FullName . Replace ( ".csv" , "_r.csv" ) ;
44
48
try
45
49
{
46
- string outputPath = fileInfo . FullName . Replace ( ".csv" , "_r.csv" ) ;
47
50
using ( var input = new FileStream ( filePath , FileMode . Open , FileAccess . ReadWrite ) )
48
51
{
49
52
var decoder = new LzmaDecodeStream ( input ) ;
@@ -61,11 +64,11 @@ public bool unzipFile(string filePath)
61
64
}
62
65
catch ( Exception )
63
66
{
64
- System . Console . WriteLine ( "[Broken file]\t " + fileInfo . Name ) ;
67
+ result = "[Broken file]\t " + fileInfo . Name ;
68
+ File . Delete ( outputPath ) ;
65
69
return false ;
66
70
}
67
-
68
- System . Console . WriteLine ( "[Unzip file]\t " + fileInfo . Name ) ;
71
+ result = "[Unzip file]\t " + fileInfo . Name ;
69
72
return true ;
70
73
}
71
74
0 commit comments