Skip to content

Commit

Permalink
intelligently locate top-level KML file in KMZ archives
Browse files Browse the repository at this point in the history
 * adds support for top-level files other than "doc.kml"
 * does not require archive structure to share archive filename base
  • Loading branch information
incanus committed Jan 31, 2011
1 parent 3af96eb commit 68b3034
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions SimpleKML.m
Expand Up @@ -43,6 +43,7 @@
@interface SimpleKML (SimpleKMLPrivate)

- (id)initWithContentsOfFile:(NSString *)path error:(NSError **)error;
+ (NSString *)topLevelKMLFilePathInArchiveAtPath:(NSString *)archivePath;

@end

Expand Down Expand Up @@ -78,7 +79,8 @@ - (id)initWithContentsOfURL:(NSURL *)URL error:(NSError **)error
}
else if ([[[URL relativePath] pathExtension] isEqualToString:@"kmz"])
{
NSData *sourceData = [SimpleKML dataFromArchiveAtPath:[URL relativePath] withFilePath:@"doc.kml"]; // TODO: find first KML, not just doc.kml
NSData *sourceData = [SimpleKML dataFromArchiveAtPath:[URL relativePath]
withFilePath:[SimpleKML topLevelKMLFilePathInArchiveAtPath:[URL relativePath]]];

if (sourceData)
source = [[NSString alloc] initWithData:sourceData encoding:NSUTF8StringEncoding];
Expand Down Expand Up @@ -223,17 +225,32 @@ + (UIColor *)colorForString:(NSString *)colorString;
return color;
}

+ (NSData *)dataFromArchiveAtPath:(NSString *)archivePath withFilePath:(NSString *)filePath
+ (NSString *)topLevelKMLFilePathInArchiveAtPath:(NSString *)archivePath
{
ZipFile *archive = [[[ZipFile alloc] initWithFileName:archivePath mode:ZipFileModeUnzip] autorelease];

NSString *archiveName = [archivePath lastPathComponent];
NSString *archiveExtension = [archivePath pathExtension];
NSArray *files = [archive listFileInZipInfos];

archiveName = [archiveName substringWithRange:NSMakeRange(0, [archiveName length] - ([archiveExtension length] + 1))];
for (FileInZipInfo *file in files)
{
// look for either "<archive name>/<file name>.kml" or just plain "<file name>.kml"
//
if ([[[file name] componentsSeparatedByString:@"/"] count] < 3 && [[[file name] pathExtension] isEqualToString:@"kml"])
{
[archive close];

return [file name];
}
}

return nil;
}

+ (NSData *)dataFromArchiveAtPath:(NSString *)archivePath withFilePath:(NSString *)filePath
{
ZipFile *archive = [[[ZipFile alloc] initWithFileName:archivePath mode:ZipFileModeUnzip] autorelease];

if ( ! [archive locateFileInZip:[NSString stringWithFormat:@"%@/%@", archiveName, filePath]] &&
! [archive locateFileInZip:filePath])
if ( ! [archive locateFileInZip:filePath])
{
[archive close];

Expand Down

0 comments on commit 68b3034

Please sign in to comment.