LibraryManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. namespace SevenZip
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Diagnostics;
  7. #if NET472 || NETSTANDARD2_0
  8. using System.Security.Permissions;
  9. #endif
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. #if UNMANAGED
  15. /// <summary>
  16. /// 7-zip library low-level wrapper.
  17. /// </summary>
  18. internal static class SevenZipLibraryManager
  19. {
  20. /// <summary>
  21. /// Synchronization root for all locking.
  22. /// </summary>
  23. private static readonly object SyncRoot = new object();
  24. /// <summary>
  25. /// Path to the 7-zip dll.
  26. /// </summary>
  27. /// <remarks>7zxa.dll supports only decoding from .7z archives.
  28. /// Features of 7za.dll:
  29. /// - Supporting 7z format;
  30. /// - Built encoders: LZMA, PPMD, BCJ, BCJ2, COPY, AES-256 Encryption.
  31. /// - Built decoders: LZMA, PPMD, BCJ, BCJ2, COPY, AES-256 Encryption, BZip2, Deflate.
  32. /// 7z.dll (from the 7-zip distribution) supports every InArchiveFormat for encoding and decoding.
  33. /// </remarks>
  34. private static string _libraryFileName;
  35. private static string DetermineLibraryFilePath()
  36. {
  37. if (string.IsNullOrEmpty(Assembly.GetExecutingAssembly().Location))
  38. {
  39. return null;
  40. }
  41. return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Environment.Is64BitProcess ? "7z64.dll" : "7z.dll");
  42. }
  43. /// <summary>
  44. /// 7-zip library handle.
  45. /// </summary>
  46. private static IntPtr _modulePtr;
  47. /// <summary>
  48. /// 7-zip library features.
  49. /// </summary>
  50. private static LibraryFeature? _features;
  51. private static Dictionary<object, Dictionary<InArchiveFormat, IInArchive>> _inArchives;
  52. private static Dictionary<object, Dictionary<OutArchiveFormat, IOutArchive>> _outArchives;
  53. private static int _totalUsers;
  54. private static bool? _modifyCapable;
  55. private static void InitUserInFormat(object user, InArchiveFormat format)
  56. {
  57. if (!_inArchives.ContainsKey(user))
  58. {
  59. _inArchives.Add(user, new Dictionary<InArchiveFormat, IInArchive>());
  60. }
  61. if (!_inArchives[user].ContainsKey(format))
  62. {
  63. _inArchives[user].Add(format, null);
  64. _totalUsers++;
  65. }
  66. }
  67. private static void InitUserOutFormat(object user, OutArchiveFormat format)
  68. {
  69. if (!_outArchives.ContainsKey(user))
  70. {
  71. _outArchives.Add(user, new Dictionary<OutArchiveFormat, IOutArchive>());
  72. }
  73. if (!_outArchives[user].ContainsKey(format))
  74. {
  75. _outArchives[user].Add(format, null);
  76. _totalUsers++;
  77. }
  78. }
  79. private static void Init()
  80. {
  81. _inArchives = new Dictionary<object, Dictionary<InArchiveFormat, IInArchive>>();
  82. _outArchives = new Dictionary<object, Dictionary<OutArchiveFormat, IOutArchive>>();
  83. }
  84. /// <summary>
  85. /// Loads the 7-zip library if necessary and adds user to the reference list
  86. /// </summary>
  87. /// <param name="user">Caller of the function</param>
  88. /// <param name="format">Archive format</param>
  89. public static void LoadLibrary(object user, Enum format)
  90. {
  91. lock (SyncRoot)
  92. {
  93. if (_inArchives == null || _outArchives == null)
  94. {
  95. Init();
  96. }
  97. if (_modulePtr == IntPtr.Zero)
  98. {
  99. if (_libraryFileName == null)
  100. {
  101. _libraryFileName = DetermineLibraryFilePath();
  102. }
  103. if (!File.Exists(_libraryFileName))
  104. {
  105. throw new SevenZipLibraryException("DLL file does not exist.");
  106. }
  107. if ((_modulePtr = NativeMethods.LoadLibrary(_libraryFileName)) == IntPtr.Zero)
  108. {
  109. throw new SevenZipLibraryException($"failed to load library from \"{_libraryFileName}\".");
  110. }
  111. if (NativeMethods.GetProcAddress(_modulePtr, "GetHandlerProperty") == IntPtr.Zero)
  112. {
  113. NativeMethods.FreeLibrary(_modulePtr);
  114. throw new SevenZipLibraryException("library is invalid.");
  115. }
  116. }
  117. if (format is InArchiveFormat archiveFormat)
  118. {
  119. InitUserInFormat(user, archiveFormat);
  120. return;
  121. }
  122. if (format is OutArchiveFormat outArchiveFormat)
  123. {
  124. InitUserOutFormat(user, outArchiveFormat);
  125. return;
  126. }
  127. throw new ArgumentException($"Enum {format} is not a valid archive format attribute!");
  128. }
  129. }
  130. /// <summary>
  131. /// Gets the value indicating whether the library supports modifying archives.
  132. /// </summary>
  133. public static bool ModifyCapable
  134. {
  135. get
  136. {
  137. lock (SyncRoot)
  138. {
  139. if (!_modifyCapable.HasValue)
  140. {
  141. if (_libraryFileName == null)
  142. {
  143. _libraryFileName = DetermineLibraryFilePath();
  144. }
  145. var dllVersionInfo = FileVersionInfo.GetVersionInfo(_libraryFileName);
  146. _modifyCapable = dllVersionInfo.FileMajorPart >= 9;
  147. }
  148. return _modifyCapable.Value;
  149. }
  150. }
  151. }
  152. static readonly string Namespace = Assembly.GetExecutingAssembly().GetManifestResourceNames()[0].Split('.')[0];
  153. private static string GetResourceString(string str)
  154. {
  155. return Namespace + ".arch." + str;
  156. }
  157. private static bool ExtractionBenchmark(string archiveFileName, Stream outStream, ref LibraryFeature? features, LibraryFeature testedFeature)
  158. {
  159. var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceString(archiveFileName));
  160. try
  161. {
  162. using (var extractor = new SevenZipExtractor(stream))
  163. {
  164. extractor.ExtractFile(0, outStream);
  165. }
  166. }
  167. catch (Exception)
  168. {
  169. return false;
  170. }
  171. features |= testedFeature;
  172. return true;
  173. }
  174. private static bool CompressionBenchmark(Stream inStream, Stream outStream, OutArchiveFormat format, CompressionMethod method, ref LibraryFeature? features, LibraryFeature testedFeature)
  175. {
  176. try
  177. {
  178. var compressor = new SevenZipCompressor { ArchiveFormat = format, CompressionMethod = method };
  179. compressor.CompressStream(inStream, outStream);
  180. }
  181. catch (Exception)
  182. {
  183. return false;
  184. }
  185. features |= testedFeature;
  186. return true;
  187. }
  188. public static LibraryFeature CurrentLibraryFeatures
  189. {
  190. get
  191. {
  192. lock (SyncRoot)
  193. {
  194. if (_features.HasValue)
  195. {
  196. return _features.Value;
  197. }
  198. _features = LibraryFeature.None;
  199. #region Benchmark
  200. #region Extraction features
  201. using (var outStream = new MemoryStream())
  202. {
  203. ExtractionBenchmark("Test.lzma.7z", outStream, ref _features, LibraryFeature.Extract7z);
  204. ExtractionBenchmark("Test.lzma2.7z", outStream, ref _features, LibraryFeature.Extract7zLZMA2);
  205. var i = 0;
  206. if (ExtractionBenchmark("Test.bzip2.7z", outStream, ref _features, _features.Value))
  207. {
  208. i++;
  209. }
  210. if (ExtractionBenchmark("Test.ppmd.7z", outStream, ref _features, _features.Value))
  211. {
  212. i++;
  213. if (i == 2 && (_features & LibraryFeature.Extract7z) != 0 &&
  214. (_features & LibraryFeature.Extract7zLZMA2) != 0)
  215. {
  216. _features |= LibraryFeature.Extract7zAll;
  217. }
  218. }
  219. ExtractionBenchmark("Test.rar", outStream, ref _features, LibraryFeature.ExtractRar);
  220. ExtractionBenchmark("Test.tar", outStream, ref _features, LibraryFeature.ExtractTar);
  221. ExtractionBenchmark("Test.txt.bz2", outStream, ref _features, LibraryFeature.ExtractBzip2);
  222. ExtractionBenchmark("Test.txt.gz", outStream, ref _features, LibraryFeature.ExtractGzip);
  223. ExtractionBenchmark("Test.txt.xz", outStream, ref _features, LibraryFeature.ExtractXz);
  224. ExtractionBenchmark("Test.zip", outStream, ref _features, LibraryFeature.ExtractZip);
  225. }
  226. #endregion
  227. #region Compression features
  228. using (var inStream = new MemoryStream())
  229. {
  230. inStream.Write(Encoding.UTF8.GetBytes("Test"), 0, 4);
  231. using (var outStream = new MemoryStream())
  232. {
  233. CompressionBenchmark(inStream, outStream,
  234. OutArchiveFormat.SevenZip, CompressionMethod.Lzma,
  235. ref _features, LibraryFeature.Compress7z);
  236. CompressionBenchmark(inStream, outStream,
  237. OutArchiveFormat.SevenZip, CompressionMethod.Lzma2,
  238. ref _features, LibraryFeature.Compress7zLZMA2);
  239. var i = 0;
  240. if (_features != null && CompressionBenchmark(inStream, outStream,
  241. OutArchiveFormat.SevenZip, CompressionMethod.BZip2,
  242. ref _features, _features.Value))
  243. {
  244. i++;
  245. }
  246. if (_features != null && CompressionBenchmark(inStream, outStream,
  247. OutArchiveFormat.SevenZip, CompressionMethod.Ppmd,
  248. ref _features, _features.Value))
  249. {
  250. i++;
  251. if (i == 2 && (_features & LibraryFeature.Compress7z) != 0 &&
  252. (_features & LibraryFeature.Compress7zLZMA2) != 0)
  253. {
  254. _features |= LibraryFeature.Compress7zAll;
  255. }
  256. }
  257. CompressionBenchmark(inStream, outStream,
  258. OutArchiveFormat.Zip, CompressionMethod.Default,
  259. ref _features, LibraryFeature.CompressZip);
  260. CompressionBenchmark(inStream, outStream,
  261. OutArchiveFormat.BZip2, CompressionMethod.Default,
  262. ref _features, LibraryFeature.CompressBzip2);
  263. CompressionBenchmark(inStream, outStream,
  264. OutArchiveFormat.GZip, CompressionMethod.Default,
  265. ref _features, LibraryFeature.CompressGzip);
  266. CompressionBenchmark(inStream, outStream,
  267. OutArchiveFormat.Tar, CompressionMethod.Default,
  268. ref _features, LibraryFeature.CompressTar);
  269. CompressionBenchmark(inStream, outStream,
  270. OutArchiveFormat.XZ, CompressionMethod.Default,
  271. ref _features, LibraryFeature.CompressXz);
  272. }
  273. }
  274. #endregion
  275. #endregion
  276. if (_features != null && ModifyCapable && (_features.Value & LibraryFeature.Compress7z) != 0)
  277. {
  278. _features |= LibraryFeature.Modify;
  279. }
  280. return _features.Value;
  281. }
  282. }
  283. }
  284. /// <summary>
  285. /// Removes user from reference list and frees the 7-zip library if it becomes empty
  286. /// </summary>
  287. /// <param name="user">Caller of the function</param>
  288. /// <param name="format">Archive format</param>
  289. public static void FreeLibrary(object user, Enum format)
  290. {
  291. #if NET472 || NETSTANDARD2_0
  292. var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  293. sp.Demand();
  294. #endif
  295. lock (SyncRoot)
  296. {
  297. if (_modulePtr != IntPtr.Zero)
  298. {
  299. if (format is InArchiveFormat archiveFormat)
  300. {
  301. if (_inArchives != null && _inArchives.ContainsKey(user) &&
  302. _inArchives[user].ContainsKey(archiveFormat) &&
  303. _inArchives[user][archiveFormat] != null)
  304. {
  305. try
  306. {
  307. Marshal.ReleaseComObject(_inArchives[user][archiveFormat]);
  308. }
  309. catch (InvalidComObjectException) {}
  310. _inArchives[user].Remove(archiveFormat);
  311. _totalUsers--;
  312. if (_inArchives[user].Count == 0)
  313. {
  314. _inArchives.Remove(user);
  315. }
  316. }
  317. }
  318. if (format is OutArchiveFormat outArchiveFormat)
  319. {
  320. if (_outArchives != null && _outArchives.ContainsKey(user) &&
  321. _outArchives[user].ContainsKey(outArchiveFormat) &&
  322. _outArchives[user][outArchiveFormat] != null)
  323. {
  324. try
  325. {
  326. Marshal.ReleaseComObject(_outArchives[user][outArchiveFormat]);
  327. }
  328. catch (InvalidComObjectException) {}
  329. _outArchives[user].Remove(outArchiveFormat);
  330. _totalUsers--;
  331. if (_outArchives[user].Count == 0)
  332. {
  333. _outArchives.Remove(user);
  334. }
  335. }
  336. }
  337. if ((_inArchives == null || _inArchives.Count == 0) && (_outArchives == null || _outArchives.Count == 0))
  338. {
  339. _inArchives = null;
  340. _outArchives = null;
  341. if (_totalUsers == 0)
  342. {
  343. NativeMethods.FreeLibrary(_modulePtr);
  344. _modulePtr = IntPtr.Zero;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. /// <summary>
  351. /// Gets IInArchive interface to extract 7-zip archives.
  352. /// </summary>
  353. /// <param name="format">Archive format.</param>
  354. /// <param name="user">Archive format user.</param>
  355. public static IInArchive InArchive(InArchiveFormat format, object user)
  356. {
  357. lock (SyncRoot)
  358. {
  359. if (!_inArchives.ContainsKey(user) || _inArchives[user][format] == null)
  360. {
  361. #if NET472 || NETSTANDARD2_0
  362. var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  363. sp.Demand();
  364. #endif
  365. if (_modulePtr == IntPtr.Zero)
  366. {
  367. LoadLibrary(user, format);
  368. if (_modulePtr == IntPtr.Zero)
  369. {
  370. throw new SevenZipLibraryException();
  371. }
  372. }
  373. var createObject = (NativeMethods.CreateObjectDelegate)
  374. Marshal.GetDelegateForFunctionPointer(
  375. NativeMethods.GetProcAddress(_modulePtr, "CreateObject"),
  376. typeof(NativeMethods.CreateObjectDelegate));
  377. if (createObject == null)
  378. {
  379. throw new SevenZipLibraryException();
  380. }
  381. object result;
  382. var interfaceId = typeof(IInArchive).GUID;
  383. var classId = Formats.InFormatGuids[format];
  384. try
  385. {
  386. createObject(ref classId, ref interfaceId, out result);
  387. }
  388. catch (Exception)
  389. {
  390. throw new SevenZipLibraryException("Your 7-zip library does not support this archive type.");
  391. }
  392. InitUserInFormat(user, format);
  393. _inArchives[user][format] = result as IInArchive;
  394. }
  395. return _inArchives[user][format];
  396. }
  397. }
  398. /// <summary>
  399. /// Gets IOutArchive interface to pack 7-zip archives.
  400. /// </summary>
  401. /// <param name="format">Archive format.</param>
  402. /// <param name="user">Archive format user.</param>
  403. public static IOutArchive OutArchive(OutArchiveFormat format, object user)
  404. {
  405. lock (SyncRoot)
  406. {
  407. if (_outArchives[user][format] == null)
  408. {
  409. #if NET472 || NETSTANDARD2_0
  410. var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  411. sp.Demand();
  412. #endif
  413. if (_modulePtr == IntPtr.Zero)
  414. {
  415. throw new SevenZipLibraryException();
  416. }
  417. var createObject = (NativeMethods.CreateObjectDelegate)
  418. Marshal.GetDelegateForFunctionPointer(
  419. NativeMethods.GetProcAddress(_modulePtr, "CreateObject"),
  420. typeof(NativeMethods.CreateObjectDelegate));
  421. var interfaceId = typeof(IOutArchive).GUID;
  422. try
  423. {
  424. var classId = Formats.OutFormatGuids[format];
  425. createObject(ref classId, ref interfaceId, out var result);
  426. InitUserOutFormat(user, format);
  427. _outArchives[user][format] = result as IOutArchive;
  428. }
  429. catch (Exception)
  430. {
  431. throw new SevenZipLibraryException("Your 7-zip library does not support this archive type.");
  432. }
  433. }
  434. return _outArchives[user][format];
  435. }
  436. }
  437. public static void SetLibraryPath(string libraryPath)
  438. {
  439. if (_modulePtr != IntPtr.Zero && !Path.GetFullPath(libraryPath).Equals(Path.GetFullPath(_libraryFileName), StringComparison.OrdinalIgnoreCase))
  440. {
  441. throw new SevenZipLibraryException($"can not change the library path while the library \"{_libraryFileName}\" is being used.");
  442. }
  443. if (!File.Exists(libraryPath))
  444. {
  445. throw new SevenZipLibraryException($"can not change the library path because the file \"{libraryPath}\" does not exist.");
  446. }
  447. _libraryFileName = libraryPath;
  448. _features = null;
  449. }
  450. }
  451. #endif
  452. }