@@ -34,77 +34,81 @@ - (void)loadCollectionWithDelegate:(id<CollectionDelegate>)delegate
34
34
novels = [[NSMutableArray alloc ] init ];
35
35
36
36
NSFileManager *fileManager = [[NSFileManager alloc ] init ];
37
- #if TARGET_IPHONE_SIMULATOR
38
- NSString *resourcePath = [[NSBundle mainBundle ] resourcePath ];
39
- #else
40
- NSString *resourcePath = [[NSBundle mainBundle ] documentsPath ];
41
- #endif
42
- NSArray *contents = [fileManager contentsOfDirectoryAtPath: resourcePath error: nil ];
43
- for (NSString *filename in contents)
37
+
38
+ NSArray *paths = [[NSArray alloc ] initWithObjects: [[NSBundle mainBundle ] resourcePath ], [[NSBundle mainBundle ] documentsPath ], nil ];
39
+ for (NSString *resourcePath in paths)
44
40
{
45
- NSString *path = [resourcePath stringByAppendingPathComponent: filename];
46
- BOOL isDir = NO ;
47
-
48
- [fileManager fileExistsAtPath: path isDirectory: &isDir];
49
-
50
- if (!isDir && [[path pathExtension ] isEqualToString: @" zip" ])
41
+ NSArray *contents = [fileManager contentsOfDirectoryAtPath: resourcePath error: nil ];
42
+ for (NSString *filename in contents)
51
43
{
52
- NSString *outPath = [resourcePath stringByAppendingPathComponent:
53
- [filename substringToIndex: [filename length ]-4 ]]; // 4 = length of ".zip"
54
- MTLog (@" Unzipping %@ to %@ " , path, outPath);
55
- [delegate collection: self willUnzipFile: filename];
44
+ NSString *path = [resourcePath stringByAppendingPathComponent: filename];
45
+ BOOL isDir = NO ;
56
46
57
- NSError *error = nil ;
58
- [SSZipArchive unzipFileAtPath: path toDestination: outPath overwrite: YES password: nil error: &error];
59
- if (error) MTLog (@" Novel Unzip Error: %@ " , error);
60
- [fileManager removeItemAtPath: path error: NULL ];
61
- [fileManager removeItemAtPath: [outPath stringByAppendingPathComponent: @" __MACOSX" ] error: nil ];
47
+ [fileManager fileExistsAtPath: path isDirectory: &isDir];
62
48
63
- if (!error && [fileManager fileExistsAtPath: [outPath stringByAppendingPathComponent :@" sound. zip" ] ])
49
+ if (!isDir && [[path pathExtension ] isEqualToString :@" zip" ])
64
50
{
65
- NSString *soundPath = [outPath stringByAppendingPathComponent: @" sound.zip" ];
66
- NSString *soundOut = [outPath stringByAppendingPathComponent: @" sound" ];
51
+ NSString *outPath = [resourcePath stringByAppendingPathComponent:
52
+ [filename substringToIndex: [filename length ]-4 ]]; // 4 = length of ".zip"
53
+ MTLog (@" Unzipping %@ to %@ " , path, outPath);
54
+ [delegate collection: self willUnzipFile: filename];
67
55
68
- [delegate collection: self willReadSoundFromNovel: [outPath lastPathComponent ]];
69
- [SSZipArchive unzipFileAtPath: soundPath
70
- toDestination: soundOut
71
- overwrite: YES password: nil error: &error];
72
- [fileManager removeItemAtPath: [outPath stringByAppendingPathComponent: @" sound.zip" ] error: NULL ];
73
- if (error) MTLog (@" Sound Unzip Error: %@ " , error);
56
+ NSError *error = nil ;
57
+ [SSZipArchive unzipFileAtPath: path toDestination: outPath overwrite: YES password: nil error: &error];
58
+ if (error) MTLog (@" Novel Unzip Error: %@ " , error);
59
+ [fileManager removeItemAtPath: path error: NULL ];
60
+ [fileManager removeItemAtPath: [outPath stringByAppendingPathComponent: @" __MACOSX" ] error: nil ];
74
61
75
- NSString *sound2Path = [soundOut stringByAppendingPathComponent: @" sound" ];
76
- BOOL sound2IsDir = NO ;
77
- BOOL sound2Exists = [fileManager fileExistsAtPath: sound2Path isDirectory: &sound2IsDir];
78
- if (sound2Exists && sound2IsDir)
62
+ if (!error && [fileManager fileExistsAtPath: [outPath stringByAppendingPathComponent: @" sound.zip" ]])
79
63
{
80
- NSArray *s2contents = [fileManager contentsOfDirectoryAtPath: sound2Path error: NULL ];
81
- for (NSString *name in s2contents)
64
+ NSString *soundPath = [outPath stringByAppendingPathComponent: @" sound.zip" ];
65
+ NSString *soundOut = [outPath stringByAppendingPathComponent: @" sound" ];
66
+
67
+ [delegate collection: self willReadSoundFromNovel: [outPath lastPathComponent ]];
68
+ [SSZipArchive unzipFileAtPath: soundPath
69
+ toDestination: soundOut
70
+ overwrite: YES password: nil error: &error];
71
+ [fileManager removeItemAtPath: [outPath stringByAppendingPathComponent: @" sound.zip" ] error: NULL ];
72
+ if (error) MTLog (@" Sound Unzip Error: %@ " , error);
73
+
74
+ NSString *sound2Path = [soundOut stringByAppendingPathComponent: @" sound" ];
75
+ BOOL sound2IsDir = NO ;
76
+ BOOL sound2Exists = [fileManager fileExistsAtPath: sound2Path isDirectory: &sound2IsDir];
77
+ if (sound2Exists && sound2IsDir)
82
78
{
83
- [fileManager moveItemAtPath: [sound2Path stringByAppendingPathComponent: name]
84
- toPath: [soundOut stringByAppendingPathComponent: name]
85
- error: NULL ];
79
+ NSArray *s2contents = [fileManager contentsOfDirectoryAtPath: sound2Path error: NULL ];
80
+ for (NSString *name in s2contents)
81
+ {
82
+ [fileManager moveItemAtPath: [sound2Path stringByAppendingPathComponent: name]
83
+ toPath: [soundOut stringByAppendingPathComponent: name]
84
+ error: NULL ];
85
+ }
86
+
87
+ [fileManager removeItemAtPath: sound2Path error: NULL ];
86
88
}
87
-
88
- [fileManager removeItemAtPath: sound2Path error: NULL ];
89
89
}
90
+
91
+ path = outPath;
92
+ filename = [path lastPathComponent ];
93
+ isDir = YES ;
90
94
}
91
95
92
- path = outPath;
93
- filename = [path lastPathComponent ];
94
- isDir = YES ;
95
- }
96
-
97
- MTLog (@" %d : %@ " , isDir, path);
98
-
99
- if (isDir)
100
- {
101
- // Skip folders that are not novels
102
- if (![fileManager fileExistsAtPath: [path stringByAppendingPathComponent: @" info.txt" ]]) continue ;
96
+ MTLog (@" %d : %@ " , isDir, path);
103
97
104
- MTLog (@" %@ " , filename);
105
- Novel *novel = [[Novel alloc ] initWithDirectory: filename];
106
- [novels addObject: novel];
107
- [novel release ];
98
+ if (isDir)
99
+ {
100
+ // Skip folders that are not novels
101
+ if (![fileManager fileExistsAtPath: [path stringByAppendingPathComponent: @" info.txt" ]])
102
+ {
103
+ MTLog (@" Couldn't find info.txt in %@ " , path);
104
+ continue ;
105
+ }
106
+
107
+ MTLog (@" %@ " , filename);
108
+ Novel *novel = [[Novel alloc ] initWithDirectory: filename];
109
+ [novels addObject: novel];
110
+ [novel release ];
111
+ }
108
112
}
109
113
}
110
114
0 commit comments