private const string WmfFile2Header = "02000900"; // Microsoft WMF format /// <summary> /// Save DOORS ole file in target format. If target format isn't supported the pure OLE file is stored. /// </summary> /// <param name="text"></param> /// <param name="filePath"></param> /// <param name="ignoreNotSupportedFiles"></param> /// <returns></returns> public static string Save(string text, string filePath, bool ignoreNotSupportedFiles = true) { if (text == null) { MessageBox.Show("", @"No string to convert found for OLE convertion, break"); return(""); } if (filePath == null) { MessageBox.Show("", @"No filepath to convert OLE file to convertion, break"); return(""); } // Check if header is available // if not use the file extension .error int startCfb = StartCfbFormat(text); int startWmf = StartWmfFormat(text); // no supported file found if (startCfb < 0 && startWmf < 0) { if (!ignoreNotSupportedFiles) { MessageBox.Show($@"File: '{filePath}' CFB CfbHeader should be: '{CfbHeader}' No CFB or WMF file found", @"File does not contain a CFB or WMF formatted file., break"); } string newFilePath = $@"{filePath}.error"; DirFiles.FileMove(filePath, newFilePath); return(newFilePath); } int start = startCfb > -1 ? startCfb : startWmf; int end = text.IndexOf('}', start); if (end < 0) { end = text.Length; } if (startCfb > -1) { // CFB File return(StoreTargetFileFromCfb(text, filePath, ignoreNotSupportedFiles, start, end)); } else { // WMF File return(StoreTargetFileFromWmf(text, filePath, start, end)); } }
/// <summary> /// Store the CFB file in target format /// </summary> /// <param name="text"></param> /// <param name="filePath"></param> /// <param name="ignoreNotSupportedFiles"></param> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> private static string StoreTargetFileFromCfb(string text, string filePath, bool ignoreNotSupportedFiles, int start, int end) { // find object type of OLE Regex rxObjectType = new Regex(@"objclass\s+([^}]*)}"); Match match = rxObjectType.Match(text.Substring(0, 70)); string typeText = ""; string ext = ""; if (match.Success) { typeText = match.Groups[1].Value; } // DOORS file types supported and tested string[] lTypes = { "AcroExch." }; //,"Excel.", "Word."}; string[] lExtensions = { ".pdf" }; //, "xlsx", "docx" }; string[] lCompoundFileStreamName = { "CONTENTS" }; int j = 0; string componentFileStreamName = ""; foreach (var type in lTypes) { if (typeText.Contains(type)) { ext = lExtensions[j]; componentFileStreamName = lCompoundFileStreamName[j]; break; } j += 1; } if (ext == "") { string newFilePath = $@"{filePath}.notSupported"; if (!ignoreNotSupportedFiles) { MessageBox.Show($@"File: '{filePath}' File type not supported: '{typeText}' Supported ole types: '{String.Join(", ", lTypes)}' Copied to: {DirFiles.GetMessageFromFile(newFilePath)} ", @"Can't convert *.ole to file, not supported type!"); } DirFiles.FileMove(filePath, newFilePath); return(newFilePath); } string filePathNew = Path.Combine(Path.GetDirectoryName(filePath), $"{Path.GetFileNameWithoutExtension(filePath)}{ext}"); StoreAsAsciiFile(text, start, end, filePathNew); // By DOORS supported file type if (componentFileStreamName != "") { using (CompoundFile cf = new CompoundFile(filePathNew)) { CFStream foundStream = cf.RootStorage.GetStream("CONTENTS"); DirFiles.WriteAllBytes(filePathNew, foundStream.GetData().ToArray()); } } if (DirFiles.FileDelete(filePath)) { return(filePathNew); } else { return(""); } }