|
| 1 | +/* |
| 2 | + * Media Extractor is an application to preview and extract packed media in Microsoft Office files (e.g. Word, PowerPoint or Excel documents) |
| 3 | + * Copyright Raphael Stoeckli © 2020 |
| 4 | + * This program is licensed under the MIT License. |
| 5 | + * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | + |
| 8 | +using SevenZipExtractor; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.IO; |
| 12 | + |
| 13 | +namespace MediaExtractor |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Class to resolve archives either by the file name or the stream (guessed) |
| 17 | + /// </summary> |
| 18 | + public static class ArchiveResolver |
| 19 | + { |
| 20 | + |
| 21 | + private static Dictionary<string, SevenZipFormat> archiveFormats = null; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Dictionary of supported archive formats |
| 25 | + /// </summary> |
| 26 | + public static Dictionary<string, SevenZipFormat> ArchiveFormats |
| 27 | + { |
| 28 | + get |
| 29 | + { |
| 30 | + if (archiveFormats == null) |
| 31 | + { |
| 32 | + GetFormats(); |
| 33 | + } |
| 34 | + return archiveFormats; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Singleton initializer to get the supported archive formats |
| 40 | + /// </summary> |
| 41 | + private static void GetFormats() |
| 42 | + { |
| 43 | + SevenZipFormat[] formats = Enum.GetValues(typeof(SevenZipFormat)) as SevenZipFormat[]; |
| 44 | + archiveFormats = new Dictionary<string, SevenZipFormat>(formats.Length); |
| 45 | + foreach (SevenZipFormat format in formats) |
| 46 | + { |
| 47 | + archiveFormats.Add(Enum.GetName(typeof(SevenZipFormat), format).ToLower(), format); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Method to open an archive, initially by its file extension |
| 53 | + /// </summary> |
| 54 | + /// <param name="stream">Memory stream of the archive</param> |
| 55 | + /// <param name="extension">File extension of the archive</param> |
| 56 | + /// <returns></returns> |
| 57 | + public static ArchiveFile Open(MemoryStream stream, string extension) |
| 58 | + { |
| 59 | + ArchiveFile archive; |
| 60 | + string error; |
| 61 | + if (ArchiveFormats.ContainsKey(extension.ToLower())) |
| 62 | + { |
| 63 | + if (OpenArchive(ref stream, ArchiveFormats[extension.ToLower()], out archive, out error)) |
| 64 | + { |
| 65 | + return archive; |
| 66 | + } |
| 67 | + } |
| 68 | + if (TryOpen(ref stream, out archive, out error)) |
| 69 | + { |
| 70 | + return archive; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + throw new IOException(error); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Method to try opening an archive without defined format |
| 80 | + /// </summary> |
| 81 | + /// <param name="stream">Memory stream of the archive</param> |
| 82 | + /// <param name="archive">Opened archive as out parameter. May be null in case of an error</param> |
| 83 | + /// <param name="error">Error message. May be empty, if no error occurred</param> |
| 84 | + /// <returns>True if the extraction was successful, otherwise false</returns> |
| 85 | + private static bool TryOpen(ref MemoryStream stream, out ArchiveFile archive, out string error) |
| 86 | + { |
| 87 | + ArchiveFile arch; |
| 88 | + string err; |
| 89 | + if (OpenArchive(ref stream, SevenZipFormat.Zip, out arch, out err)) |
| 90 | + { |
| 91 | + archive = arch; |
| 92 | + error = string.Empty; |
| 93 | + return true; |
| 94 | + } |
| 95 | + foreach (KeyValuePair<string, SevenZipFormat> format in ArchiveFormats) |
| 96 | + { |
| 97 | + if (format.Value == SevenZipFormat.Zip) |
| 98 | + { |
| 99 | + continue; // Already checked |
| 100 | + } |
| 101 | + if (OpenArchive(ref stream, format.Value, out arch, out err)) |
| 102 | + { |
| 103 | + archive = arch; |
| 104 | + error = string.Empty; |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + error = err; |
| 109 | + archive = null; |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Method to try opening an archive, based on an archive type |
| 115 | + /// </summary> |
| 116 | + /// <param name="stream">Memory stream of the archive</param> |
| 117 | + /// <param name="format">Predefined archive format</param> |
| 118 | + /// <param name="archive">Opened archive as out parameter. May be null in case of an error</param> |
| 119 | + /// <param name="error">Error message. May be empty, if no error occurred</param> |
| 120 | + /// <returns>True if the extraction was successful, otherwise false</returns> |
| 121 | + private static bool OpenArchive(ref MemoryStream stream, SevenZipFormat format, out ArchiveFile archive, out string error) |
| 122 | + { |
| 123 | + try |
| 124 | + { |
| 125 | + archive = new ArchiveFile(stream, format); |
| 126 | + error = string.Empty; |
| 127 | + return true; |
| 128 | + } |
| 129 | + catch (Exception ex) |
| 130 | + { |
| 131 | + archive = null; |
| 132 | + stream.Position = 0; |
| 133 | + error = ex.Message; |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | +} |
0 commit comments