10
10
#import " LSYChapterModel.h"
11
11
#import " ZipArchive.h"
12
12
#import " TouchXML.h"
13
+
13
14
@implementation LSYReadUtilites
14
15
+(void )separateChapter : (NSMutableArray **)chapters content : (NSString *)content
15
16
{
@@ -141,22 +142,23 @@ +(NSString *)unZip:(NSString *)path
141
142
ZipArchive *zip = [[ZipArchive alloc ] init ];
142
143
NSString *zipFile = [[path stringByDeletingPathExtension ] lastPathComponent ];
143
144
if ([zip UnzipOpenFile: path]) {
144
- NSString *zipPath = [NSString stringWithFormat: @" %@ /%@ " ,NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ).firstObject ,zipFile];
145
+ NSString *zipPath = [NSString stringWithFormat: @" %@ /%@ " ,kDocuments ,zipFile];
145
146
NSFileManager *filemanager=[[NSFileManager alloc ] init ];
146
147
if ([filemanager fileExistsAtPath: zipPath]) {
147
148
NSError *error;
148
149
[filemanager removeItemAtPath: zipPath error: &error];
149
150
}
150
151
if ([zip UnzipFileTo: [NSString stringWithFormat: @" %@ /" ,zipPath] overWrite: YES ]) {
151
- return zipPath ;
152
+ return zipFile ;
152
153
}
153
154
}
154
155
return nil ;
155
156
}
156
157
#pragma mark - OPF文件路径
157
158
+(NSString *)OPFPath : (NSString *)epubPath
158
159
{
159
- NSString *containerPath = [NSString stringWithFormat: @" %@ /META-INF/container.xml" ,epubPath];
160
+
161
+ NSString *containerPath = [NSString stringWithFormat: @" %@ /%@ /META-INF/container.xml" ,kDocuments ,epubPath];
160
162
// container.xml文件路径 通过container.xml获取到opf文件的路径
161
163
NSFileManager *fileManager = [[NSFileManager alloc ] init ];
162
164
if ([fileManager fileExistsAtPath: containerPath]) {
@@ -191,7 +193,8 @@ +(NSString *)OPFPath:(NSString *)epubPath
191
193
#pragma mark - 解析OPF文件
192
194
+(NSMutableArray *)parseOPF : (NSString *)opfPath
193
195
{
194
- CXMLDocument* document = [[CXMLDocument alloc ] initWithContentsOfURL: [NSURL fileURLWithPath: opfPath] options: 0 error: nil ];
196
+ NSString *fullPath = [NSString stringWithFormat: @" %@ /%@ " ,kDocuments ,opfPath];
197
+ CXMLDocument* document = [[CXMLDocument alloc ] initWithContentsOfURL: [NSURL fileURLWithPath: fullPath] options: 0 error: nil ];
195
198
NSArray * itemsArray = [document nodesForXPath: @" //opf:item" namespaceMappings: [NSDictionary dictionaryWithObject: @" http://www.idpf.org/2007/opf" forKey: @" opf" ] error: nil ];
196
199
// opf文件的命名空间 xmlns="http://www.idpf.org/2007/opf" 需要取到某个节点设置命名空间的键为opf 用opf:节点来获取节点
197
200
NSString *ncxFile;
@@ -204,7 +207,7 @@ +(NSMutableArray *)parseOPF:(NSString *)opfPath
204
207
}
205
208
}
206
209
207
- NSString *absolutePath = [opfPath stringByDeletingLastPathComponent ];
210
+ NSString *absolutePath = [fullPath stringByDeletingLastPathComponent ];
208
211
CXMLDocument *ncxDoc = [[CXMLDocument alloc ] initWithContentsOfURL: [NSURL fileURLWithPath: [NSString stringWithFormat: @" %@ /%@ " , absolutePath,ncxFile]] options: 0 error: nil ];
209
212
NSMutableDictionary * titleDictionary = [[NSMutableDictionary alloc ] init ];
210
213
for (CXMLElement* element in itemsArray) {
@@ -236,7 +239,8 @@ +(NSMutableArray *)parseOPF:(NSString *)opfPath
236
239
for (CXMLElement* element in itemRefsArray){
237
240
NSString * chapHref = [itemDictionary objectForKey: [[element attributeForName: @" idref" ] stringValue ]];
238
241
// LSYChapterModel *model = [LSYChapterModel chapterWithEpub:[NSString stringWithFormat:@"%@/%@",absolutePath,chapHref] title:[titleDictionary valueForKey:chapHref] imagePath:[opfPath stringByDeletingLastPathComponent]];
239
- LSYChapterModel *model = [LSYChapterModel chapterWithEpub: [NSString stringWithFormat: @" %@ /%@ " ,absolutePath,chapHref] title: [titleDictionary objectForKey: chapHref] imagePath: [[[opfPath stringByDeletingLastPathComponent ]stringByAppendingPathComponent:chapHref] stringByDeletingLastPathComponent ]];
242
+ NSString *path = [[opfPath stringByDeletingLastPathComponent ] stringByAppendingPathComponent: chapHref];
243
+ LSYChapterModel *model = [LSYChapterModel chapterWithEpub: path title: [titleDictionary objectForKey: chapHref] imagePath: [path stringByDeletingLastPathComponent ]];
240
244
[chapters addObject: model];
241
245
242
246
}
0 commit comments