/// <summary> /// Creates a new KmzFile using the data specified in the Kml file. /// </summary> /// <param name="path">The path of the Kml file.</param> /// <returns> /// A new KmzFile populated with the data specified in the Kml file. /// </returns> /// <remarks> /// Any local references in the file are written to the Kmz as archived /// resources if the resource URI is relative to and below the location /// of the Kml file. This means all absolute paths, such as /// <href>/etc/passwd</href>, are ignored, as well as /// relative paths that point to point to an object that is not in /// the same folder or subfolder of the Kml file, e.g. /// <href>../../etc/passwd</href> will be ignored for the /// file "/home/libkml/kmlfile.kml". /// </remarks> /// <exception cref="ArgumentNullException">path is null.</exception> /// <exception cref="IOException">An I/O error occurred.</exception> /// <exception cref="System.Xml.XmlException"> /// An error occurred while parsing the KML. /// </exception> public static KmzFile Create(string path) { // We'll need the base url for relative Url's later string basePath = Path.GetDirectoryName(path); byte[] data = FileHandler.ReadBytes(new Uri(path, UriKind.RelativeOrAbsolute)); // Find all the links in the Kml LinkResolver links; using (var stream = new MemoryStream(data, false)) using (var reader = new StreamReader(stream)) { links = new LinkResolver(reader, false); } // Now, if nothing threw, create the actual Kmz file var instance = new KmzFile(new MemoryStream()); instance._zip = new ZipFile(); // The first file in the archive with a .kml extension is the default // file, so make sure we add the original Kml file first. instance.AddFile(DefaultKmlFilename, data); try { // Next gather the local references and add them. foreach (var link in links.RelativePaths) { // Make sure it doesn't point to a directory below the base path if (!link.StartsWith("..", StringComparison.Ordinal)) { Uri uri = new Uri(Path.Combine(basePath, link), UriKind.RelativeOrAbsolute); byte[] file = FileHandler.ReadBytes(uri); instance.AddFile(link, file); } } } catch // Make sure we don't leak anything { instance.Dispose(); throw; } return(instance); }
/// <summary> /// Creates a new KmzFile using the data specified in the Kml file. /// </summary> /// <param name="path">The path of the Kml file.</param> /// <returns> /// A new KmzFile populated with the data specified in the Kml file. /// </returns> /// <remarks> /// Any local references in the file are written to the Kmz as archived /// resources if the resource URI is relative to and below the location /// of the Kml file. This means all absolute paths, such as /// <href>/etc/passwd</href>, are ignored, as well as /// relative paths that point to point to an object that is not in /// the same folder or subfolder of the Kml file, e.g. /// <href>../../etc/passwd</href> will be ignored for the /// file "/home/libkml/kmlfile.kml". /// </remarks> /// <exception cref="ArgumentNullException">path is null.</exception> /// <exception cref="IOException">An I/O error occurred.</exception> /// <exception cref="System.Xml.XmlException"> /// An error occurred while parsing the KML. /// </exception> public static KmzFile Create(string path) { // We'll need the base url for relative Url's later string basePath = Path.GetDirectoryName(path); byte[] data = FileHandler.ReadBytes(new Uri(path, UriKind.RelativeOrAbsolute)); // Find all the links in the Kml LinkResolver links; using (var stream = new MemoryStream(data, false)) using (var reader = new StreamReader(stream)) { links = new LinkResolver(reader, false); } // Now, if nothing threw, create the actual Kmz file var instance = new KmzFile(new MemoryStream()); instance._zip = new ZipFile(); // The first file in the archive with a .kml extension is the default // file, so make sure we add the original Kml file first. instance.AddFile(DefaultKmlFilename, data); try { // Next gather the local references and add them. foreach (var link in links.RelativePaths) { // Make sure it doesn't point to a directory below the base path if (!link.StartsWith("..", StringComparison.Ordinal)) { Uri uri = new Uri(Path.Combine(basePath, link), UriKind.RelativeOrAbsolute); byte[] file = FileHandler.ReadBytes(uri); instance.AddFile(link, file); } } } catch // Make sure we don't leak anything { instance.Dispose(); throw; } return instance; }