打开.webbarchive文件,修改并保存



我正在为Lion开发一个应用程序,我想做的是打开一个。webchive文件,修改DOM的一个片段,然后将修改后的DOM写入同一个文件。

这是我到目前为止的代码。它打开webchive,修改它,然后保存回文件。

    NSString *archivePath = @"/Users/tigger/Library/Mail/V2/MailData/Signatures/1216DD8D-C7E2-4DE1-9FCD-0A9A3412C788.webarchive";
    NSData *plistData = [NSData dataWithContentsOfFile:archivePath];
    NSString *error;
    NSPropertyListFormat format;
    NSMutableDictionary *plist;
    plist = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistData
                                             mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                       format:&format
                                             errorDescription:&error];
    if(!plist){
        printf("no plist");
        [error release];
    }else{
        NSString *s = [NSString stringWithUTF8String:[[[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"] bytes]];
        NSString *new = [s stringByReplacingOccurrencesOfString:@"</body>" withString:@"hey there!</body>"];
        [[plist objectForKey:@"WebMainResource"] setObject:new forKey:@"WebResourceData"];
        printf("Archive: %s", [[plist description] UTF8String]);       
        NSData *data = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil];
        [data writeToURL:[NSURL fileURLWithPath:@"/Users/tigger/Library/Mail/V2/MailData/Signatures/test.webarchive"] atomically:YES];
    }

问题是生成的网页搜索是无效的。原来是这样的:

bplist00—_WebMainResource’  
_WebResourceTextEncodingName_WebResourceFrameName^WebResourceURL_WebResourceData_WebResourceMIMETypeUUTF-8PUdata:O<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Dan Shipper</div><div>dshipper@gmail.com</div><div><br></div></body></span><br class="Apple-interchange-newline">Ytext/html(F]l~îöõ°™
¥

结果是这样的:

bplist00—_WebMainResource’  
^WebResourceURL_WebResourceFrameName_WebResourceMIMEType_WebResourceData_WebResourceTextEncodingNameUdata:PYtext/html_<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Dan Shipper</div><div>dshipper@gmail.com</div><div><br></div>hey there!</body></span><br class="Apple-interchange-newline">UUTF-8(7Ndvîöõ•∏
æ

谁有任何想法,为什么它是无效的或如何修复它?非常感谢你的帮助!

我也尝试过使用textutil转换命令来生成webchive,但它不起作用,因为在我的原始HTML文件中,我有一个像这样的图像:

<img src="http://www.domainpolish.com/images/crowd.png">

但是当我使用textil时,它会下载图像并保存为:

<img src"file:///1.png">

即使我不希望它下载或更改url。我已经使用了noload, nostore和baseurl选项,但无济于事。

编辑:修正了!所以问题是当我替换HTML时我插入的是NSString而不是NSData:

NSString *s = [NSString stringWithUTF8String:[[[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"] bytes]];
NSString *new = [s stringByReplacingOccurrencesOfString:@"</body>" withString:@"hi there!</body>"];
NSData *sourceData = [new dataUsingEncoding:NSUTF8StringEncoding];
[[plist objectForKey:@"WebMainResource"] setObject:sourceData forKey:@"WebResourceData"];

更新:我刚刚重新阅读了问题,看到了解决方案…

您正在用本行中的错误对象替换主资源数据:

[[plist objectForKey:@"WebMainResource"] setObject:new forKey:@"WebResourceData"];

new是一个NSString,如果你应该是一个NSData对象:

替换后,应将字符串内容转换为二进制数据。

[[plist objectForKey:@"WebMainResource"] setObject:[new dataUsingEncoding:NSUTF8StringEncoding] forKey:@"WebResourceData"];

来自维基百科:

webchive格式是源文件与使用NSKeyedEncoder以二进制plist格式保存的文件名。

考虑到这一点,您可以使用NSKeyedEncoder来查找文件列表,然后使用NSData来拆分文件并找到您正在寻找的HTML

相关内容

  • 没有找到相关文章