FileExt.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.IO;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. namespace Masuit.Tools.Files
  6. {
  7. /// <summary>
  8. /// 大文件操作扩展类
  9. /// </summary>
  10. public static class FileExt
  11. {
  12. /// <summary>
  13. /// 以文件流的形式复制大文件
  14. /// </summary>
  15. /// <param name="fs">源</param>
  16. /// <param name="dest">目标地址</param>
  17. /// <param name="bufferSize">缓冲区大小,默认8MB</param>
  18. public static void CopyToFile(this Stream fs, string dest, int bufferSize = 1024 * 8 * 1024)
  19. {
  20. using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  21. byte[] buf = new byte[bufferSize];
  22. int len;
  23. while ((len = fs.Read(buf, 0, buf.Length)) != 0)
  24. {
  25. fsWrite.Write(buf, 0, len);
  26. }
  27. }
  28. /// <summary>
  29. /// 以文件流的形式复制大文件(异步方式)
  30. /// </summary>
  31. /// <param name="fs">源</param>
  32. /// <param name="dest">目标地址</param>
  33. /// <param name="bufferSize">缓冲区大小,默认8MB</param>
  34. public static async void CopyToFileAsync(this Stream fs, string dest, int bufferSize = 1024 * 1024 * 8)
  35. {
  36. await using FileStream fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  37. byte[] buf = new byte[bufferSize];
  38. int len;
  39. await Task.Run(() =>
  40. {
  41. while ((len = fs.Read(buf, 0, buf.Length)) != 0)
  42. {
  43. fsWrite.Write(buf, 0, len);
  44. }
  45. }).ConfigureAwait(true);
  46. }
  47. /// <summary>
  48. /// 将内存流转储成文件
  49. /// </summary>
  50. /// <param name="ms"></param>
  51. /// <param name="filename"></param>
  52. public static void SaveFile(this MemoryStream ms, string filename)
  53. {
  54. using var fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  55. byte[] buffer = ms.ToArray(); // 转化为byte格式存储
  56. fs.Write(buffer, 0, buffer.Length);
  57. fs.Flush();
  58. buffer = null;
  59. }
  60. /// <summary>
  61. /// 计算文件的 MD5 值
  62. /// </summary>
  63. /// <param name="fs">源文件流</param>
  64. /// <returns>MD5 值16进制字符串</returns>
  65. public static string GetFileMD5(this FileStream fs) => HashFile(fs, "md5");
  66. /// <summary>
  67. /// 计算文件的 sha1 值
  68. /// </summary>
  69. /// <param name="fs">源文件流</param>
  70. /// <returns>sha1 值16进制字符串</returns>
  71. public static string GetFileSha1(this Stream fs) => HashFile(fs, "sha1");
  72. /// <summary>
  73. /// 计算文件的哈希值
  74. /// </summary>
  75. /// <param name="fs">被操作的源数据流</param>
  76. /// <param name="algo">加密算法</param>
  77. /// <returns>哈希值16进制字符串</returns>
  78. private static string HashFile(Stream fs, string algo)
  79. {
  80. HashAlgorithm crypto;
  81. switch (algo)
  82. {
  83. case "sha1":
  84. crypto = new SHA1CryptoServiceProvider();
  85. break;
  86. default:
  87. crypto = new MD5CryptoServiceProvider();
  88. break;
  89. }
  90. byte[] retVal = crypto.ComputeHash(fs);
  91. StringBuilder sb = new StringBuilder();
  92. foreach (var t in retVal)
  93. {
  94. sb.Append(t.ToString("x2"));
  95. }
  96. return sb.ToString();
  97. }
  98. }
  99. }