Beispiel #1
0
        /// <summary>
        /// This method saves all the Word embedded binary objects from the <paramref name="inputFile"/> to the
        /// <see cref="outputFolder"/>
        /// </summary>
        /// <param name="inputFile">The binary Word file</param>
        /// <param name="outputFolder">The output folder</param>
        /// <returns></returns>
        /// <exception cref="OEFileIsPasswordProtected">Raised when the <see cref="inputFile"/> is password protected</exception>
        public static List <string> SaveToFolder(string inputFile, string outputFolder)
        {
            using (var compoundFile = new CompoundFile(inputFile))
            {
                if (IsPasswordProtected(compoundFile))
                {
                    throw new OEFileIsPasswordProtected("The file '" + Path.GetFileName(inputFile) +
                                                        "' is password protected");
                }

                var result = new List <string>();

                if (!compoundFile.RootStorage.ExistsStorage("ObjectPool"))
                {
                    return(result);
                }
                var objectPoolStorage = compoundFile.RootStorage.GetStorage("ObjectPool") as CFStorage;
                if (objectPoolStorage == null)
                {
                    return(result);
                }

                // Multiple objects are stored as children of the storage object
                foreach (var child in objectPoolStorage.Children)
                {
                    var childStorage = child as CFStorage;
                    if (childStorage == null)
                    {
                        continue;
                    }

                    // Get the objInfo stream to check if this is a linked file... if so then ignore it
                    var objInfo       = childStorage.GetStream("\x0003ObjInfo");
                    var objInfoStream = new ObjInfoStream(objInfo);

                    // We don't want to export linked objects and objects that are not shown as an icon...
                    // because these objects are already visible on the Word document
                    if (objInfoStream.Link || !objInfoStream.Icon)
                    {
                        continue;
                    }

                    var extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder);
                    if (!string.IsNullOrEmpty(extractedFileName))
                    {
                        result.Add(extractedFileName);
                    }
                }

                return(result);
            }
        }
Beispiel #2
0
        /// <summary>
        /// This method saves all the Word embedded binary objects from the <paramref name="inputFile"/> to the
        /// <paramref name="outputFolder"/>
        /// </summary>
        /// <param name="inputFile">The binary Word file</param>
        /// <param name="outputFolder">The output folder</param>
        /// <returns></returns>
        /// <exception cref="OEFileIsPasswordProtected">Raised when the <paramref name="inputFile"/> is password protected</exception>
        internal List <string> Extract(string inputFile, string outputFolder)
        {
            using (var compoundFile = new CompoundFile(inputFile))
            {
                var result = new List <string>();

                var objectPoolStorage = compoundFile.RootStorage.TryGetStorage("ObjectPool");
                if (objectPoolStorage == null)
                {
                    return(result);
                }

                Logger.WriteToLog("Object Pool stream found (Word)");

                void Entries(CFItem item)
                {
                    var childStorage = item as CFStorage;

                    if (childStorage == null)
                    {
                        return;
                    }

                    string extractedFileName;

                    if (childStorage.TryGetStream("\x0001Ole10Native") != null)
                    {
                        var compObj = childStorage.TryGetStream("\x0001CompObj");
                        if (compObj != null)
                        {
                            var compObjStream = new CompObjStream(compObj);
                            if (compObjStream.AnsiUserType == "OLE Package")
                            {
                                extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder, null);
                                if (!string.IsNullOrEmpty(extractedFileName))
                                {
                                    result.Add(extractedFileName);
                                }
                                return;
                            }
                        }

                        var objInfo = childStorage.TryGetStream("\x0003ObjInfo");
                        if (objInfo != null)
                        {
                            var objInfoStream = new ObjInfoStream(objInfo);
                            // We don't want to export linked objects and objects that are not shown as an icon...
                            // because these objects are already visible on the Word document
                            if (objInfoStream.Link || !objInfoStream.Icon)
                            {
                                return;
                            }
                        }

                        extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder, null);
                    }
                    else
                    {
                        // Get the objInfo stream to check if this is a linked file... if so then ignore it
                        var objInfo       = childStorage.GetStream("\x0003ObjInfo");
                        var objInfoStream = new ObjInfoStream(objInfo);

                        // We don't want to export linked objects and objects that are not shown as an icon...
                        // because these objects are already visible on the Word document
                        if (objInfoStream.Link || !objInfoStream.Icon)
                        {
                            return;
                        }
                        extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder);
                    }

                    if (!string.IsNullOrEmpty(extractedFileName))
                    {
                        result.Add(extractedFileName);
                    }
                }

                objectPoolStorage.VisitEntries(Entries, false);

                return(result);
            }
        }
Beispiel #3
0
        /// <summary>
        /// This method saves all the Word embedded binary objects from the <paramref name="inputFile"/> to the
        /// <see cref="outputFolder"/>
        /// </summary>
        /// <param name="inputFile">The binary Word file</param>
        /// <param name="outputFolder">The output folder</param>
        /// <returns></returns>
        /// <exception cref="OEFileIsPasswordProtected">Raised when the <see cref="inputFile"/> is password protected</exception>
        public static List <string> SaveToFolder(string inputFile, string outputFolder)
        {
            var fileName = Path.GetFileName(inputFile);

            using (var compoundFile = new CompoundFile(inputFile))
            {
                if (IsPasswordProtected(compoundFile, fileName))
                {
                    throw new OEFileIsPasswordProtected("The file '" + fileName +
                                                        "' is password protected");
                }

                var result = new List <string>();

                var objectPoolStorage = compoundFile.RootStorage.TryGetStorage("ObjectPool");
                if (objectPoolStorage == null)
                {
                    return(result);
                }

                Action <CFItem> entries = item =>
                {
                    var childStorage = item as CFStorage;
                    if (childStorage == null)
                    {
                        return;
                    }

                    string extractedFileName;

                    if (childStorage.TryGetStream("\x0001Ole10Native") != null)
                    {
                        var compObj = childStorage.TryGetStream("\x0001CompObj");
                        if (compObj != null)
                        {
                            var compObjStream = new CompObjStream(compObj);
                            if (compObjStream.AnsiUserType == "OLE Package")
                            {
                                extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder, null);
                                if (!string.IsNullOrEmpty(extractedFileName))
                                {
                                    result.Add(extractedFileName);
                                }
                                return;
                            }
                        }

                        var objInfo = childStorage.TryGetStream("\x0003ObjInfo");
                        if (objInfo != null)
                        {
                            var objInfoStream = new ObjInfoStream(objInfo);
                            // We don't want to export linked objects and objects that are not shown as an icon...
                            // because these objects are already visible on the Word document
                            if (objInfoStream.Link || !objInfoStream.Icon)
                            {
                                return;
                            }
                        }

                        extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder, null);
                    }
                    else
                    {
                        // Get the objInfo stream to check if this is a linked file... if so then ignore it
                        var objInfo       = childStorage.GetStream("\x0003ObjInfo");
                        var objInfoStream = new ObjInfoStream(objInfo);

                        // We don't want to export linked objects and objects that are not shown as an icon...
                        // because these objects are already visible on the Word document
                        if (objInfoStream.Link || !objInfoStream.Icon)
                        {
                            return;
                        }
                        extractedFileName = Extraction.SaveFromStorageNode(childStorage, outputFolder);
                    }

                    if (!string.IsNullOrEmpty(extractedFileName))
                    {
                        result.Add(extractedFileName);
                    }
                };

                objectPoolStorage.VisitEntries(entries, false);

                return(result);
            }
        }