主要内容: C#遍历文件(夹)、读取tar.gz格式压缩文件,提取信息(后续会读取各种格式遥感影像的元数据)并批量建库入库(Access)。不废话了,上代码(将近900行,但很多有待优化)(每天早上8点到晚上22点一直对着电脑呢,期间两三个小时的吃饭午休时间,对不起我的眼睛呀
):
using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using ADOX;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
namespace ConsoleApplication2
{
class Program
{
/**********解压函数**********/
//public static void ZipToFile(string path, string addres)
//{
// ZipInputStream s = new ZipInputStream(File.OpenRead(path));
// ZipEntry fileEntry;
// while ((fileEntry = s.GetNextEntry()) != null)
// {
// string filename = Path.GetFileName(fileEntry.Name);
// if (filename != "")
// {
// filename = addres + "\\" + filename;
// FileStream streamWriter = File.Create(filename);
// int size = 2048;
// byte[] buffer = new byte[s.Length];
// size = s.Read(buffer, 0, size);
// streamWriter.Write(buffer, 0, size);
// streamWriter.Close();
// }
// }
// s.Close();
//}
public static List getModTarGzFileNames(string TargetFile, string fileDir)
{
List modfiles = new List();
string rootFile = " ";
string filepath = Path.Combine(fileDir, TargetFile);
//读取压缩文件(zip文件),准备解压缩
GZipInputStream gzs = new GZipInputStream(File.OpenRead(filepath.Trim()));
string tarfilename = Path.Combine(fileDir, Path.GetFileNameWithoutExtension(TargetFile));
FileStream destFile = File.Open(tarfilename, FileMode.Create);
try
{
int buffersize = 2048;//缓冲区的尺寸,一般是2048的倍数
byte[] FileData = new byte[buffersize];//创建缓冲数据
while (buffersize > 0)//一直读取到文件末尾
{
buffersize = gzs.Read(FileData, 0, buffersize);//读取压缩文件数据
destFile.Write(FileData, 0, buffersize);//写入目标文件
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
destFile.Close();//关闭目标文件
gzs.Close();//关闭压缩文件
TarInputStream s = new TarInputStream(File.OpenRead(tarfilename.Trim()));
//ZipInputStream();
TarEntry theEntry;
string path = fileDir;
//解压出来的文件保存的路径
string rootDir = " ";
//根目录下的第一个子文件夹的名称
while ((theEntry = s.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(theEntry.Name);
//得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= 0)
{
rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
}
string dir = Path.GetDirectoryName(theEntry.Name);
//根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name);
if (fileName != "")
{
modfiles.Add(fileName);
}
}
s.Close();
File.Delete(tarfilename);
return modfiles;
}
//给出某年的第几天转换成年月日表示 Day一个示例:A2010106
public static string DayToDate(string Day)
{
char[] ch = Day.ToCharArray();
string Date = "ch[1]" + "ch[2]" + "ch[3]" + "ch[4]";
int year = (ch[1] - '0') * 1000 + (ch[2] - '0') * 100 + (ch[3] - '0') * 10 + (ch[4] - '0');
Date = year.ToString() + "年";
int day = (ch[5] - '0') * 100 + (ch[6] - '0') * 10 + (ch[7] - '0');
int month=0,date=0;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
//闰年
if (day >= 1 && day <= 31)
{
month = 1;
date = day;
}
else if(day>=32&&day<=60)
{
month = 2;
date = day-31;
}
else if (day >= 61 && day <=91)
{
month = 3;
date = day - 60;
}
else if (day >= 92 && day <= 121)
{
month = 4;
date = day - 91;
}
else if (day >=122 && day <= 152)
{
month = 5;
date = day - 121;
}
else if (day >= 153 && day <= 182)
{
month = 6;
date = day - 152;
}
else if (day >= 183 && day <= 213)
{
month = 7;
date = day - 182;
}
else if (day >= 214 && day <= 244)
{
month = 8;
date = day - 213;
}
else if (day >= 245 && day <= 274)
{
month = 9;
date = day - 244;
}
else if (day >= 275 && day <= 305)
{
month = 10;
date = day - 274;
}
else if (day >= 306 && day <= 335)
{
month = 11;
date = day - 305;
}
else if (day >= 336 && day <= 366)
{
month = 12;
date = day - 335;
}
}
else
{
//非闰年
if (day >= 1 && day <= 31)
{
month = 1;
date = day;
}
else if (day >= 32 && day <= 59)
{
month = 2;
date = day - 31;
}
else if (day >= 60 && day <= 90)
{
month = 3;
date = day - 59;
}
else if (day >= 91 && day <= 120)
{
month = 4;
date = day - 90;
}
else if (day >= 121 && day <= 151)
{
month = 5;
date = day - 120;
}
else if (day >= 152 && day <= 181)
{
month = 6;
date = day - 151;
}
else if (day >= 182 && day <= 212)
{
month = 7;
date = day - 181;
}
else if (day >= 213 && day <= 243)
{
month = 8;
date = day - 212;
}
else if (day >= 244 && day <= 273)
{
month = 9;
date = day - 243;
}
else if (day >= 274 && day <= 304)
{
month = 10;
date = day - 273;
}
else if (day >= 305 && day <= 334)
{
month = 11;
date = day - 304;
}
else if (day >= 335 && day <= 365)
{
month = 12;
date = day - 334;
}
}
Date += month.ToString() + "月" + date.ToString() + "日";
return Date;
}
static void Main(string[] args)
{
#region "已注释:TEST"
//string filePath = @"E:\DATA02\AVHRR\1149891193\001\NSS.GHRR.NJ.D99001.S0258.E0451.B2063738.WI";
//string fullFileName = System.IO.Path.GetFileName(filePath);
//int position = fullFileName.IndexOf("WI");
//string fileName = fullFileName.Substring(0, position - 1);
//Console.WriteLine("文件名:");
//Console.WriteLine(fileName);
//Console.WriteLine("带格式的文件名:");
//Console.WriteLine(fullFileName);
/*
Console.WriteLine("请输入目录:");
//string ddir = Console.ReadLine();
string dirInput = Console.ReadLine();
Console.WriteLine("该目录下的子目录有:");
DirectoryInfo dir = new DirectoryInfo(dirInput);
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo my in dirs)
{
Console.WriteLine(my.FullName);
}
*/
Console.WriteLine("请输入类型(如:modis、omi、tmos等并按回车结束(不区分大小写)):");
string typeInput=Console.ReadLine();
if (typeInput.ToLower() == "modis")
goto LabelModis;
else if (typeInput.ToLower() == "omi")
goto LabelOmi;
else if (typeInput.ToLower() == "tmos")
goto LabelTmos;
else if (typeInput.ToLower() == "avhrr")
goto LabelAvhrr;
#endregion
#region "已注释:判断文件夹下有无子目录"
//if (dirs.Length > 0)
//{
// foreach(DirectoryInfo dChild in dir.GetDirectories("*"))
// {
// //如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
// //Console.WriteLine(dChild);//打印文件名
// Console.WriteLine(dChild.FullName);//打印路径和文件名
// }
//}
//else
//{
// //没有子文件夹
//}
#endregion
/***********************************AVHRR***********************************/
LabelAvhrr:
{
#region "LabelAvhrr"
Console.WriteLine("请输入目录(至底):");
// string avhrrPath=@"E:\DATA02\AVHRR\1149891193\001";
string avhrrPath=Console.ReadLine();
//从字符串路径转化为目录信息
DirectoryInfo dirAvhrr= new DirectoryInfo(avhrrPath);
Console.WriteLine("目录\"{0}\"下的文件有:",avhrrPath);
/********fileNum记录了文件的个数********/
int fileNum = dirAvhrr.GetFiles().Length;
/********字符串数组fileNames记录了这些文件的文件名********/
string[] fileNames = new string[fileNum];
int filenumber = 0;
foreach (FileInfo dChild in dirAvhrr.GetFiles("*"))
{
//如果用GetFiles("*.txt"),那么全部txt文件会被显示
//Console.WriteLine(dChild.Name); //显示文件名
// Console.WriteLine(dChild.FullName);//显示路径和文件名
fileNames[filenumber] = dChild.Name;
filenumber++;
}
//测试信息:显示所有文件名
foreach (string myFileName in fileNames)
{
Console.WriteLine(myFileName);
}
/**********Split文件名,生成信息项**********/
//一维数组info1起TEMP作用:存储生成的信息项
//二维数组info2【文件序号,信息项序号】存储每个文件的每个信息项
char[] separator = { '.' };
string[] info1 = new string[10];
string[,] info2 = new string[fileNum, 10];
for (int ii = 0; ii < fileNum; ++ii)
{
info1 = fileNames[ii].Split(separator);
{
int numOfItem = info1.Length;
for (int jj = 0; jj < numOfItem;jj++ )
info2[ii, jj] = info1[jj];
}
}
#region "已注释:显示每个文件的信息项,查证是否正确"
//遍历二维数组 注意:不是数组的数组
//Console.WriteLine();
//Console.WriteLine("信息项如下所示:");
//foreach (string aa in info2)
//{
// Console.Write(aa+" ");
// if(aa=="WI"||aa=="GC")
// Console.WriteLine();
//}
#endregion
/******************Access数据库操作部分******************/
//建立Access数据库Metadata以及表AVHRR
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfo.mdb;Jet OLEDB:Engine Type=5");
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfo.mdb", null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = "AVHRR";
#region "已注释:键的添加"
// ADOX.Column column = new ADOX.Column();
// column.ParentCatalog = catalog;
// column.Name = "RecordID";
// column.Type = DataTypeEnum.adInteger;
// column.DefinedSize = 9;
//column.Properties["AutoIncrement"].Value = true;
// table.Columns.Append(column, DataTypeEnum.adInteger, 9);
// table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
// table.Columns.Append("CustomerName", DataTypeEnum.adVarWChar, 50);
// table.Columns.Append("Age", DataTypeEnum.adInteger, 9);
// table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);
#endregion
table.Columns.Append("Type", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("SubType", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Band", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Date", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("MaxLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MaxLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("Path", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("FileName", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Extension", DataTypeEnum.adVarWChar, 50);
catalog.Tables.Append(table);
/*************插入数据*************/
//循环遍历所有文件,将含有的信息项添加进Access数据库中
for (int ia = 0; ia < fileNum; ++ia)
{
//构造sql语句
string sql = "insert into AVHRR values ('AVHRR','null','null','null',+90,-90,+180,-180,'"+avhrrPath+"','"+fileNames[ia]+"','" + info2[ia, 7] + "')";
//string sql = "INSERT INTO AVHRR (Band,[Date],MaxLat,MinLat,MaxLon,MinLon) VALUES ('info2[0,2]','info2[0,3]','info2[0,4]','info2[0,5]','info2[0,6]','info2[0,7]')";
string dbpath = "E:\\MetaInfo\\MetaInfo.mdb"; //access数据库的路径
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbpath); //定义数据库连接对象
OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
con.Open(); //打开数据库连接
cmd.ExecuteNonQuery(); //执行Command命令
con.Close(); //关闭数据库连接
}
cn.Close();
}
#endregion
/***********************************MODIS***********************************/
LabelModis:
{
#region "LabelModis"
Console.WriteLine("请输入目录(至底):");
string inputPath = Console.ReadLine();
DirectoryInfo dirInput = new DirectoryInfo(inputPath);
/********fileNum记录了文件的个数********/
int fileNumOfCompressed = dirInput.GetFiles().Length;
/********字符串数组fileNamesModis记录了这些文件的文件名********/
string[] fileNamesOfCompressed = new string[fileNumOfCompressed];
int fileNumOfCompressed1 = 0;
foreach (FileInfo dChild in dirInput.GetFiles("*"))
{
//如果用GetFiles("*.txt"),那么全部txt文件会被显示
//Console.WriteLine(dChild.Name); //显示文件名
// Console.WriteLine(dChild.FullName);//显示路径和文件名
fileNamesOfCompressed[fileNumOfCompressed1] = dChild.Name;
fileNumOfCompressed1++;
}
//测试信息:显示所有压缩文件名
Console.WriteLine("所在路径:{0}", inputPath);
Console.WriteLine("该路径有压缩文件:");
foreach (string myFileName in fileNamesOfCompressed)
{
Console.WriteLine(myFileName);
}
//路径:inputPath,文件名:fileNamesModis[i], i从0到fileNumberModis-1
/////////////////////////读取压缩文件
List fileNames = new List();
//遍历每个tar.gz压缩文件
for (int imodisname = 0; imodisname < fileNumOfCompressed1; ++imodisname)
{
//fileNames存储了所有压缩文件里面的文件名
fileNames.AddRange(getModTarGzFileNames(fileNamesOfCompressed[imodisname], inputPath));
}
Console.WriteLine();
Console.WriteLine("所有压缩文件里面的文件有:");
foreach (string myFN in fileNames)
Console.WriteLine(myFN);
//提取MODIS、OMI、TMOS等传感器大类
//typeName存储了大类的名字
char[] sep = { '\\' };
List tabName = new List();
tabName.AddRange(inputPath.Split(sep));
string typeName = "";
for (int itabname = 0; itabname < tabName.Count; ++itabname)
{
if ("MODIS" == tabName[itabname])
typeName = "MODIS";
else if ("OMI" == tabName[itabname])
typeName = "OMI";
else if ("TMOS" == tabName[itabname])
typeName = "TMOS";
}
Console.WriteLine(typeName);
//第一次分割:按照“.”分割成 fileInfoItem
//List类型 fileNames存储了所有压缩文件里面的文件名
List fileInfoItem = new List();
char[] sepdot = { '.' };
foreach (string myFN in fileNames)
{
fileInfoItem.AddRange(myFN.Split(sepdot));
}
//格式名字:fileInfoItem[fileInfoItem.Count - 1]
Console.WriteLine(fileInfoItem[fileInfoItem.Count - 1]);
foreach (string iiiiii in fileInfoItem)
Console.WriteLine(iiiiii);
//判断日平均还是月平均
string averagemonthorday = "";
List pathItem = new List();
pathItem.AddRange(inputPath.Split('\\'));
for (int idayormonth = 0; idayormonth < pathItem.Count; ++idayormonth)
{
if ("monthly" == pathItem[idayormonth])
averagemonthorday = "monthly";
else if ("daily" == pathItem[idayormonth])
averagemonthorday = "daily";
}
Console.WriteLine("月平均还是日平均?");
Console.WriteLine(averagemonthorday);
string dateyyyymmdd = DayToDate(fileInfoItem[1]);
//若fileInfoItem含有“_”则进行二次分割:fileInfoSubItem
// fileInfoSubItem
/******************Access数据库操作部分******************/
//建立Access数据库Metadata以及表AVHRR
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfoMODIS.mdb;Jet OLEDB:Engine Type=5");
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfoMODIS.mdb", null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = typeName;
table.Columns.Append("Type", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("SubType", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Band", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Date", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("MaxLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MaxLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("Path", DataTypeEnum.adVarWChar, 150);
table.Columns.Append("FileName", DataTypeEnum.adVarWChar, 150);
table.Columns.Append("Extension", DataTypeEnum.adVarWChar, 50);
catalog.Tables.Append(table);
/*************插入数据*************/
//循环遍历所有文件,将含有的信息项添加进Access数据库中
for (int ia = 0; ia < fileNames.Count; ++ia)
{
//构造sql语句
string sql = "insert into MODIS values ('MODIS','" + averagemonthorday + "','null','" + dateyyyymmdd + "',+90,-90,+180,-180,'" + inputPath + "','" + fileNames[ia] + "','" + fileInfoItem[fileInfoItem.Count - 1] + "')";
//string sql = "INSERT INTO AVHRR (Band,[Date],MaxLat,MinLat,MaxLon,MinLon) VALUES ('info2[0,2]','info2[0,3]','info2[0,4]','info2[0,5]','info2[0,6]','info2[0,7]')";
string dbpath = "E:\\MetaInfo\\MetaInfoMODIS.mdb"; //access数据库的路径
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbpath); //定义数据库连接对象
OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
con.Open(); //打开数据库连接
cmd.ExecuteNonQuery(); //执行Command命令
con.Close(); //关闭数据库连接
}
cn.Close();
#endregion
}
LabelOmi:
{
#region "LabelOmi"
Console.WriteLine("请输入目录(至底):");
string inputPath = Console.ReadLine();
DirectoryInfo dirInput = new DirectoryInfo(inputPath);
/********fileNum记录了文件的个数********/
int fileNumOfCompressed = dirInput.GetFiles().Length;
/********字符串数组fileNamesModis记录了这些文件的文件名********/
string[] fileNamesOfCompressed = new string[fileNumOfCompressed];
int fileNumOfCompressed1 = 0;
foreach (FileInfo dChild in dirInput.GetFiles("*"))
{
//如果用GetFiles("*.txt"),那么全部txt文件会被显示
//Console.WriteLine(dChild.Name); //显示文件名
// Console.WriteLine(dChild.FullName);//显示路径和文件名
fileNamesOfCompressed[fileNumOfCompressed1] = dChild.Name;
fileNumOfCompressed1++;
}
//测试信息:显示所有压缩文件名
Console.WriteLine("所在路径:{0}", inputPath);
Console.WriteLine("该路径有压缩文件:");
foreach (string myFileName in fileNamesOfCompressed)
{
Console.WriteLine(myFileName);
}
//路径:inputPath,文件名:fileNamesModis[i], i从0到fileNumberModis-1
/////////////////////////读取压缩文件///////////////////
List fileNames = new List();
//遍历每个tar.gz压缩文件
for (int imodisname = 0; imodisname < fileNumOfCompressed1; ++imodisname)
{
//fileNames存储了所有压缩文件里面的文件名
fileNames.AddRange(getModTarGzFileNames(fileNamesOfCompressed[imodisname], inputPath));
}
Console.WriteLine();
Console.WriteLine("所有压缩文件里面的文件有:");
int fileNumber = 0;
foreach (string myFN in fileNames)
{
Console.WriteLine(myFN);
fileNumber++;
}
Console.WriteLine("文件个数:{0}", fileNumber);
//提取MODIS、OMI、TMOS等传感器大类
//typeName存储了大类的名字
char[] sep = { '\\' };
List tabName = new List();
tabName.AddRange(inputPath.Split(sep));
string typeName = "";
for (int itabname = 0; itabname < tabName.Count; ++itabname)
{
if ("MODIS" == tabName[itabname])
typeName = "MODIS";
else if ("OMI" == tabName[itabname])
typeName = "OMI";
else if ("TMOS" == tabName[itabname])
typeName = "TMOS";
}
Console.WriteLine("类型名字:{0}",typeName);
//第一次分割:按照“.”分割成 fileInfoItem
// List类型 fileNames存储了所有压缩文件里面的文件名
List fileInfoItem = new List();
char[] sepdot = { '.' };
foreach (string myFN in fileNames)
{
fileInfoItem.AddRange(myFN.Split(sepdot));
}
//格式名字:fileInfoItem[fileInfoItem.Count - 1]
Console.WriteLine("格式名字:{0}", fileInfoItem[fileInfoItem.Count - 1]);
//foreach (string iiiiii in fileInfoItem)
// Console.WriteLine(iiiiii);
//分割压缩文件名,提取年月日信息
List compressedFileInfoItem = new List();
int ix = 0;
string[,] comFileItemInfoItem = new string[fileNumOfCompressed, 2];
char[] separator = { '_' };
string[] info1 = new string[2];
string[] info2 = new string[fileNumOfCompressed];
for (int ii = 0; ii < fileNumOfCompressed; ++ii)
{
info1 = fileNamesOfCompressed[ii].Split(separator);
{
for (int jj = 0; jj < fileNumOfCompressed; jj++)
info2[ii] = info1[1];
}
}
//info2[0----fileNumOfCompressed-1]存储了以八位数字开始的字符串
string[] info3 = new string[3];
string[] info4 = new string[fileNumOfCompressed];
for (int ii = 0; ii < fileNumOfCompressed; ++ii)
{
info3 = info2[ii].Split('.');
{
for (int jj = 0; jj < fileNumOfCompressed; jj++)
info4[ii] = info3[0];
}
}
//info4[0----fileNumOfCompressed-1]存储了8位数
//foreach (string mystr in info4)
//{
// Console.WriteLine(mystr);
//}
string[] dateyyyymmdd = new string[fileNumOfCompressed];
for (int ixx = 0; ixx < fileNumOfCompressed; ++ixx)
{
char[] ch01 = info4[ixx].ToCharArray();
dateyyyymmdd[ixx] = ch01[0].ToString() + ch01[1].ToString() + ch01[2].ToString() + ch01[3].ToString() + "年";
dateyyyymmdd[ixx]+= ch01[4].ToString()+ ch01[5].ToString()+"月"+ ch01[6].ToString()+ ch01[7].ToString()+"日";
}
string[] dateyyyymmdd1 = new string[fileNumber];
for (int idate = 0,iidate=0; idate < fileNumOfCompressed;++idate )
{
iidate = idate;
dateyyyymmdd1[iidate*2] = dateyyyymmdd[idate];
dateyyyymmdd1[iidate*2+1] = dateyyyymmdd[idate];
}
foreach (string mydate in dateyyyymmdd1)
{
Console.WriteLine(mydate);
}
//若fileInfoItem含有“_”则进行二次分割:fileInfoSubItem
// fileInfoSubItem
/******************Access数据库操作部分******************/
//建立Access数据库Metadata以及表AVHRR
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfoOMI.mdb;Jet OLEDB:Engine Type=5");
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfoOMI.mdb", null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = typeName;
table.Columns.Append("Type", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("SubType", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Band", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Date", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("MaxLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MaxLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("Path", DataTypeEnum.adVarWChar, 150);
table.Columns.Append("FileName", DataTypeEnum.adVarWChar, 150);
table.Columns.Append("Extension", DataTypeEnum.adVarWChar, 50);
catalog.Tables.Append(table);
/*************插入数据*************/
//循环遍历所有文件,将含有的信息项添加进Access数据库中
for (int ia = 0; ia < fileNumber; ++ia)
{
//构造sql语句
string sql = "insert into OMI values ('OMI','null','null','" + dateyyyymmdd1[ia] + "',+90,-90,+180,-180,'" + inputPath + "','" + fileNames[ia] + "','hdf')";
//string sql = "INSERT INTO AVHRR (Band,[Date],MaxLat,MinLat,MaxLon,MinLon) VALUES ('info2[0,2]','info2[0,3]','info2[0,4]','info2[0,5]','info2[0,6]','info2[0,7]')";
string dbpath = "E:\\MetaInfo\\MetaInfoOMI.mdb"; //access数据库的路径
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbpath); //定义数据库连接对象
OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
con.Open(); //打开数据库连接
cmd.ExecuteNonQuery(); //执行Command命令
con.Close(); //关闭数据库连接
}
cn.Close();
}
#endregion
LabelTmos:
{
Console.WriteLine("请输入目录(至底):");
string inputPath = Console.ReadLine();
DirectoryInfo dirInput = new DirectoryInfo(inputPath);
/********fileNumOfCompressed记录了压缩文件的个数********/
int fileNumOfCompressed = dirInput.GetFiles().Length;
/********字符串数组fileNamesOfCompressed记录了这些压缩文件的名称********/
string[] fileNamesOfCompressed = new string[fileNumOfCompressed];
int fileNumOfCompressed1 = 0;
foreach (FileInfo dChild in dirInput.GetFiles("*"))
{
//如果用GetFiles("*.txt"),那么全部txt文件会被显示
//Console.WriteLine(dChild.Name); //显示文件名
// Console.WriteLine(dChild.FullName);//显示路径和文件名
fileNamesOfCompressed[fileNumOfCompressed1] = dChild.Name;
fileNumOfCompressed1++;
}
//测试信息:显示所有压缩文件名
Console.WriteLine("所在路径:{0}", inputPath);
Console.WriteLine("该路径有压缩文件:");
foreach (string myFileName in fileNamesOfCompressed)
{
Console.WriteLine(myFileName);
}
//路径:inputPath,文件名:fileNamesOfCompressed[i], i从0到fileNumOfCompressed-1
/////////////////////////读取压缩文件///////////////////
List fileNames = new List();
//遍历每个tar.gz压缩文件
for (int imodisname = 0; imodisname < fileNumOfCompressed1; ++imodisname)
{
//fileNames存储了所有压缩文件里面的文件名
fileNames.AddRange(getModTarGzFileNames(fileNamesOfCompressed[imodisname], inputPath));
}
Console.WriteLine();
Console.WriteLine("所有压缩文件里面的文件有:");
int fileNumber = 0;
foreach (string myFN in fileNames)
{
Console.WriteLine(myFN);
fileNumber++;
}
Console.WriteLine("文件个数:{0}", fileNumber);
//提取MODIS、OMI、TMOS等传感器大类
//typeName存储了大类的名字
char[] sep = { '\\' };
List tabName = new List();
tabName.AddRange(inputPath.Split(sep));
string typeName = "";
for (int itabname = 0; itabname < tabName.Count; ++itabname)
{
if ("MODIS" == tabName[itabname])
typeName = "MODIS";
else if ("OMI" == tabName[itabname])
typeName = "OMI";
else if ("TMOS" == tabName[itabname])
typeName = "TMOS";
}
Console.WriteLine("类型名字:{0}",typeName);
/******************Access数据库操作部分******************/
//建立Access数据库Metadata以及表AVHRR
ADOX.Catalog catalog = new Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfoTMOS.mdb;Jet OLEDB:Engine Type=5");
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MetaInfo\\MetaInfoTMOS.mdb", null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = typeName;
table.Columns.Append("Type", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("SubType", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Band", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Date", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("MaxLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLat", DataTypeEnum.adDouble, 50);
table.Columns.Append("MaxLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("MinLon", DataTypeEnum.adDouble, 50);
table.Columns.Append("Path", DataTypeEnum.adVarWChar, 150);
table.Columns.Append("FileName", DataTypeEnum.adVarWChar, 150);
table.Columns.Append("Extension", DataTypeEnum.adVarWChar, 50);
catalog.Tables.Append(table);
/*************插入数据*************/
//循环遍历所有文件,将含有的信息项添加进Access数据库中
for (int ia = 0; ia < fileNumber; ++ia)
{
//构造sql语句
string sql = "insert into TMOS values ('TMOS','null','null','null',+90,-90,+180,-180,'" + inputPath + "','" + fileNames[ia] + "','hdf')";
//string sql = "INSERT INTO AVHRR (Band,[Date],MaxLat,MinLat,MaxLon,MinLon) VALUES ('info2[0,2]','info2[0,3]','info2[0,4]','info2[0,5]','info2[0,6]','info2[0,7]')";
string dbpath = "E:\\MetaInfo\\MetaInfoTMOS.mdb"; //access数据库的路径
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbpath); //定义数据库连接对象
OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
con.Open(); //打开数据库连接
cmd.ExecuteNonQuery(); //执行Command命令
con.Close(); //关闭数据库连接
}
cn.Close();
}
Console.ReadKey();
}
}
}