在 C# 中以编程方式从 KML 文件创建 KMZ 文件
Create KMZ file from KML file Programatically in C#
我在目录中生成了一些 kml 文件。
我想知道是否有办法在 C# 中以编程方式将它们全部分组到一个 kmz 文件中。 google earth.
中显示名称和描述
谢谢并致以最诚挚的问候,
private static void combineAllKMLFilesULHR(String dirPath)
{
string kmzPath = "outputULHR.kmz";
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string rootKml = appPath + @"\" + dirPath + @"\doc.kml";
Console.WriteLine(appPath + @"\"+ dirPath);
String[] filepaths = Directory.GetFiles(appPath + @"\" + dirPath);
using (ZipArchive archive = ZipFile.Open(kmzPath, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile( rootKml, "doc.kml");
foreach (String file in filepaths)
{
Console.WriteLine(file);
if(!file.Equals(rootKml))
archive.CreateEntryFromFile( file, Path.GetFileName(file) );
}
}
}
KMZ 文件是一个 zip 存档,您可以使用 ZipArchive class 生成它。
string kmzPath = "output.kmz";
string rootKml = "doc.kml";
string referencedKml = "someother.kml";
using (ZipArchive archive = ZipFile.Open(kmzPath, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(rootKml, "doc.kml");
archive.CreateEntryFromFile(referencedKml, "someother.kml");
}
请记住将默认的 kml 命名为 doc.kml,来自 documentation:
Put the default KML file (doc.kml, or whatever name you want to give
it) at the top level within this folder. Include only one .kml file.
(When Google Earth opens a KMZ file, it scans the file, looking for
the first .kml file in this list. It ignores all subsequent .kml
files, if any, in the archive. If the archive contains multiple .kml
files, you cannot be sure which one will be found first, so you need
to include only one.)
我在目录中生成了一些 kml 文件。
我想知道是否有办法在 C# 中以编程方式将它们全部分组到一个 kmz 文件中。 google earth.
中显示名称和描述谢谢并致以最诚挚的问候,
private static void combineAllKMLFilesULHR(String dirPath)
{
string kmzPath = "outputULHR.kmz";
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string rootKml = appPath + @"\" + dirPath + @"\doc.kml";
Console.WriteLine(appPath + @"\"+ dirPath);
String[] filepaths = Directory.GetFiles(appPath + @"\" + dirPath);
using (ZipArchive archive = ZipFile.Open(kmzPath, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile( rootKml, "doc.kml");
foreach (String file in filepaths)
{
Console.WriteLine(file);
if(!file.Equals(rootKml))
archive.CreateEntryFromFile( file, Path.GetFileName(file) );
}
}
}
KMZ 文件是一个 zip 存档,您可以使用 ZipArchive class 生成它。
string kmzPath = "output.kmz";
string rootKml = "doc.kml";
string referencedKml = "someother.kml";
using (ZipArchive archive = ZipFile.Open(kmzPath, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(rootKml, "doc.kml");
archive.CreateEntryFromFile(referencedKml, "someother.kml");
}
请记住将默认的 kml 命名为 doc.kml,来自 documentation:
Put the default KML file (doc.kml, or whatever name you want to give it) at the top level within this folder. Include only one .kml file. (When Google Earth opens a KMZ file, it scans the file, looking for the first .kml file in this list. It ignores all subsequent .kml files, if any, in the archive. If the archive contains multiple .kml files, you cannot be sure which one will be found first, so you need to include only one.)