Example #1
1
        /// <summary>
        /// Creates a new KmzFile using the data specified in the KmlFile.
        /// </summary>
        /// <param name="kml">The Kml data to add to the archive.</param>
        /// <returns>
        /// A new KmzFile populated with the data specified in the KmlFile.
        /// </returns>
        /// <remarks>
        /// This overloaded version does not resolve any links in the Kml data
        /// and, therefore, will not add any local references to the archive.
        /// </remarks>
        /// <exception cref="ArgumentNullException">kml is null.</exception>
        public static KmzFile Create(KmlFile kml)
        {
            if (kml == null)
            {
                throw new ArgumentNullException("kml");
            }

            var instance = new KmzFile(new MemoryStream());
            instance._zip = new ZipFile();

            // Add the Kml data
            using (var stream = new MemoryStream())
            {
                kml.Save(stream);
                instance.AddFile(DefaultKmlFilename, stream.ToArray());
            }
            return instance;
        }
Example #2
0
        /// <summary>Opens a KmzFile from the specified stream.</summary>
        /// <param name="stream">The stream to read the data from.</param>
        /// <returns>A KmzFile representing the specified stream.</returns>
        /// <exception cref="ArgumentNullException">stream is null.</exception>
        /// <exception cref="IOException">
        /// The Kmz archive is not in the expected format.
        /// </exception>
        /// <exception cref="IOException">An I/O error occurred.</exception>
        /// <exception cref="NotSupportedException">
        /// The stream does not support reading.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The stream was closed.
        /// </exception>
        public static KmzFile Open(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var memory = new MemoryStream();

            try
            {
                stream.CopyTo(memory);
            }
            catch
            {
                memory.Dispose();
                throw;
            }

            // Check if it's a valid Zip file
            memory.Position = 0;
            if (!ZipFile.IsZipFile(memory, true))
            {
                memory.Dispose();
#if SILVERLIGHT
                throw new IOException("The Kmz archive is not in the expected format.");
#else
                throw new InvalidDataException("The Kmz archive is not in the expected format.");
#endif
            }

            // Everything's ok
            var instance = new KmzFile(memory);
            instance.ResetZip(false);
            return(instance);
        }
Example #3
0
        /// <summary>Opens a KmzFile from the specified stream.</summary>
        /// <param name="stream">The stream to read the data from.</param>
        /// <returns>A KmzFile representing the specified stream.</returns>
        /// <exception cref="ArgumentNullException">stream is null.</exception>
        /// <exception cref="IOException">
        /// The Kmz archive is not in the expected format.
        /// </exception>
        /// <exception cref="IOException">An I/O error occurred.</exception>
        /// <exception cref="NotSupportedException">
        /// The stream does not support reading.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The stream was closed.
        /// </exception>
        public static KmzFile Open(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var memory = new MemoryStream();
            try
            {
                stream.CopyTo(memory);
            }
            catch
            {
                memory.Dispose();
                throw;
            }

            // Check if it's a valid Zip file
            memory.Position = 0;
            if (!ZipFile.IsZipFile(memory, true))
            {
                memory.Dispose();
                throw new InvalidDataException("The Kmz archive is not in the expected format.");
            }

            // Everything's ok
            var instance = new KmzFile(memory);
            instance.ResetZip(false);
            return instance;
        }
Example #4
0
        /// <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
        /// &lt;href&gt;/etc/passwd&lt;/href&gt;, 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.
        /// &lt;href&gt;../../etc/passwd&lt;/href&gt; 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;
        }