Skip to content

Commit 3366c7b

Browse files
authored
Merge pull request #8 from Crequency/dev=main
[Pull Request] More extensions functions added.
2 parents aa66377 + d9d8b3d commit 3366c7b

5 files changed

Lines changed: 171 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Common.BasicHelper.Utils.Extensions;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace Common.BasicHelper.IO;
7+
8+
public class DirectoryHelper
9+
{
10+
/// <summary>
11+
/// 获取一个文件夹包括子文件夹及其子文件的总大小
12+
/// </summary>
13+
/// <param name="path">文件夹路径</param>
14+
/// <returns>总大小</returns>
15+
public static long GetDirectorySize(string path)
16+
{
17+
var result = 0L;
18+
19+
var dir = path.GetFullPath();
20+
21+
if (!Directory.Exists(dir)) return result;
22+
23+
var pendingFolders = new Queue<DirectoryInfo>();
24+
25+
pendingFolders.Enqueue(new DirectoryInfo(dir));
26+
27+
while (pendingFolders.Count > 0)
28+
{
29+
var folder = pendingFolders.Dequeue();
30+
31+
result += folder.GetFiles().Sum(file => file.Length);
32+
33+
foreach (var subFolder in folder.GetDirectories())
34+
pendingFolders.Enqueue(subFolder);
35+
}
36+
37+
return result;
38+
}
39+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Text;
2+
3+
namespace Common.BasicHelper.Utils.Extensions;
4+
5+
public static class BinaryHelper
6+
{
7+
/// <summary>
8+
/// 二进制内容转 UTF8 字符串
9+
/// </summary>
10+
/// <param name="content">字节数组</param>
11+
/// <param name="index">内容起始下标</param>
12+
/// <param name="count">内容长度</param>
13+
/// <returns>UTF8 字符串</returns>
14+
public static string ToUTF8(this byte[] content, int? index = null, int? count = null)
15+
{
16+
if (index is null || count is null)
17+
return Encoding.UTF8.GetString(content);
18+
else
19+
return Encoding.UTF8.GetString(
20+
content,
21+
index ?? 0,
22+
count ?? content.Length
23+
);
24+
}
25+
26+
/// <summary>
27+
/// 二进制内容转 UTF32 字符串
28+
/// </summary>
29+
/// <param name="content">字节数组</param>
30+
/// <param name="index">内容起始下标</param>
31+
/// <param name="count">内容长度</param>
32+
/// <returns>UTF32 字符串</returns>
33+
public static string ToUTF32(this byte[] content, int? index = null, int? count = null)
34+
{
35+
if (index is null || count is null)
36+
return Encoding.UTF32.GetString(content);
37+
else
38+
return Encoding.UTF32.GetString(
39+
content,
40+
index ?? 0,
41+
count ?? content.Length
42+
);
43+
}
44+
45+
/// <summary>
46+
/// 二进制内容转 Unicode 字符串
47+
/// </summary>
48+
/// <param name="content">字节数组</param>
49+
/// <param name="index">内容起始下标</param>
50+
/// <param name="count">内容长度</param>
51+
/// <returns>Unicode 字符串</returns>
52+
public static string ToUnicode(this byte[] content, int? index = null, int? count = null)
53+
{
54+
if (index is null || count is null)
55+
return Encoding.Unicode.GetString(content);
56+
else
57+
return Encoding.Unicode.GetString(
58+
content,
59+
index ?? 0,
60+
count ?? content.Length
61+
);
62+
}
63+
64+
/// <summary>
65+
/// 二进制内容转 ASCII 字符串
66+
/// </summary>
67+
/// <param name="content">字节数组</param>
68+
/// <param name="index">内容起始下标</param>
69+
/// <param name="count">内容长度</param>
70+
/// <returns>ASCII 字符串</returns>
71+
public static string ToASCII(this byte[] content, int? index = null, int? count = null)
72+
{
73+
if (index is null || count is null)
74+
return Encoding.ASCII.GetString(content);
75+
else
76+
return Encoding.ASCII.GetString(
77+
content,
78+
index ?? 0,
79+
count ?? content.Length
80+
);
81+
}
82+
}

Common.BasicHelper/Utils/Extensions/StringHelper.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Common.BasicHelper.IO;
2+
using System;
23
using System.IO;
34
using System.Text;
45
using System.Threading.Tasks;
@@ -116,4 +117,46 @@ public static void Throw<T>(this string? message) where T : Exception
116117
var exp = Activator.CreateInstance(typeof(T), message);
117118
throw (exp as T) ?? new Exception(message);
118119
}
120+
121+
/// <summary>
122+
/// 获取给定路径的完整路径
123+
/// </summary>
124+
/// <param name="path">路径</param>
125+
/// <returns>完整路径</returns>
126+
public static string GetFullPath(this string path) => Path.GetFullPath(path);
127+
128+
/// <summary>
129+
/// 获取给定路径的完整大小 (字节数)
130+
/// </summary>
131+
/// <param name="path">路径</param>
132+
/// <returns>给定路径的完整字节数</returns>
133+
public static long GetTotalSize(this string path) => DirectoryHelper.GetDirectorySize(path);
134+
135+
/// <summary>
136+
/// 使用 UTF8 编码将字符串转换为字节数组
137+
/// </summary>
138+
/// <param name="text">字符串</param>
139+
/// <returns>字节数组</returns>
140+
public static byte[] FromUTF8(this string text) => Encoding.UTF8.GetBytes(text);
141+
142+
/// <summary>
143+
/// 使用 UTF32 编码将字符串转换为字节数组
144+
/// </summary>
145+
/// <param name="text">字符串</param>
146+
/// <returns>字节数组</returns>
147+
public static byte[] FromUTF32(this string text) => Encoding.UTF32.GetBytes(text);
148+
149+
/// <summary>
150+
/// 使用 Unicode 编码将字符串转换为字节数组
151+
/// </summary>
152+
/// <param name="text">字符串</param>
153+
/// <returns>字节数组</returns>
154+
public static byte[] FromUnicode(this string text) => Encoding.Unicode.GetBytes(text);
155+
156+
/// <summary>
157+
/// 使用 ASCII 编码将字符串转换为字节数组
158+
/// </summary>
159+
/// <param name="text">字符串</param>
160+
/// <returns>字节数组</returns>
161+
public static byte[] FromASCII(this string text) => Encoding.ASCII.GetBytes(text);
119162
}

view-samples.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
dotnet run --project .\Common.BasicHelper.Samples\

view-samples.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
dotnet run --project ./Common.BasicHelper.Samples/
3+
4+

0 commit comments

Comments
 (0)