private static KmzFile SaveKmlAndLinkedContentIntoAKmzArchive(KmlFile kml, string path) { // All the links in the KML will be relative to the KML file, so // find it's directory so we can add them later string basePath = Path.GetDirectoryName(path); // Create the archive with the KML data KmzFile kmz = KmzFile.Create(kml); // Now find all the linked content in the KML so we can add the // files to the KMZ archive var links = new LinkResolver(kml); // Next gather the local references and add them. foreach (string relativePath in links.GetRelativePaths()) { // Make sure it doesn't point to a directory below the base path if (relativePath.StartsWith("..", StringComparison.Ordinal)) { continue; } // Add it to the archive string fullPath = Path.Combine(basePath, relativePath); using (Stream file = File.OpenRead(fullPath)) { kmz.AddFile(relativePath, file); } } return(kmz); }