Statistics
| Branch: | Revision:

root / programs / ios / WebODF / Classes / NativeZip.m @ cfb53624

History | View | Annotate | Download (3.1 kB)

1
#import "NativeZip.h"
2
#import "minizip/unzip.h"
3
#import "NSData+Base64.h"
4
5
@implementation NativeZip
6
@synthesize callbackID;
7
8
9
-(void) load:(BOOL)base64 arguments:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
10
{
11
    self.callbackID = [arguments objectAtIndex:0];
12
    NSString *zipPath = [arguments objectAtIndex:1];
13
    NSString *entryPath = [arguments objectAtIndex:2]; 
14
    
15
    const char* path = [ zipPath cStringUsingEncoding:NSUTF8StringEncoding ];
16
    unzFile unzipFile = unzOpen(path);
17
    NSString* jsString = nil;
18
    BOOL error = TRUE;
19
    if (!unzipFile) {
20
		jsString = [[NSString alloc] initWithString: @"cannot open file"];
21
    } else {
22
        path = [ entryPath cStringUsingEncoding:NSUTF8StringEncoding ];
23
        int r = unzLocateFile(unzipFile, path, 1);
24
        if (r != UNZ_OK) {
25
		    jsString = [[NSString alloc] initWithString: @"cannot find entry"];
26
        } else {
27
            unz_file_info info;
28
            r = unzGetCurrentFileInfo(unzipFile, &info, 0, 0, 0, 0, 0, 0);
29
            if (r != UNZ_OK) {
30
		        jsString = [[NSString alloc] initWithString: @"cannot determine size"];
31
            } else {
32
                r = unzOpenCurrentFile(unzipFile);
33
                if (r != UNZ_OK) {
34
		            jsString = [[NSString alloc] initWithString: @"cannot open entry"];
35
                } else {
36
                    char* contents = malloc(info.uncompressed_size);
37
                    r = unzReadCurrentFile(unzipFile, contents, info.uncompressed_size);
38
                    if (r != info.uncompressed_size) {
39
                        jsString = [[NSString alloc] initWithString: @"cannot uncompress file"];
40
                    } else {
41
                        if (base64) {
42
                            NSData* readData = [NSData dataWithBytes:(const void *)contents length:sizeof(unsigned char)*info.uncompressed_size];
43
                            jsString = [NSString stringWithFormat:@"data:%@;base64,%@", @"mimetype", [readData base64EncodedString]];
44
                        } else {                    
45
                            jsString = [[NSString alloc] initWithUTF8String: contents];
46
                        }
47
                    }
48
                    unzCloseCurrentFile(unzipFile);
49
                    free(contents);
50
                    error = FALSE;
51
                }
52
            }
53
        }
54
        unzClose(unzipFile);
55
    }
56
    PluginResult* pluginResult = [PluginResult
57
                                  resultWithStatus:PGCommandStatus_OK
58
                                  messageAsString: jsString
59
                                  ];
60
    if (!error) {
61
        [self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
62
    } else {
63
        [self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
64
    }
65
}
66
67
-(void)loadAsString:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options  
68
{
69
    [self load:FALSE arguments:arguments withDict:options];
70
}
71
-(void)loadAsDataURL:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options  
72
{
73
    [self load:TRUE arguments:arguments withDict:options];
74
}
75
76
@end