StringExtension (压缩)

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace Ctrip.Domestic.Adapter.Utility
{
public static class StringExtension
{
public static bool IsCnString(this string str)
{
if (string.IsNullOrEmpty(str))
{
return false;
}

return System.Text.RegularExpressions.Regex.IsMatch(str, @"^[\u4e00-\u9fa5]+$");
}

public static string ToDateTimeStr(this string str)
{
if (!string.IsNullOrEmpty(str))
{
var year = "2015";
var month = "01";
var day = "01";
var hour = "00";
var minute = "00";
var second = "00";

if (str.Length >= 8)
{
year = str.Substring(0, 4);
month = str.Substring(4, 2);
day = str.Substring(6, 2);
}

if (str.Length >= 12)
{
hour = str.Substring(8, 2);
minute = str.Substring(10, 2);
}

if (str.Length == 14)
{
second = str.Substring(12, 2);
}

int parseValue = 0;
if (int.TryParse(year, out parseValue) && int.TryParse(month, out parseValue)
&& int.TryParse(day, out parseValue) && int.TryParse(hour, out parseValue)
&& int.TryParse(minute, out parseValue) && int.TryParse(second, out parseValue))
{
return string.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second);
}
}


return string.Empty;
}

public static string GetSubstring(this string strValue, int length)
{
if (strValue != null && strValue.Length > length)
{
return strValue.Substring(0, length);
}

return strValue;
}

public static string Compress(this string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}

byte[] inputBytes = Encoding.Default.GetBytes(input);
byte[] result = Compress(inputBytes);
return Convert.ToBase64String(result);
}

public static string CompressUTF8(this string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}

byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] result = Compress(inputBytes);
return Convert.ToBase64String(result);

}
public static string Decompress(this string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}

byte[] inputBytes = Convert.FromBase64String(input);
byte[] depressBytes = Decompress(inputBytes);
return Encoding.Default.GetString(depressBytes);
}

public static byte[] Compress(this byte[] inputBytes)
{
using (MemoryStream outStream = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(outStream, CompressionMode.Compress, true))
{
zipStream.Write(inputBytes, 0, inputBytes.Length);
zipStream.Close();
return outStream.ToArray();
}
}
}

public static byte[] Decompress(this byte[] inputBytes)
{

using (MemoryStream inputStream = new MemoryStream(inputBytes))
{
using (MemoryStream outStream = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
zipStream.CopyTo(outStream);
zipStream.Close();
return outStream.ToArray();
}
}

}
}
}
}

转载于:https://www.cnblogs.com/qiliu/p/5412451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值