C# xml压缩包不解压的情况下解析xml内容
2016-06-21 14:28
465 查看
string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip"; FileInfo fileInfo = new FileInfo(sourceFilePath); long length = fileInfo.Length; if (length == 0) { return; } using (ZipInputStream zip = new ZipInputStream(File.OpenRead(sourceFilePath))) { ZipEntry theEntry; string destinationDirPath = @"\\172.21.3.21\liung\"; while ((theEntry = zip.GetNextEntry()) != null) { destinationDirPath += theEntry.Name; using (FileStream streamWriter = new FileStream(destinationDirPath, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Write)) { byte[] bytes = new byte[zip.Length]; zip.Read(bytes, 0, bytes.Length); streamWriter.Write(bytes, 0, bytes.Length); } XmlTextReader xmlReader = new XmlTextReader(zip); while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.ToLower() == "fileheader") { string reportTime = xmlReader.GetAttribute("reportTime").Replace("24:00:00", "23:59:59"); string startTime = xmlReader.GetAttribute("startTime").Replace("24:00:00", "23:59:59"); string endTime = xmlReader.GetAttribute("endTime").Replace("24:00:00", "23:59:59"); break; } } } } Console.ReadKey(); using (GZipInputStream zip = new GZipInputStream(File.OpenRead(sourceFilePath))) { using (XmlTextReader xmlReader = new XmlTextReader(zip)) { while (xmlReader.Read()) { // hw没有period,暂时给一个默认值15 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.ToLower() == "fileheader") { string reportTime = xmlReader.GetAttribute("reportTime").Replace("24:00:00", "23:59:59"); string startTime = xmlReader.GetAttribute("startTime").Replace("24:00:00", "23:59:59"); string endTime = xmlReader.GetAttribute("endTime").Replace("24:00:00", "23:59:59"); break; } } } }