Exemplo n.º 1
0
        /// <summary>
        /// Extracts all the embedded object from the Microsoft Office <paramref name="inputFile"/> to the
        /// <see cref="outputFolder"/> and returns the files with full path as a list of strings
        /// </summary>
        /// <param name="inputFile">The Microsoft Office file</param>
        /// <param name="outputFolder">The output folder</param>
        /// <returns>List with files or en empty list when there are nog embedded files</returns>
        /// <exception cref="ArgumentNullException">Raised when the <paramref name="inputFile"/> or <paramref name="outputFolder"/> is null or empty</exception>
        /// <exception cref="FileNotFoundException">Raised when the <sparamref name="inputFile"/> does not exist</exception>
        /// <exception cref="DirectoryNotFoundException">Raised when the <paramref name="outputFolder"/> does not exists</exception>
        /// <exception cref="OEFileIsCorrupt">Raised when the <paramref name="inputFile" /> is corrupt</exception>
        /// <exception cref="OEFileTypeNotSupported">Raised when the <paramref name="inputFile"/> is not supported</exception>
        /// <exception cref="OEFileIsPasswordProtected">Raised when the <paramref name="inputFile"/> is password protected</exception>
        public List <string> SaveToFolder(string inputFile, string outputFolder)
        {
            CheckFileNameAndOutputFolder(inputFile, outputFolder);

            var extension = GetExtension(inputFile);

            outputFolder = FileManager.CheckForBackSlash(outputFolder);

            switch (extension)
            {
            case ".ODT":
            case ".ODS":
            case ".ODP":
                return(ExtractFromOpenDocumentFormat(inputFile, outputFolder));

            case ".DOC":
            case ".DOT":
                // Word 97 - 2003
                return(Word.SaveToFolder(inputFile, outputFolder));

            case ".DOCM":
            case ".DOCX":
            case ".DOTM":
                // Word 2007 - 2013
                return(ExtractFromOfficeOpenXmlFormat(inputFile, "/word/embeddings/", outputFolder));

            case ".RTF":
                return(Rtf.SaveToFolder(inputFile, outputFolder));

            case ".XLS":
            case ".XLT":
            case ".XLW":
                // Excel 97 - 2003
                return(Excel.SaveToFolder(inputFile, outputFolder));

            case ".XLSB":
            case ".XLSM":
            case ".XLSX":
            case ".XLTM":
            case ".XLTX":
                // Excel 2007 - 2013
                return(ExtractFromOfficeOpenXmlFormat(inputFile, "/xl/embeddings/", outputFolder));

            case ".POT":
            case ".PPT":
            case ".PPS":
                // PowerPoint 97 - 2003
                return(PowerPoint.SaveToFolder(inputFile, outputFolder));

            case ".POTM":
            case ".POTX":
            case ".PPSM":
            case ".PPSX":
            case ".PPTM":
            case ".PPTX":
                // PowerPoint 2007 - 2013
                return(ExtractFromOfficeOpenXmlFormat(inputFile, "/ppt/embeddings/", outputFolder));

            default:
                throw new OEFileTypeNotSupported("The file '" + Path.GetFileName(inputFile) +
                                                 "' is not supported, only .ODT, .DOC, .DOCM, .DOCX, .DOT, .DOTM, .RTF, .XLS, .XLSB, .XLSM, .XLSX, .XLT, " +
                                                 ".XLTM, .XLTX, .XLW, .POT, .PPT, .POTM, .POTX, .PPS, .PPSM, .PPSX, .PPTM and .PPTX are supported");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extracts all the embedded object from the Microsoft Office <paramref name="inputFile"/> to the
        /// <see cref="outputFolder"/> and returns the files with full path as a list of strings
        /// </summary>
        /// <param name="inputFile">The Microsoft Office file</param>
        /// <param name="outputFolder">The output folder</param>
        /// <param name="logStream">When set then logging is written to this stream</param>
        /// <returns>List with files or en empty list when there are nog embedded files</returns>
        /// <exception cref="ArgumentNullException">Raised when the <paramref name="inputFile"/> or <paramref name="outputFolder"/> is null or empty</exception>
        /// <exception cref="FileNotFoundException">Raised when the <sparamref name="inputFile"/> does not exist</exception>
        /// <exception cref="DirectoryNotFoundException">Raised when the <paramref name="outputFolder"/> does not exists</exception>
        /// <exception cref="OEFileIsCorrupt">Raised when the <paramref name="inputFile" /> is corrupt</exception>
        /// <exception cref="OEFileTypeNotSupported">Raised when the <paramref name="inputFile"/> is not supported</exception>
        /// <exception cref="OEFileIsPasswordProtected">Raised when the <paramref name="inputFile"/> is password protected</exception>
        public List <string> SaveToFolder(string inputFile, string outputFolder, Stream logStream = null)
        {
            if (logStream != null)
            {
                Logger.LogStream = logStream;
            }

            CheckFileNameAndOutputFolder(inputFile, outputFolder);

            var extension = GetExtension(inputFile);

            Logger.WriteToLog($"Checking if file '{inputFile}' contains any embeded objects");

            outputFolder = FileManager.CheckForBackSlash(outputFolder);

            try
            {
                switch (extension)
                {
                case ".ODT":
                case ".ODS":
                case ".ODP":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    return(ExtractFromOpenDocumentFormat(inputFile, outputFolder));
                }

                case ".DOC":
                case ".DOT":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    // Word 97 - 2003
                    return(Word.SaveToFolder(inputFile, outputFolder));
                }

                case ".DOCM":
                case ".DOCX":
                case ".DOTM":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    // Word 2007 - 2013
                    return(ExtractFromOfficeOpenXmlFormat(inputFile, "/word/embeddings/", outputFolder));
                }

                case ".RTF":
                    return(Rtf.SaveToFolder(inputFile, outputFolder));

                case ".XLS":
                case ".XLT":
                case ".XLW":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    // Excel 97 - 2003
                    return(Excel.SaveToFolder(inputFile, outputFolder));
                }

                case ".XLSB":
                case ".XLSM":
                case ".XLSX":
                case ".XLTM":
                case ".XLTX":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    // Excel 2007 - 2013
                    return(ExtractFromOfficeOpenXmlFormat(inputFile, "/xl/embeddings/", outputFolder));
                }

                case ".POT":
                case ".PPT":
                case ".PPS":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    // PowerPoint 97 - 2003
                    return(PowerPoint.SaveToFolder(inputFile, outputFolder));
                }

                case ".POTM":
                case ".POTX":
                case ".PPSM":
                case ".PPSX":
                case ".PPTM":
                case ".PPTX":
                {
                    var result = _passwordProtectedChecker.IsFileProtected(inputFile);
                    if (result.Protected)
                    {
                        ThrowPasswordProtected(inputFile);
                    }

                    // PowerPoint 2007 - 2013
                    return(ExtractFromOfficeOpenXmlFormat(inputFile, "/ppt/embeddings/", outputFolder));
                }

                default:
                    var message = "The file '" + Path.GetFileName(inputFile) +
                                  "' is not supported, only .ODT, .DOC, .DOCM, .DOCX, .DOT, .DOTM, .RTF, .XLS, .XLSB, .XLSM, .XLSX, .XLT, " +
                                  ".XLTM, .XLTX, .XLW, .POT, .PPT, .POTM, .POTX, .PPS, .PPSM, .PPSX, .PPTM and .PPTX are supported";

                    Logger.WriteToLog(message);
                    throw new OEFileTypeNotSupported(message);
                }
            }
            catch (CFCorruptedFileException)
            {
                throw new OEFileIsCorrupt("The file '" + Path.GetFileName(inputFile) + "' is corrupt");
            }
        }