diff --git a/Masuit.Tools.Abstractions/Extensions/BaseType/StreamExtensions.cs b/Masuit.Tools.Abstractions/Extensions/BaseType/StreamExtensions.cs index d073327d..55a6506c 100644 --- a/Masuit.Tools.Abstractions/Extensions/BaseType/StreamExtensions.cs +++ b/Masuit.Tools.Abstractions/Extensions/BaseType/StreamExtensions.cs @@ -44,7 +44,7 @@ public static byte[] ToArray(this Stream stream) { stream.Position = 0; byte[] bytes = new byte[stream.Length]; - stream.Read(bytes, 0, bytes.Length); + _ = stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); diff --git a/Masuit.Tools.Abstractions/Files/FileExt.cs b/Masuit.Tools.Abstractions/Files/FileExt.cs index e9e5e6e1..db966a47 100644 --- a/Masuit.Tools.Abstractions/Files/FileExt.cs +++ b/Masuit.Tools.Abstractions/Files/FileExt.cs @@ -5,112 +5,121 @@ 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); - var stream = new BufferedStream(fs, bufferSize); - stream.CopyTo(fsWrite); - } + /// + /// 大文件操作扩展类 + /// + public static class FileExt + { + /// + /// 以文件流的形式复制大文件 + /// + /// 源 + /// 目标地址 + /// 缓冲区大小,默认8MB + public static void CopyToFile(this Stream fs, string dest, int bufferSize = 1024 * 8 * 1024) + { + fs.Seek(0, SeekOrigin.Begin); + using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite); + var stream = new BufferedStream(fs, bufferSize); + stream.CopyTo(fsWrite); + fs.Seek(0, SeekOrigin.Begin); + } - /// - /// 以文件流的形式复制大文件(异步方式) - /// - /// 源 - /// 目标地址 - /// 缓冲区大小,默认8MB - public static Task CopyToFileAsync(this Stream fs, string dest, int bufferSize = 1024 * 1024 * 8) - { - using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite); - var stream = new BufferedStream(fs, bufferSize); - return stream.CopyToAsync(fsWrite); - } + /// + /// 以文件流的形式复制大文件(异步方式) + /// + /// 源 + /// 目标地址 + /// 缓冲区大小,默认8MB + public static Task CopyToFileAsync(this Stream fs, string dest, int bufferSize = 1024 * 1024 * 8) + { + using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite); + var stream = new BufferedStream(fs, bufferSize); + return stream.CopyToAsync(fsWrite); + } - /// - /// 将内存流转储成文件 - /// - /// - /// - public static void SaveFile(this Stream ms, string filename) - { - using var fs = new FileStream(filename, FileMode.Create, FileAccess.Write); - var stream = new BufferedStream(ms, 1048576); - stream.CopyTo(fs); - } + /// + /// 将内存流转储成文件 + /// + /// + /// + public static void SaveFile(this Stream ms, string filename) + { + ms.Seek(0, SeekOrigin.Begin); + using var fs = new FileStream(filename, FileMode.Create, FileAccess.Write); + var stream = new BufferedStream(ms, 1048576); + stream.CopyTo(fs); + ms.Seek(0, SeekOrigin.Begin); + } - /// - /// 将内存流转储成文件 - /// - /// - /// - public static Task SaveFileAsync(this Stream ms, string filename) - { - using var fs = new FileStream(filename, FileMode.Create, FileAccess.Write); - var stream = new BufferedStream(ms, 1048576); - return stream.CopyToAsync(fs); - } + /// + /// 将内存流转储成文件 + /// + /// + /// + public static async Task SaveFileAsync(this Stream ms, string filename) + { + ms.Seek(0, SeekOrigin.Begin); + using var fs = new FileStream(filename, FileMode.Create, FileAccess.Write); + var stream = new BufferedStream(ms, 1048576); + await stream.CopyToAsync(fs); + ms.Seek(0, SeekOrigin.Begin); + } - /// - /// 计算文件的 MD5 值 - /// - /// 源文件流 - /// MD5 值16进制字符串 - public static string GetFileMD5(this FileStream fs) => HashFile(fs); + /// + /// 计算文件的 MD5 值 + /// + /// 源文件流 + /// MD5 值16进制字符串 + public static string GetFileMD5(this FileStream fs) => HashFile(fs); - /// - /// 计算文件的 sha1 值 - /// - /// 源文件流 - /// sha1 值16进制字符串 - public static string GetFileSha1(this Stream fs) => HashFile(fs, nameof(SHA1)); + /// + /// 计算文件的 sha1 值 + /// + /// 源文件流 + /// sha1 值16进制字符串 + public static string GetFileSha1(this Stream fs) => HashFile(fs, nameof(SHA1)); - /// - /// 计算文件的 sha256 值 - /// - /// 源文件流 - /// sha256 值16进制字符串 - public static string GetFileSha256(this Stream fs) => HashFile(fs, nameof(SHA256)); + /// + /// 计算文件的 sha256 值 + /// + /// 源文件流 + /// sha256 值16进制字符串 + public static string GetFileSha256(this Stream fs) => HashFile(fs, nameof(SHA256)); - /// - /// 计算文件的 sha512 值 - /// - /// 源文件流 - /// sha512 值16进制字符串 - public static string GetFileSha512(this Stream fs) => HashFile(fs, nameof(SHA512)); + /// + /// 计算文件的 sha512 值 + /// + /// 源文件流 + /// sha512 值16进制字符串 + public static string GetFileSha512(this Stream fs) => HashFile(fs, nameof(SHA512)); - /// - /// 计算文件的哈希值 - /// - /// 被操作的源数据流 - /// 加密算法 - /// 哈希值16进制字符串 - private static string HashFile(Stream fs, string algo = nameof(MD5)) - { - using HashAlgorithm crypto = algo switch - { - nameof(SHA1) => SHA1.Create(), - nameof(SHA256) => SHA256.Create(), - nameof(SHA512) => SHA512.Create(), - _ => MD5.Create(), - }; - var stream = new BufferedStream(fs, 1048576); - byte[] hash = crypto.ComputeHash(stream); - var sb = new StringBuilder(); - foreach (var t in hash) - { - sb.Append(t.ToString("x2")); - } - return sb.ToString(); - } - } + /// + /// 计算文件的哈希值 + /// + /// 被操作的源数据流 + /// 加密算法 + /// 哈希值16进制字符串 + private static string HashFile(Stream fs, string algo = nameof(MD5)) + { + fs.Seek(0, SeekOrigin.Begin); + using HashAlgorithm crypto = algo switch + { + nameof(SHA1) => SHA1.Create(), + nameof(SHA256) => SHA256.Create(), + nameof(SHA512) => SHA512.Create(), + _ => MD5.Create(), + }; + var stream = new BufferedStream(fs, 1048576); + byte[] hash = crypto.ComputeHash(stream); + var sb = new StringBuilder(); + foreach (var t in hash) + { + sb.Append(t.ToString("x2")); + } + + fs.Seek(0, SeekOrigin.Begin); + return sb.ToString(); + } + } } diff --git a/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj b/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj index 48f08057..4e938c1d 100644 --- a/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj +++ b/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj @@ -3,7 +3,7 @@ netstandard2.0;netstandard2.1;net461;net5;net6;net7;net8 latest true - 2.6.8.2 + 2.6.8.3 懒得勤快 全龄段友好的C#万能工具库,码数吐司库,不管你是菜鸟新手还是骨灰级玩家都能轻松上手,Masuit.Tools基础公共库(适用于.NET4.6.1/.NET Standard2.0及以上项目),包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。 官网教程:https://tools.masuit.org diff --git a/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj b/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj index 5c471d76..507064b2 100644 --- a/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj +++ b/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj @@ -18,7 +18,7 @@ Masuit.Tools.AspNetCore Masuit.Tools.AspNetCore latest - 1.2.8.2 + 1.2.8.3 True 1.1.9 diff --git a/Masuit.Tools.Core/Masuit.Tools.Core.csproj b/Masuit.Tools.Core/Masuit.Tools.Core.csproj index 81a3f83a..0388be25 100644 --- a/Masuit.Tools.Core/Masuit.Tools.Core.csproj +++ b/Masuit.Tools.Core/Masuit.Tools.Core.csproj @@ -6,7 +6,7 @@ 官网教程:https://tools.masuit.org github:https://github.com/ldqk/Masuit.Tools - 2.6.8.2 + 2.6.8.3 Copyright © 懒得勤快 https://github.com/ldqk/Masuit.Tools Masuit.Tools,工具库,Utility,Crypt,Extensions diff --git a/Masuit.Tools.Excel/Masuit.Tools.Excel.csproj b/Masuit.Tools.Excel/Masuit.Tools.Excel.csproj index a0b100a4..94d576dc 100644 --- a/Masuit.Tools.Excel/Masuit.Tools.Excel.csproj +++ b/Masuit.Tools.Excel/Masuit.Tools.Excel.csproj @@ -3,7 +3,7 @@ netstandard2.0 latest true - 1.2.8.1 + 1.2.8.2 懒得勤快 Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列 懒得勤快 @@ -38,7 +38,7 @@ - + diff --git a/Masuit.Tools.Net45/Media/ImageUtilities.cs b/Masuit.Tools.Net45/Media/ImageUtilities.cs index 9fd554d8..df800349 100644 --- a/Masuit.Tools.Net45/Media/ImageUtilities.cs +++ b/Masuit.Tools.Net45/Media/ImageUtilities.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using Masuit.Tools.Systems; namespace Masuit.Tools.Media { @@ -1088,7 +1089,7 @@ public static Bitmap SaveDataUriAsImageFile(this string source) { string strbase64 = source.Substring(source.IndexOf(',') + 1).Trim('\0'); byte[] arr = Convert.FromBase64String(strbase64); - using var ms = new MemoryStream(arr); + using var ms = new PooledMemoryStream(arr); using var bmpp = new Bitmap(ms); //新建第二个bitmap类型的bmpp2变量。 diff --git a/Masuit.Tools.Net45/package.nuspec b/Masuit.Tools.Net45/package.nuspec index 5166a6c4..edd11849 100644 --- a/Masuit.Tools.Net45/package.nuspec +++ b/Masuit.Tools.Net45/package.nuspec @@ -2,7 +2,7 @@ Masuit.Tools.Net45 - 2.6.8.2 + 2.6.8.3 Masuit.Tools 懒得勤快 masuit.com @@ -10,7 +10,7 @@ https://github.com/ldqk/Masuit.Tools false - Masuit.Tools的.NET Framework 4.5专用版本,相比4.6.1及.NET Core的版本,阉割了HTML、文件压缩、ASP.NET扩展、硬件监测、Session扩展等功能。 + 全龄段友好的C#万能工具库,码数吐司库(适用于.NET Core项目),Masuit.Tools的.NET Framework 4.5专用版本,相比4.6.1及.NET Core的版本,阉割了HTML、文件压缩、ASP.NET扩展、硬件监测、Session扩展等功能。 如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools Copyright © 懒得勤快 Masuit.Tools,工具库,Utility,Crypt,Extensions diff --git a/Masuit.Tools.NoSQL.MongoDBClient/Masuit.Tools.NoSQL.MongoDBClient.csproj b/Masuit.Tools.NoSQL.MongoDBClient/Masuit.Tools.NoSQL.MongoDBClient.csproj index 347aeb95..bf4e493f 100644 --- a/Masuit.Tools.NoSQL.MongoDBClient/Masuit.Tools.NoSQL.MongoDBClient.csproj +++ b/Masuit.Tools.NoSQL.MongoDBClient/Masuit.Tools.NoSQL.MongoDBClient.csproj @@ -38,7 +38,7 @@ - + diff --git a/Masuit.Tools/Files/SevenZipCompressor.cs b/Masuit.Tools/Files/SevenZipCompressor.cs index d2ef7950..336d3aee 100644 --- a/Masuit.Tools/Files/SevenZipCompressor.cs +++ b/Masuit.Tools/Files/SevenZipCompressor.cs @@ -25,10 +25,10 @@ public static class SevenZipCompressor /// 多个文件路径,文件或文件夹,或网络路径http/https /// /// 文件流 - public static MemoryStream ZipStream(List files, string rootdir = "") + public static PooledMemoryStream ZipStream(List files, string rootdir = "") { using var archive = CreateZipArchive(files, rootdir); - var ms = new MemoryStream(); + var ms = new PooledMemoryStream(); archive.SaveTo(ms, new WriterOptions(CompressionType.LZMA) { LeaveStreamOpen = true, @@ -47,7 +47,7 @@ public static MemoryStream ZipStream(List files, string rootdir = "") /// /// 是否需要释放所有流 /// 文件流 - public static MemoryStream ZipStream(DisposableDictionary streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false) + public static PooledMemoryStream ZipStream(DisposableDictionary streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false) { using var archive = ArchiveFactory.Create(archiveType); foreach (var pair in streams) @@ -55,7 +55,7 @@ public static MemoryStream ZipStream(DisposableDictionary stream archive.AddEntry(pair.Key, pair.Value, true); } - var ms = new MemoryStream(); + var ms = new PooledMemoryStream(); archive.SaveTo(ms, new WriterOptions(CompressionType.LZMA) { LeaveStreamOpen = true, diff --git a/Masuit.Tools/Masuit.Tools.csproj b/Masuit.Tools/Masuit.Tools.csproj index f7f4c16d..eae20c32 100644 --- a/Masuit.Tools/Masuit.Tools.csproj +++ b/Masuit.Tools/Masuit.Tools.csproj @@ -220,7 +220,7 @@ 1.0.0 - 2.7.4 + 2.7.10 4.5.0 diff --git a/Masuit.Tools/Mvc/ActionResults/ResumeFileContentResult.cs b/Masuit.Tools/Mvc/ActionResults/ResumeFileContentResult.cs index bfb6b4c1..6e17d8e2 100644 --- a/Masuit.Tools/Mvc/ActionResults/ResumeFileContentResult.cs +++ b/Masuit.Tools/Mvc/ActionResults/ResumeFileContentResult.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Masuit.Tools.Systems; namespace Masuit.Tools.Mvc.ActionResults { @@ -12,7 +13,7 @@ public ResumeFileContentResult(byte[] fileContents, string fileName) : base(file throw new ArgumentNullException(nameof(fileContents)); } - FileContents = new MemoryStream(fileContents); + FileContents = new PooledMemoryStream(fileContents); } public ResumeFileContentResult(byte[] fileContents, string fileName, string etag) : this(fileContents, fileName) @@ -20,4 +21,4 @@ public ResumeFileContentResult(byte[] fileContents, string fileName, string etag EntityTag = etag; } } -} \ No newline at end of file +} diff --git a/Masuit.Tools/Strings/ValidateCode.cs b/Masuit.Tools/Strings/ValidateCode.cs index 2b94a58b..38936ead 100644 --- a/Masuit.Tools/Strings/ValidateCode.cs +++ b/Masuit.Tools/Strings/ValidateCode.cs @@ -5,6 +5,8 @@ using System.Security.Cryptography; using System.Text; using System.Web; +using Masuit.Tools.Systems; + namespace Masuit.Tools.Strings { /// @@ -45,10 +47,13 @@ public static byte[] CreateValidateGraphic(this HttpContext context, string vali { using Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * (fontSize + 2.0)), lineHeight); using Graphics g = Graphics.FromImage(image); + //生成随机生成器 Random random = new Random(); + //清空图片背景色 g.Clear(Color.White); + //画图片的干扰线 for (int i = 0; i < 75; i++) { @@ -67,6 +72,7 @@ public static byte[] CreateValidateGraphic(this HttpContext context, string vali new Font("宋体", fontSize, FontStyle.Bold | FontStyle.Italic), new Font("楷体", fontSize, FontStyle.Bold | FontStyle.Italic) }; + //渐变. using var brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(validateCode, fonts[new Random().Next(fonts.Length)], brush, 3, 2); @@ -81,9 +87,11 @@ public static byte[] CreateValidateGraphic(this HttpContext context, string vali //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); + //保存图片数据 - using MemoryStream stream = new MemoryStream(); + using var stream = new PooledMemoryStream(); image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); + //输出图片流 context.Response.Clear(); context.Response.ContentType = "image/jpeg"; diff --git a/Masuit.Tools/package.nuspec b/Masuit.Tools/package.nuspec index f575b45b..2b61e9d5 100644 --- a/Masuit.Tools/package.nuspec +++ b/Masuit.Tools/package.nuspec @@ -2,16 +2,14 @@ Masuit.Tools.Net - 2.6.8.2 + 2.6.8.3 Masuit.Tools 懒得勤快 masuit.com https://github.com/ldqk/Masuit.Tools false - 包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。 -官网教程:https://ldqk.org/55 - - + 全龄段友好的C#万能工具库,码数吐司库(适用于.NET Framework 4.6.1+项目),不管你是菜鸟新手还是骨灰级玩家都能轻松上手,包含一些常用的操作类,大都是静态类,加密解密,反射操作,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。 +官网教程:https://tools.masuit.org github:https://github.com/ldqk/Masuit.Tools 如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools Copyright © 懒得勤快 diff --git a/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj b/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj index f1380b37..8b83f24b 100644 --- a/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj +++ b/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj @@ -14,8 +14,8 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj b/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj index d75561c1..cac41702 100644 --- a/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj +++ b/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj @@ -12,8 +12,8 @@ - - + + all runtime; build; native; contentfiles; analyzers diff --git a/Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj b/Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj index 3417f788..5b2cc742 100644 --- a/Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj +++ b/Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj @@ -116,25 +116,25 @@ 4.5.0 - 2.6.2 + 2.6.3 2.0.3 - 1.6.0 + 1.7.0 - 2.6.2 + 2.6.3 - 2.6.2 + 2.6.3 - 2.6.2 + 2.6.3 - 2.6.2 + 2.6.3 diff --git a/Test/Masuit.Tools.Test/Mvc/Mocks/MockHttpRequest.cs b/Test/Masuit.Tools.Test/Mvc/Mocks/MockHttpRequest.cs index 0a9b3fca..c3ad6a20 100644 --- a/Test/Masuit.Tools.Test/Mvc/Mocks/MockHttpRequest.cs +++ b/Test/Masuit.Tools.Test/Mvc/Mocks/MockHttpRequest.cs @@ -3,6 +3,7 @@ using System.IO; using System.Web; using System.Web.Routing; +using Masuit.Tools.Systems; namespace Masuit.Tools.Test.Mvc.Mocks { @@ -13,6 +14,7 @@ public class MockHttpRequest : HttpRequestBase public override HttpFileCollectionBase Files { get; } public override RequestContext RequestContext => _context; + public override string ApplicationPath => _applicationPath; public override System.IO.Stream InputStream @@ -21,7 +23,7 @@ public override System.IO.Stream InputStream { if (TestInput != null) { - var stream = new MemoryStream(); + var stream = new PooledMemoryStream(); var chars = TestInput.ToCharArray(); foreach (var c in chars) { @@ -29,11 +31,12 @@ public override System.IO.Stream InputStream } return stream; } - return new MemoryStream(); + return new PooledMemoryStream(); } } public override string HttpMethod => TestHttpMethod; + private readonly NameValueCollection _headers = new NameValueCollection(); private readonly MockRequestContext _context = new MockRequestContext(); private string _applicationPath; @@ -58,4 +61,4 @@ public MockHttpRequest SetApplicationPath(string path) return this; } } -} \ No newline at end of file +}