使用CLR函式壓縮(Gzip)ntext型別欄位
Gzip to String
String to Gzip
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.IO.Compression;
using System.Text;
public partial class Gzip
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlChars GzipToString(SqlBytes gBytes)
{
byte[] bytes = gBytes.Value;
bytes = Decompress(bytes);
string str = Encoding.GetEncoding(936).GetString(bytes);
SqlChars sqlchars = new SqlChars(str);
return (sqlchars);
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlBytes StringToGzip(SqlChars chars)
{
byte[] bytes = Encoding.GetEncoding(936).GetBytes(chars.Buffer);
bytes = Compress(bytes);
SqlBytes gBytes = new SqlBytes(bytes);
return (gBytes);
}
#region 採用.net系統自帶Gzip壓縮類進行流壓縮
/**/
///
/// 壓縮資料
///
///
///
public static byte[] Compress(byte[] data)
{
byte[] bData;
MemoryStream ms = new MemoryStream();
GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
stream.Write(data, 0, data.Length);
stream.Close();
stream.Dispose();
//必須把stream流關閉才能返回ms流資料,不然資料會不完整
//並且解壓縮方法stream.Read(buffer, 0, buffer.Length)時會返回0
bData = ms.ToArray();
ms.Close();
ms.Dispose();
return bData;
}
/**/
///
/// 解壓資料
///
///
///
public static byte[] Decompress(byte[] data)
{
byte[] bData;
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Position = 0;
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress, true);
byte[] buffer = new byte[1024];
MemoryStream temp = new MemoryStream();
int read = stream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
temp.Write(buffer, 0, read);
read = stream.Read(buffer, 0, buffer.Length);
}
//必須把stream流關閉才能返回ms流資料,不然資料會不完整
stream.Close();
stream.Dispose();
ms.Close();
ms.Dispose();
bData = temp.ToArray();
temp.Close();
temp.Dispose();
return bData;
}
#endregion
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/66009/viewspace-1059288/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用gzip壓縮檔案
- Apache 開啟gzip壓縮Apache
- 使用filter實現網站的gzip壓縮Filter網站
- 關於Java的GZIP壓縮與.net C#的GZIP壓縮的差異JavaC#
- 前端效能優化gzip初探(補充gzip壓縮使用演算法brotli壓縮的相關介紹)前端優化演算法
- 給Tomcat,Apache配置gzip壓縮(HTTP壓縮)功能 (轉)TomcatApacheHTTP
- 保留兩位小數:資料庫欄位型別NUMBER,Java欄位型別Double型別資料庫型別Java
- Vue開啟gzip壓縮檔案Vue
- Apache開啟GZIP壓縮功能方法Apache
- vue-cli3.0配置GZIP壓縮Vue
- 高效能 gzip 壓縮工具 pgzip
- MongoDB更改欄位型別MongoDB型別
- oracle的欄位型別Oracle型別
- andFilterWhere()函式找不出某個int型別欄位為0的資料Filter函式型別
- 關gzip壓縮,我有新發現
- Android總結之Gzip/Zip壓縮Android
- linux下gzip的壓縮詳解Linux
- sql語句修改欄位型別和增加欄位SQL型別
- MySQL欄位型別最全解析MySql型別
- date、timestamp欄位型別型別
- MySQL欄位型別小記MySql型別
- 資料欄位型別匹配型別
- 修改表的欄位型別型別
- sqlite sql 修改欄位型別SQLite型別
- JSON欄位型別在ORM中的使用JSON型別ORM
- nginx快取配置及開啟gzip壓縮Nginx快取
- Apache開啟gzip壓縮提高網站速度Apache網站
- Java和.NET的GZIP壓縮功能對比Java
- 【mongo】mongo 欄位型別互轉Go型別
- [轉]MySQL 欄位型別參考MySql型別
- MongoDB中的欄位型別IdMongoDB型別
- oracle 修改欄位型別的方法Oracle型別
- 欄位型別檢測指令碼型別指令碼
- 比較所有的欄位型別型別
- MySQL欄位新增註釋,但不改變欄位的型別MySql型別
- Java實現檔案壓縮與解壓[zip格式,gzip格式]Java
- [提問交流]建立模型,新增屬性,欄位型別如何設定2位小數的欄位型別模型型別
- 伺服器端如何開啟GZIP壓縮功能伺服器