2

I am writing a C# program using http://www.icsharpcode.net/opensource/sharpziplib/ to compress to a zip file a KMZ file that will contain a KML file and icons.

My attempt:

  1. After opening the KMZ file in Google earth the icons do now show.
  2. I then convert the KMZ to a zip file so I can inspect its contents.
  3. I rename the icon to a different name then back to its original name.
  4. I then change it back to a KMZ file and open in Google earth and the icons show fine.

Any ideas as to what I am doing wrong in the compression process that would cause the icons to not initially show?

P̲̳x͓L̳
  • 3,615
  • 3
  • 29
  • 37
TheWommies
  • 4,922
  • 11
  • 61
  • 79
  • Might want to elaborate on how KMZ is "converted" to a ZIP file since KMZ is already a .ZIP but with a different extension. Assume you just rename file name extension (i.e. .kmz to .zip). If you're using WinZip to rename the icon then it might reset some properties on the entries and/or cleanup other header info when saved so the resulting file is valid. Snippet of code how KMZ is created would be helpful. – CodeMonkey Mar 28 '14 at 17:47

1 Answers1

3

One trick to get KMZ files created with CSharpZipLib to properly work with Google Earth is turning off the Zip64 mode which is not compatible with Google Earth.

For KMZ files to be interoperable in Google Earth and other earth browsers it must be ZIP 2.0 compatible using "legacy" compression methods (e.g. deflate) and not use extensions such as Zip64. This issue is mentioned in the KML Errata.

Here's snippet of C# code to create a KMZ file:

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();
    }
}
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
  • I have turned it off, still have the same issue when the icon is in a directory in the zip file. However when the icon files are not in a directory then they show up fine. Not sure this does not work when a image is in a directory of the zip file as to why this does not work – TheWommies Mar 30 '14 at 22:25
  • Before calling PutNextEntry() on icon entry try manually setting the file size on the entry; e.g. newEntry.Size = . – CodeMonkey Mar 31 '14 at 01:10