gzip
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Web; 5 using System.IO; 6 using System.Text.RegularExpressions; 7 using System.Xml; 8 using System.IO.Compression; 9 10 public class txtdata 11 { 12 /// <summary> 13 /// 14 /// </summary> 15 /// <param name="filename"></param> 16 /// <param name="content"></param> 17 /// <returns></returns> 18 public static string txtwrite(string filename, string content) 19 { 20 string restr = ""; 21 try 22 { 23 content = CompressString(content); 24 FileStream fs = new FileStream(filename, FileMode.Create); 25 //获得字节数组 26 byte[] data = System.Text.Encoding.UTF8.GetBytes(content); 27 //开始写入 28 fs.Write(data, 0, data.Length); 29 //清空缓冲区、关闭流 30 fs.Flush(); 31 fs.Close(); 32 restr = "写入成功"; 33 } 34 catch (Exception ex) 35 { 36 ex.ToString(); 37 restr = "error"; 38 } 39 return restr; 40 } 41 42 /// <summary> 43 /// 44 /// </summary> 45 /// <param name="path"></param> 46 /// <returns></returns> 47 public static string txtread(string path) 48 { 49 string restr = ""; 50 try 51 { 52 StreamReader sr = new StreamReader(path, Encoding.UTF8); 53 string r1 = sr.ReadToEnd(); 54 sr.Close(); 55 restr = DecompressString(r1); 56 } 57 catch (Exception ex) 58 { 59 restr = "error"; 60 } 61 return restr; 62 } 63 64 /// <summary> 65 /// stream转为string 66 /// </summary> 67 /// <param name="sw"></param> 68 /// <returns></returns> 69 70 public static string stream_to_string(Stream sw) 71 { 72 StreamReader reader = new StreamReader(sw); 73 string str1 = reader.ReadToEnd(); 74 return str1; 75 } 76 77 /// <summary> 78 /// 将c# DateTime时间格式转换为Unix时间戳格式 79 /// </summary> 80 /// <param name="time"></param> 81 /// <returns></returns> 82 public static long ConvertDateTimeInt(System.DateTime time) 83 { 84 //double intResult = 0; 85 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); 86 //intResult = (time- startTime).TotalMilliseconds; 87 long t = (time.Ticks - startTime.Ticks) / 10000000; //除10000调整为13位 88 return t; 89 } 90 91 /// <summary> 92 /// 将Unix时间戳转换为DateTime类型时间 93 /// </summary> 94 /// <param name="d">double 型数字</param> 95 /// <returns>DateTime</returns> 96 public static string ConvertIntDateTime(double d) 97 { 98 System.DateTime time = System.DateTime.MinValue; 99 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 100 time = startTime.AddSeconds(d); 101 return time.ToString(); ; 102 } 103 104 /// <summary> 105 ///生成随机数 106 /// </summary> 107 /// <param name="max"></param> 108 /// <param name="min"></param> 109 /// <returns></returns> 110 public static int random_num(int max, int min = 0) 111 { 112 Random ran = new Random((int)DateTime.Now.Ticks); 113 int n = ran.Next(min, max); 114 System.Threading.Thread.Sleep(1); 115 return n; 116 } 117 /// <summary> 118 /// 字节数组压缩 119 /// </summary> 120 /// <param name="strSource"></param> 121 /// <returns></returns> 122 public static byte[] Compress(byte[] data) 123 { 124 try 125 { 126 MemoryStream ms = new MemoryStream(); 127 GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true); 128 zip.Write(data, 0, data.Length); 129 zip.Close(); 130 byte[] buffer = new byte[ms.Length]; 131 ms.Position = 0; 132 ms.Read(buffer, 0, buffer.Length); 133 ms.Close(); 134 return buffer; 135 136 } 137 catch (Exception e) 138 { 139 throw new Exception(e.Message); 140 } 141 } 142 /// <summary> 143 /// 字节数组解压缩 144 /// </summary> 145 /// <param name="strSource"></param> 146 /// <returns></returns> 147 public static byte[] Decompress(byte[] data) 148 { 149 try 150 { 151 MemoryStream ms = new MemoryStream(data); 152 GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true); 153 MemoryStream msreader = new MemoryStream(); 154 byte[] buffer = new byte[0x1000]; 155 while (true) 156 { 157 int reader = zip.Read(buffer, 0, buffer.Length); 158 if (reader <= 0) 159 { 160 break; 161 } 162 msreader.Write(buffer, 0, reader); 163 } 164 zip.Close(); 165 ms.Close(); 166 msreader.Position = 0; 167 buffer = msreader.ToArray(); 168 msreader.Close(); 169 return buffer; 170 } 171 catch (Exception e) 172 { 173 throw new Exception(e.Message); 174 } 175 } 176 177 /// <summary> 178 /// 字符串压缩 179 /// </summary> 180 /// <returns>The string.</returns> 181 /// <param name="str">String.</param> 182 public static string CompressString(string str) 183 { 184 string compressString = ""; 185 byte[] compressBeforeByte = Encoding.UTF8.GetBytes(str); 186 byte[] compressAfterByte = Compress(compressBeforeByte); 187 //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); 188 compressString = Convert.ToBase64String(compressAfterByte); 189 return compressString; 190 } 191 /// <summary> 192 /// 字符串解压缩 193 /// </summary> 194 /// <returns>The string.</returns> 195 /// <param name="str">String.</param> 196 public static string DecompressString(string str) 197 { 198 string compressString = ""; 199 //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); 200 byte[] compressBeforeByte = Convert.FromBase64String(str); 201 byte[] compressAfterByte = Decompress(compressBeforeByte); 202 compressString = Encoding.UTF8.GetString(compressAfterByte); 203 return compressString; 204 } 205 206 }
这是一个带GZIP压缩的txt写入类,可以有效减小写入文件大小,提升写入读取速度大约20倍