2016-08-24 324 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文件。