让使用CSharpZipLib创建的 KMZ 文件与 Google 地球一起正常工作的一个技巧是关闭与 Google 地球不兼容的 Zip64 模式。
要使 KMZ 文件在 Google 地球和其他地球浏览器中可互操作,它必须使用“传统”压缩方法(例如 deflate)与 ZIP 2.0 兼容,并且不能使用 Zip64 等扩展名。KML Errata中提到了这个问题。
这是用于创建 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(/*binary array*/);
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();
}
}