2016-08-24 328 views
1

我已经创建成功kml文件名为gis.kml,现在我看到了一个边际的大小变化,当您转换KML到KMZ使用googleearth。所以我在想如何转换KMLKMZ在C#。我有代码转换任何文件为.zip,但不会在这里工作如何KML文件转换为KMZ文件在C#中

+1

Peter:请参阅以下网页:https://developers.google.com/kml/documentation/kmzarchives – jdweng

+0

这意味着ZIP实用程序是否足够?我使用7zip从kml和zip文件中使用googleearth创建了kmz文件,并找到了一些KB大小的差异。因此认为它会有所不同。但是你发送的链接提到了相同的 – peter

+1

只要确保在kml中引用的任何文件在kmz中的正确位置。描述说kmz必须有一个文件夹(可能是空的)。 – jdweng

回答

2

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

这里是C#代码片段创建KMZ文件:

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文件。