peter
peter

Reputation: 8682

How to convert KML file to KMZ file in c#

I have created successfully kml file called gis.kml,Now i had seen a marginal size change when you convert KML to KMZ by using googleearth.So i am thinking how to convert KML to KMZ in c#.I have code to convert any file to .zip ,but that will not work here

Upvotes: 1

Views: 3193

Answers (2)

CodeMonkey
CodeMonkey

Reputation: 23748

You can either read in the file 'gis.kml' and add its contents to a KMZ file or you can programmatically create the KML elements and convert to a byte array to write to the KMZ stream. This solution uses the CSharpZipLib to create the KMZ file.

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(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();
    }
}

Can also create the KMZ file using the ZipArchive class.

Upvotes: 2

peter
peter

Reputation: 8682

KMZ to KML

Zip the file then change the extension to KMZ

Upvotes: 0

Related Questions