记录一个C#语言文件操作类
主要功能:
1、压缩和解压字符串 2、字符串数组转换
FileHelper.cs
/// <summary>
/// 文件帮助类
/// </summary>
public static class FileHelper
{
#region 压缩和解压字符串
/// <summary>
/// 将传入字符串以GZip算法压缩后,返回Base64编码字符
/// </summary>
/// <param name="rawString">需要压缩的字符串</param>
/// <returns>压缩后的Base64编码的字符串</returns>
public static string GZipCompressString(string rawString)
{
if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
{
return "";
}
else
{
byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
byte[] zippedData = Compress(rawData);
return (string)(Convert.ToBase64String(zippedData));
}
}
/// <summary>
/// GZip压缩
/// </summary>
/// <param name="rawData"></param>
/// <returns></returns>
public static byte[] Compress(byte[] rawData)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
compressedzipStream.Write(rawData, 0, rawData.Length);
compressedzipStream.Close();
return ms.ToArray();
}
/// <summary>
/// 解压Sring
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string GetStringByString(string Value)
{
string CC = GZipDecompressString(Value);
return CC;
}
/// <summary>
/// 将传入的二进制字符串资料以GZip算法解压缩
/// </summary>
/// <param name="zippedString">经GZip压缩后的二进制字符串</param>
/// <returns>原始未压缩字符串</returns>
public static string GZipDecompressString(string zippedString)
{
if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
{
return "";
}
else
{
byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));
}
}
/// <summary>
/// ZIP解压
/// </summary>
/// <param name="zippedData"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] zippedData)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(zippedData);
System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
System.IO.MemoryStream outBuffer = new System.IO.MemoryStream();
byte[] block = new byte[1024];
while (true)
{
int bytesRead = compressedzipStream.Read(block, 0, block.Length);
if (bytesRead <= 0)
break;
else
outBuffer.Write(block, 0, bytesRead);
}
compressedzipStream.Close();
return outBuffer.ToArray();
}
#endregion
/// <summary>
/// 文章转换base64字符串
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string BaseStrFromFile(string path)
{
if (File.Exists(path) == false)
return string.Empty;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
string basestr = string.Empty;
fs.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[fs.Length];
int log = Convert.ToInt32(fs.Length);
fs.Read(data, 0, log);
basestr = Convert.ToBase64String(data);
fs.Close();
return basestr;
}
}
catch (Exception ex)
{
Logger.logError(ex);
return string.Empty;
}
}
/// <summary>
/// 文章转换byte数组
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static byte[] ByteFromFileData(string path)
{
if (File.Exists(path) == false)
return null;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// string basestr = string.Empty;
fs.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[fs.Length];
int log = Convert.ToInt32(fs.Length);
fs.Read(data, 0, log);
//basestr = Convert.ToBase64String(data);
return data;
}
}
catch (Exception ex)
{
Logger.logError(ex);
return null;
}
}
/// <summary>
/// Base64字符串转文件并保存
/// </summary>
/// <param name="data">base64字符串</param>
/// <param name="fileName">保存的文件名</param>
/// <returns>是否转换并保存成功</returns>
public static bool ByteToFile(byte[] data, string fileFullPath)
{
bool opResult = false;
try
{
if (data != null && data.Length > 0)
{
string strDate = DateTime.Now.ToString("yyyyMMdd");
using (FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.Write(data, 0, data.Length);
fs.Close();
}
}
opResult = true;
}
catch (Exception e)
{
Logger.logError("异常类型: \t" + e.GetType());
Logger.logError("异常描述:\t" + e.Message);
Logger.logError("异常方法:\t" + e.TargetSite);
Logger.logError("异常堆栈:\t" + e.StackTrace);
}
return opResult;
}
/// <summary>
/// Base64字符串转文件并保存
/// </summary>
/// <param name="base64String">base64字符串</param>
/// <param name="fileName">保存的文件名</param>
/// <returns>是否转换并保存成功</returns>
public static bool Base64StringToFile(string base64String, string fileFullPath)
{
bool opResult = false;
try
{
string strDate = DateTime.Now.ToString("yyyyMMdd");
string strbase64 = base64String.Trim().Substring(base64String.IndexOf(",") + 1); //将‘,’以前的多余字符串删除
MemoryStream stream = new MemoryStream(Convert.FromBase64String(strbase64));
using (FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] b = stream.ToArray();
fs.Write(b, 0, b.Length);
fs.Close();
}
opResult = true;
}
catch (Exception e)
{
Logger.logError("异常类型: \t" + e.GetType());
Logger.logError("异常描述:\t" + e.Message);
Logger.logError("异常方法:\t" + e.TargetSite);
Logger.logError("异常堆栈:\t" + e.StackTrace);
}
return opResult;
}
}