public static MmsPart Create(MmsNode parentNode, XElement partNode, DirectoryInfo outputDir, int partIndex) { XAttribute typeAttr = partNode.Attributes("ct").FirstOrDefault(); if (typeAttr == null) { throw new ApplicationException("Missing part type attribute"); } if ((typeAttr.Value == "image/jpeg") || (typeAttr.Value == "image/png")) { MmsPartImage ImgResult = new MmsPartImage(); ImgResult.Initialize(parentNode, partNode, outputDir, partIndex); return(ImgResult); } if (typeAttr.Value == "text/plain") { MmsPartText TxtResult = new MmsPartText(); TxtResult.Initialize(partNode); return(TxtResult); } Console.WriteLine("Unknown part type: {0}", typeAttr.Value); MmsPart BaseResult = new MmsPart(); BaseResult.Initialize(partNode); return(BaseResult); }
public void Initialize(MmsNode parentNode, XElement node, DirectoryInfo outputDir, int imageIndex) { base.Initialize(node); byte[] byteBuffer = new byte[4096]; if (parentNode.MessageDate.HasValue) { DirectoryInfo ImgDirectory = new DirectoryInfo(Path.Combine(outputDir.FullName, parentNode.MessageDate.Value.ToString("yyyy-MM-dd"))); if (!ImgDirectory.Exists) { ImgDirectory.Create(); } FileName = Path.Combine(ImgDirectory.FullName, string.Format("{0}-{1}.{2}", parentNode.MessageDate.Value.ToString("yyyyMMdd-HHmmss"), imageIndex, GetExtension())); } else { DirectoryInfo ImgDirectory = new DirectoryInfo(Path.Combine(outputDir.FullName, "Unknown")); if (!ImgDirectory.Exists) { ImgDirectory.Create(); } FileName = Path.Combine(ImgDirectory.FullName, string.Format("{0}.jpg", Guid.NewGuid().ToString("d"))); } foreach (XAttribute tmpAttr in node.Attributes()) { if (tmpAttr.Name == "data") { using (MemoryStream SourceMS = new MemoryStream(Encoding.UTF8.GetBytes(tmpAttr.Value))) { using (CryptoStream CS = new CryptoStream(SourceMS, new FromBase64Transform(), CryptoStreamMode.Read)) { using (FileStream imageFS = new FileStream(FileName, FileMode.Create, FileAccess.Write)) { int bytesRead = CS.Read(byteBuffer, 0, byteBuffer.Length); while (bytesRead > 0) { imageFS.Write(byteBuffer, 0, bytesRead); bytesRead = CS.Read(byteBuffer, 0, byteBuffer.Length); } } } } } } }
public static void Main(string[] args) { IBackupFileSource fileSource = new LocalBackupFileSource(@"C:\Temp"); DirectoryInfo OutputDir = new DirectoryInfo(Path.Combine("C:\\", "Temp", "SMSParseTest", "Output")); Dictionary <string, List <MessageBase> > Messages = new Dictionary <string, List <MessageBase> >(); foreach (FileInfo sourceFile in fileSource.GetFiles()) { using (FileStream FS = sourceFile.OpenRead()) { using (StreamReader SR = new StreamReader(FS, Encoding.UTF8)) { using (XmlReader XR = XmlReader.Create( FS, new XmlReaderSettings() { IgnoreComments = true, IgnoreProcessingInstructions = true, IgnoreWhitespace = true, CheckCharacters = false })) { XR.MoveToContent(); while (XR.Read()) { if (XR.NodeType == XmlNodeType.Element) { if (XR.Name == "sms") { XElement SmsElement = XNode.ReadFrom(XR) as XElement; if (SmsElement != null) { SmsNode Result = new SmsNode(); Result.Initialize(SmsElement, OutputDir); if (!string.IsNullOrEmpty(Result.Address)) { if (!Messages.ContainsKey(Result.Address)) { Messages.Add(Result.Address, new List <MessageBase>()); } Messages[Result.Address].Add(Result); } } } else if (XR.Name == "mms") { XElement MmsElement = XNode.ReadFrom(XR) as XElement; if (MmsElement != null) { MmsNode Result = new MmsNode(); Result.Initialize(MmsElement, OutputDir); if (!string.IsNullOrEmpty(Result.Address)) { if (!Messages.ContainsKey(Result.Address)) { Messages.Add(Result.Address, new List <MessageBase>()); } Messages[Result.Address].Add(Result); } } } } } } } } } if (!OutputDir.Exists) { OutputDir.Create(); } foreach (string key in Messages.Keys) { using (FileStream outputFS = new FileStream(Path.Combine(OutputDir.FullName, string.Format("SMSLog - {0}.txt", key)), FileMode.Create, FileAccess.Write)) { using (StreamWriter outputSW = new StreamWriter(outputFS)) { Messages[key].Sort(Comparer <MessageBase> .Create((tmpNode1, tmpNode2) => { if ((tmpNode1.MessageDate.HasValue) && (tmpNode2.MessageDate.HasValue)) { return(tmpNode1.MessageDate.Value.CompareTo(tmpNode2.MessageDate.Value)); } else if (tmpNode1.MessageDate.HasValue) { return(-1); } else if (tmpNode2.MessageDate.HasValue) { return(1); } else { return(0); } })); Messages[key].ForEach((node) => outputSW.WriteLine(node.ToString())); } } // Write the images out to the file } }