Exemple #1
0
        /// <summary>
        /// Extracts all the embedded object from the OpenDocument <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 OpenDocument format 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="OEFileIsPasswordProtected">Raised when the OpenDocument format file is password protected</exception>
        internal List <string> ExtractFromOpenDocumentFormat(string inputFile, string outputFolder)
        {
            var result = new List <string>();

            var zipFile = new ZipFile(inputFile);

            // Check if the file is password protected
            var manifestEntry = zipFile.FindEntry("META-INF/manifest.xml", true);

            if (manifestEntry != -1)
            {
                using (var manifestEntryStream = zipFile.GetInputStream(manifestEntry))
                    using (var manifestEntryMemoryStream = new MemoryStream())
                    {
                        manifestEntryStream.CopyTo(manifestEntryMemoryStream);
                        manifestEntryMemoryStream.Position = 0;
                        using (var streamReader = new StreamReader(manifestEntryMemoryStream))
                        {
                            var manifest = streamReader.ReadToEnd();
                            if (manifest.ToUpperInvariant().Contains("ENCRYPTION-DATA"))
                            {
                                throw new OEFileIsPasswordProtected("The file '" + Path.GetFileName(inputFile) +
                                                                    "' is password protected");
                            }
                        }
                    }
            }

            foreach (ZipEntry zipEntry in zipFile)
            {
                if (!zipEntry.IsFile)
                {
                    continue;
                }
                if (zipEntry.IsCrypted)
                {
                    throw new OEFileIsPasswordProtected("The file '" + Path.GetFileName(inputFile) +
                                                        "' is password protected");
                }

                var name = zipEntry.Name.ToUpperInvariant();
                if (!name.StartsWith("OBJECT") || name.Contains("/"))
                {
                    continue;
                }

                string fileName = null;

                var objectReplacementFileIndex = zipFile.FindEntry("ObjectReplacements/" + name, true);
                if (objectReplacementFileIndex != -1)
                {
                    fileName = Extraction.GetFileNameFromObjectReplacementFile(zipFile, objectReplacementFileIndex);
                }

                using (var zipEntryStream = zipFile.GetInputStream(zipEntry))
                    using (var zipEntryMemoryStream = new MemoryStream())
                    {
                        zipEntryStream.CopyTo(zipEntryMemoryStream);

                        using (var compoundFile = new CompoundFile(zipEntryMemoryStream))
                            result.Add(Extraction.SaveFromStorageNode(compoundFile.RootStorage, outputFolder, fileName));
                    }
            }

            return(result);
        }
        /// <summary>
        /// Extracts all the embedded object from the OpenDocument <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 OpenDocument format 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="OEFileIsPasswordProtected">Raised when the OpenDocument format file is password protected</exception>
        internal List <string> ExtractFromOpenDocumentFormat(string inputFile, string outputFolder)
        {
            Logger.WriteToLog("The file is of type 'Open document format'");

            var result  = new List <string>();
            var zipFile = SharpCompress.Archives.Zip.ZipArchive.Open(inputFile);

            // Check if the file is password protected
            var manifestEntry = FindEntryByName(zipFile, "META-INF/manifest.xml");

            if (manifestEntry != null)
            {
                using (var manifestEntryStream = manifestEntry.OpenEntryStream())
                    using (var manifestEntryMemoryStream = new MemoryStream())
                    {
                        manifestEntryStream.CopyTo(manifestEntryMemoryStream);
                        manifestEntryMemoryStream.Position = 0;
                        using (var streamReader = new StreamReader(manifestEntryMemoryStream))
                        {
                            var manifest = streamReader.ReadToEnd();
                            if (manifest.ToUpperInvariant().Contains("ENCRYPTION-DATA"))
                            {
                                throw new OEFileIsPasswordProtected("The file '" + Path.GetFileName(inputFile) +
                                                                    "' is password protected");
                            }
                        }
                    }
            }

            foreach (var zipEntry in zipFile.Entries)
            {
                if (zipEntry.IsDirectory)
                {
                    continue;
                }
                if (zipEntry.IsEncrypted)
                {
                    throw new OEFileIsPasswordProtected("The file '" + Path.GetFileName(inputFile) +
                                                        "' is password protected");
                }

                var name = zipEntry.Key.ToUpperInvariant();
                if (!name.StartsWith("OBJECT") || name.Contains("/"))
                {
                    continue;
                }

                string fileName = null;

                var objectReplacementFile = FindEntryByName(zipFile, "ObjectReplacements/" + name);
                if (objectReplacementFile != null)
                {
                    fileName = Extraction.GetFileNameFromObjectReplacementFile(objectReplacementFile);
                }

                Logger.WriteToLog($"Extracting embedded object '{fileName}'");

                using (var zipEntryStream = zipEntry.OpenEntryStream())
                    using (var zipEntryMemoryStream = new MemoryStream())
                    {
                        zipEntryStream.CopyTo(zipEntryMemoryStream);

                        using (var compoundFile = new CompoundFile(zipEntryMemoryStream))
                            result.Add(Extraction.SaveFromStorageNode(compoundFile.RootStorage, outputFolder, fileName));
                    }
            }

            return(result);
        }