如何在c#中将KML文件转换为KMZ文件

本文关键字:文件 转换 KMZ KML 中将 | 更新日期: 2023-09-27 18:04:00

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

如何在c#中将KML文件转换为KMZ文件

您可以在文件'gis '中读取。并将其内容添加到KMZ文件中,或者您可以通过编程方式创建kml元素并将其转换为字节数组以写入KMZ流。此解决方案使用CSharpZipLib创建KMZ文件。

下面是创建KMZ文件的c#代码片段:
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文件。

KMZ to KML

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