【问题标题】:How to convert KML file to KMZ file in c#如何在 C# 中将 KML 文件转换为 KMZ 文件
【发布时间】:2016-08-24 16:45:57
【问题描述】:

我已经成功创建了kml 名为gis.kml 的文件,现在当您使用googleearth 将KML 转换为KMZ 时,我看到了边际大小变化。所以我正在考虑如何将KML 转换为KMZ在 c# 中。我有将任何文件转换为 .zip 的代码,但这在这里不起作用

【问题讨论】:

  • 这意味着 ZIP 实用程序就足够了?我使用来自 kml 的 googleearth 创建了 kmz 文件,并使用 7zip 创建了 zip 文件并发现了一些 KB 大小差异。因此认为它会有所不同。但是您发送的链接提到都一样
  • 只要确保在 kml 中引用的所有文件都在 kmz 中的正确位置。描述说 kmz 必须有一个文件夹(可以是空的)。
  • @jdweng 太好了
  • @jdweng 关于我两天前问的问题,你能在顶部保持相同的命名空间结构并删除更多实例吗??

标签: c# asp.net .net kml kmz


【解决方案1】:

您可以读入文件“gis.kml”并将其内容添加到 KMZ 文件中,也可以以编程方式创建 KML 元素并转换为字节数组以写入 KMZ 流。此解决方案使用CSharpZipLib 创建 KMZ 文件。

这是创建 KMZ 文件的 C# 代码的 sn-p:

using (FileStream fileStream = File.Create(ZipFilePath)) // Zip File Path (String Type)
{
    using (ZipOutputStream zipOutputStream = new ZipOutputStream(fileStream))
    {
        // following line must be present for KMZ file to work in Google Earth
        zipOutputStream.UseZip64 = UseZip64.Off;

        // now normally create the zip file as you normally would 
        // add root KML as first entry
        ZipEntry zipEntry = new ZipEntry("doc.kml");
        zipOutputStream.PutNextEntry(zipEntry);  
        //build you binary array from FileSystem or from memory... 
        zipOutputStream.write(System.IO.File.ReadAllBytes("gis.kml")); 
        zipOutputStream.CloseEntry();
        // next add referenced file entries (e.g. icons, etc.)
        // ...
        //don't forget to clean your resources and finish the outputStream
        zipOutputStream.Finish();
        zipOutputStream.Close();
    }
}

还可以使用ZipArchive 类创建KMZ 文件。

【讨论】:

    【解决方案2】:

    KMZ to KML

    压缩文件,然后将扩展名更改为 KMZ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2020-03-10
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多