Created
April 3, 2015 08:37
-
-
Save dzindra/296d54c6f16428e2a6ca to your computer and use it in GitHub Desktop.
Extracting safari webarchive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// webarchivedump | |
// | |
// Created by Jindrich Dolezy on 03.04.15. | |
// Copyright (c) 2015 Jindrich Dolezy. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
BOOL extractResource(NSDictionary *resource, NSString *outputDir, NSString *urlBase) { | |
NSString *url = resource[@"WebResourceURL"]; | |
NSData *data = resource[@"WebResourceData"]; | |
if (![url hasPrefix:urlBase]) { | |
NSLog(@"Url of resource %@ does not start with %@", url, urlBase); | |
return NO; | |
} | |
NSString *outputPath = [outputDir stringByAppendingPathComponent:[url substringFromIndex:urlBase.length]]; | |
[[NSFileManager defaultManager] createDirectoryAtPath:[outputPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; | |
if (![data writeToFile:outputPath atomically:NO]) { | |
NSLog(@"Failed to write file at %@", outputPath); | |
return NO; | |
} | |
return YES; | |
} | |
BOOL extractWebarch(NSString *srcFile, NSString *outputDir) { | |
NSDictionary *webarch = [NSDictionary dictionaryWithContentsOfFile:srcFile]; | |
if (!webarch) { | |
NSLog(@"Unable to open file %@", srcFile); | |
return NO; | |
} | |
[[NSFileManager defaultManager] createDirectoryAtPath:outputDir withIntermediateDirectories:YES attributes:nil error:nil]; | |
NSString *url = webarch[@"WebMainResource"][@"WebResourceURL"]; | |
NSString *urlBase = [[[NSURL URLWithString:url] URLByDeletingLastPathComponent] absoluteString]; | |
NSLog(@"url = %@", url); | |
NSLog(@"urlBase = %@", urlBase); | |
extractResource(webarch[@"WebMainResource"], outputDir, urlBase); | |
for (NSDictionary *res in webarch[@"WebSubresources"]) { | |
extractResource(res, outputDir, urlBase); | |
} | |
return YES; | |
} | |
int main(int argc, const char *argv[]) { | |
@autoreleasepool { | |
extractWebarch(@"webarchive.plist", @"output"); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment