using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Masuit.Tools.Files
{
///
/// 大文件操作扩展类
///
public static class FileExt
{
///
/// 以文件流的形式复制大文件
///
/// 源
/// 目标地址
/// 缓冲区大小,默认8MB
public static void CopyToFile(this Stream fs, string dest, int bufferSize = 1024 * 8 * 1024)
{
using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] buf = new byte[bufferSize];
int len;
while ((len = fs.Read(buf, 0, buf.Length)) != 0)
{
fsWrite.Write(buf, 0, len);
}
}
///
/// 以文件流的形式复制大文件(异步方式)
///
/// 源
/// 目标地址
/// 缓冲区大小,默认8MB
public static async Task CopyToFileAsync(this Stream fs, string dest, int bufferSize = 1024 * 1024 * 8)
{
using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] buf = new byte[bufferSize];
int len;
while ((len = await fs.ReadAsync(buf, 0, buf.Length)) != 0)
{
await fsWrite.WriteAsync(buf, 0, len);
}
}
///
/// 将内存流转储成文件
///
///
///
public static void SaveFile(this Stream ms, string filename)
{
using var fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
byte[] buffer = ms.ToArray(); // 转化为byte格式存储
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
}
///
/// 计算文件的 MD5 值
///
/// 源文件流
/// MD5 值16进制字符串
public static string GetFileMD5(this FileStream fs) => HashFile(fs, "md5");
///
/// 计算文件的 sha1 值
///
/// 源文件流
/// sha1 值16进制字符串
public static string GetFileSha1(this Stream fs) => HashFile(fs, "sha1");
///
/// 计算文件的 sha256 值
///
/// 源文件流
/// sha256 值16进制字符串
public static string GetFileSha256(this Stream fs) => HashFile(fs, "sha256");
///
/// 计算文件的 sha512 值
///
/// 源文件流
/// sha512 值16进制字符串
public static string GetFileSha512(this Stream fs) => HashFile(fs, "sha512");
///
/// 计算文件的哈希值
///
/// 被操作的源数据流
/// 加密算法
/// 哈希值16进制字符串
private static string HashFile(Stream fs, string algo)
{
using HashAlgorithm crypto = algo switch
{
"sha1" => SHA1.Create(),
"sha256" => SHA256.Create(),
"sha512" => SHA512.Create(),
_ => MD5.Create(),
};
byte[] retVal = crypto.ComputeHash(fs);
var sb = new StringBuilder();
foreach (var t in retVal)
{
sb.Append(t.ToString("x2"));
}
return sb.ToString();
}
}
}