using System;
using System.IO;
namespace IStation.Untity
{
///
///
///
public class StreamHelper
{
private static readonly string[] _suffixes = new string[] { " B", " KB", " MB", " GB", " TB", " PB" };
///
/// 获取文件大小的显示字符串
///
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();
}
///
/// 获取可读尺寸
///
public static string GetReadableSize(Stream stream)
{
return BytesToReadableValue(stream.Length);
}
}
}