Skip to content

Commit

Permalink
修正流访问的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ldqk committed Dec 15, 2023
1 parent 0ca9e96 commit 01bb198
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
209 changes: 109 additions & 100 deletions Masuit.Tools.Abstractions/Files/FileExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,112 +5,121 @@

namespace Masuit.Tools.Files
{
/// <summary>
/// 大文件操作扩展类
/// </summary>
public static class FileExt
{
/// <summary>
/// 以文件流的形式复制大文件
/// </summary>
/// <param name="fs">源</param>
/// <param name="dest">目标地址</param>
/// <param name="bufferSize">缓冲区大小,默认8MB</param>
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);
}
/// <summary>
/// 大文件操作扩展类
/// </summary>
public static class FileExt
{
/// <summary>
/// 以文件流的形式复制大文件
/// </summary>
/// <param name="fs">源</param>
/// <param name="dest">目标地址</param>
/// <param name="bufferSize">缓冲区大小,默认8MB</param>
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);
}

/// <summary>
/// 以文件流的形式复制大文件(异步方式)
/// </summary>
/// <param name="fs">源</param>
/// <param name="dest">目标地址</param>
/// <param name="bufferSize">缓冲区大小,默认8MB</param>
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);
}
/// <summary>
/// 以文件流的形式复制大文件(异步方式)
/// </summary>
/// <param name="fs">源</param>
/// <param name="dest">目标地址</param>
/// <param name="bufferSize">缓冲区大小,默认8MB</param>
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);
}

/// <summary>
/// 将内存流转储成文件
/// </summary>
/// <param name="ms"></param>
/// <param name="filename"></param>
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);
}
/// <summary>
/// 将内存流转储成文件
/// </summary>
/// <param name="ms"></param>
/// <param name="filename"></param>
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);
}

/// <summary>
/// 将内存流转储成文件
/// </summary>
/// <param name="ms"></param>
/// <param name="filename"></param>
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);
}
/// <summary>
/// 将内存流转储成文件
/// </summary>
/// <param name="ms"></param>
/// <param name="filename"></param>
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);
}

/// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>MD5 值16进制字符串</returns>
public static string GetFileMD5(this FileStream fs) => HashFile(fs);
/// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>MD5 值16进制字符串</returns>
public static string GetFileMD5(this FileStream fs) => HashFile(fs);

/// <summary>
/// 计算文件的 sha1 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>sha1 值16进制字符串</returns>
public static string GetFileSha1(this Stream fs) => HashFile(fs, nameof(SHA1));
/// <summary>
/// 计算文件的 sha1 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>sha1 值16进制字符串</returns>
public static string GetFileSha1(this Stream fs) => HashFile(fs, nameof(SHA1));

/// <summary>
/// 计算文件的 sha256 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>sha256 值16进制字符串</returns>
public static string GetFileSha256(this Stream fs) => HashFile(fs, nameof(SHA256));
/// <summary>
/// 计算文件的 sha256 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>sha256 值16进制字符串</returns>
public static string GetFileSha256(this Stream fs) => HashFile(fs, nameof(SHA256));

/// <summary>
/// 计算文件的 sha512 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>sha512 值16进制字符串</returns>
public static string GetFileSha512(this Stream fs) => HashFile(fs, nameof(SHA512));
/// <summary>
/// 计算文件的 sha512 值
/// </summary>
/// <param name="fs">源文件流</param>
/// <returns>sha512 值16进制字符串</returns>
public static string GetFileSha512(this Stream fs) => HashFile(fs, nameof(SHA512));

/// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fs">被操作的源数据流</param>
/// <param name="algo">加密算法</param>
/// <returns>哈希值16进制字符串</returns>
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();
}
}
/// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fs">被操作的源数据流</param>
/// <param name="algo">加密算法</param>
/// <returns>哈希值16进制字符串</returns>
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();
}
}
}
2 changes: 1 addition & 1 deletion Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6;net7;net8</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>2.6.8.2</Version>
<Version>2.6.8.3</Version>
<Authors>懒得勤快</Authors>
<Description>全龄段友好的C#万能工具库,码数吐司库,不管你是菜鸟新手还是骨灰级玩家都能轻松上手,Masuit.Tools基础公共库(适用于.NET4.6.1/.NET Standard2.0及以上项目),包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
官网教程:https://tools.masuit.org
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Product>Masuit.Tools.AspNetCore</Product>
<PackageId>Masuit.Tools.AspNetCore</PackageId>
<LangVersion>latest</LangVersion>
<Version>1.2.8.2</Version>
<Version>1.2.8.3</Version>
<RepositoryType></RepositoryType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<FileVersion>1.1.9</FileVersion>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.Core/Masuit.Tools.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
官网教程:https://tools.masuit.org
github:https://github.com/ldqk/Masuit.Tools
</Description>
<Version>2.6.8.2</Version>
<Version>2.6.8.3</Version>
<Copyright>Copyright © 懒得勤快</Copyright>
<PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
<PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>
Expand Down
4 changes: 2 additions & 2 deletions Masuit.Tools.Excel/Masuit.Tools.Excel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.2.8.1</Version>
<Version>1.2.8.2</Version>
<Authors>懒得勤快</Authors>
<Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
<Copyright>懒得勤快</Copyright>
Expand Down Expand Up @@ -38,7 +38,7 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="EPPlus" Version="7.0.3" />
<PackageReference Include="EPPlus" Version="7.0.4" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Masuit.Tools.Net45/Media/ImageUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Masuit.Tools.Systems;

namespace Masuit.Tools.Media
{
Expand Down Expand Up @@ -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变量。
Expand Down
4 changes: 2 additions & 2 deletions Masuit.Tools.Net45/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<package>
<metadata>
<id>Masuit.Tools.Net45</id>
<version>2.6.8.2</version>
<version>2.6.8.3</version>
<title>Masuit.Tools</title>
<authors>懒得勤快</authors>
<owners>masuit.com</owners>
<licenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/ldqk/Masuit.Tools</projectUrl>
<!--<iconUrl></iconUrl>-->
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Masuit.Tools的.NET Framework 4.5专用版本,相比4.6.1及.NET Core的版本,阉割了HTML、文件压缩、ASP.NET扩展、硬件监测、Session扩展等功能。</description>
<description>全龄段友好的C#万能工具库,码数吐司库(适用于.NET Core项目),Masuit.Tools的.NET Framework 4.5专用版本,相比4.6.1及.NET Core的版本,阉割了HTML、文件压缩、ASP.NET扩展、硬件监测、Session扩展等功能。</description>
<releaseNotes>如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools</releaseNotes>
<copyright>Copyright © 懒得勤快</copyright>
<tags>Masuit.Tools,工具库,Utility,Crypt,Extensions</tags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="MongoDB.Driver" Version="2.23.0" />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions Masuit.Tools/Files/SevenZipCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public static class SevenZipCompressor
/// <param name="files">多个文件路径,文件或文件夹,或网络路径http/https</param>
/// <param name="rootdir"></param>
/// <returns>文件流</returns>
public static MemoryStream ZipStream(List<string> files, string rootdir = "")
public static PooledMemoryStream ZipStream(List<string> 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,
Expand All @@ -47,15 +47,15 @@ public static MemoryStream ZipStream(List<string> files, string rootdir = "")
/// <param name="archiveType"></param>
/// <param name="disposeAllStreams">是否需要释放所有流</param>
/// <returns>文件流</returns>
public static MemoryStream ZipStream(DisposableDictionary<string, Stream> streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
public static PooledMemoryStream ZipStream(DisposableDictionary<string, Stream> streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
{
using var archive = ArchiveFactory.Create(archiveType);
foreach (var pair in streams)
{
archive.AddEntry(pair.Key, pair.Value, true);
}

var ms = new MemoryStream();
var ms = new PooledMemoryStream();
archive.SaveTo(ms, new WriterOptions(CompressionType.LZMA)
{
LeaveStreamOpen = true,
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools/Masuit.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
<Version>1.0.0</Version>
</PackageReference>
<PackageReference Include="StackExchange.Redis">
<Version>2.7.4</Version>
<Version>2.7.10</Version>
</PackageReference>
<PackageReference Include="System.ValueTuple">
<Version>4.5.0</Version>
Expand Down
5 changes: 3 additions & 2 deletions Masuit.Tools/Mvc/ActionResults/ResumeFileContentResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using Masuit.Tools.Systems;

namespace Masuit.Tools.Mvc.ActionResults
{
Expand All @@ -12,12 +13,12 @@ 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)
{
EntityTag = etag;
}
}
}
}
Loading

0 comments on commit 01bb198

Please sign in to comment.