lixiaojun
2022-09-02 d5a3d08601fa59498edc092cec23c7ca166a0d15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Untity
{
    /// <summary>
    /// 
    /// </summary>
    public class StreamHelper
    {
        private static readonly string[] _suffixes = new string[] { " B", " KB", " MB", " GB", " TB", " PB" }; 
 
        /// <summary>
        /// 获取文件大小的显示字符串
        /// </summary>
        private static string BytesToReadableValue(long number)
        {
            double last = 1;
            for (int i = 0; i < _suffixes.Length; i++)
            {
                var current = Math.Pow(1024, i + 1);
                var temp = number / current;
                if (temp < 1)
                {
                    return (number / last).ToString("n2") + _suffixes[i];
                }
                last = current;
            }
            return number.ToString();
        }
 
        /// <summary>
        /// 获取可读尺寸
        /// </summary>
        public static string GetReadableSize(Stream stream) 
        {
            return BytesToReadableValue(stream.Length);
        }
 
    }
}