GZip对字符串压缩和解压
/// <summary>
/// 压缩
/// </summary>
/// <param name="value">需要压缩字符串</param>
/// <returns>结果</returns>
public static string Compression(string value)
{
byte[] data = Encoding.UTF8.GetBytes(value);
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true))
{
compressedzipStream.Write(data, , data.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
} /// <summary>
/// 解压
/// </summary>
/// <param name="value">需要解压字符串</param>
/// <returns>结果</returns>
public static string Decompress(string value)
{
byte[] data = Convert.FromBase64String(value);
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress))
{
using (MemoryStream outBuffer = new MemoryStream())
{
byte[] block = new byte[];
int bytesRead = compressedzipStream.Read(block, , block.Length);
outBuffer.Write(block, , bytesRead);
return Encoding.UTF8.GetString(outBuffer.ToArray());
}
}
}
}
/// <summary>
/// 解压(数据量过大的时候解压)
/// </summary>
/// <param name="value">需要解压字符串</param>
/// <returns>结果</returns>
public static string Decompress(string value)
{
byte[] data = Convert.FromBase64String(value);
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress))
{
using (MemoryStream outBuffer = new MemoryStream())
{
byte[] block = new byte[];
int bytesRead;
do
{
bytesRead = compressedzipStream.Read(block, , block.Length);
outBuffer.Write(block, , bytesRead);
} while (bytesRead > );
return Encoding.UTF8.GetString(outBuffer.ToArray());
}
}
}
}
/// <summary>
/// 测试
/// </summary>
public static void TT()
{
var json = "{\"Key\":\"Value\"}";
var ys = Compression(json);
var jy = Decompress(ys);
}
GZip对字符串压缩和解压的更多相关文章
- C# 使用GZip对字符串压缩和解压
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- C# GZip对字符串压缩和解压
/// <summary> /// 压缩方法 /// </summary> public static string CompressString(string str) { ...
- Linux下的压缩和解压
1. gzip, bzip2 能否直接压缩目录呢?不可以 2. 请快速写出,使用gzip和bzip2压缩和解压一个文件的命令.压缩:gzip 1.txt bzip2 1.txt解压:gzip -d 1 ...
- 对数据进行GZIP压缩和解压
public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...
- C#实现通过Gzip来对数据进行压缩和解压
C#实现通过Gzip来对数据进行压缩和解压 internal static byte[] Compress(byte[] data) { using (var compressedStream = n ...
- VB6进行GZIP解压&C#进行GZIP压缩和解压
VB进行GZIP解压的,DLL是系统的,如果没有 [点击下载] Option Explicit 'GZIP API '----------------------------------------- ...
- 使用pako.js实现gzip的压缩和解压
poko.js可至Github下载:https://github.com/nodeca/pako 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
- Linux 时间日期类、搜索查找类、 压缩和解压类指令
l 时间日期类 date指令-显示当前日期 基本语法 1) date (功能描述:显示当前时间) 2) date +%Y (功能描述:显示当前年份) 3) date +%m (功能描述:显示当前月份) ...
- linux常用命令:4文件压缩和解压命令
文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...
随机推荐
- Golang并发编程有缓冲通道和无缓冲通道(channel)
无缓冲通道 是指在接收前没有能力保存任何值得通道.这种类型的通道要求发送goroutine和接收goroutine同时准备好,才能完成发送和接收操作.如果两个goroutine没有同时准备好,通道会导 ...
- End of script output before headers错误解决方法
注意是否丢失两个换行符\n\n printf("Content-type: text/html;charset=utf-8\n\n");
- mysql Out of range value adjusted for column导致Warning(1265)Data truncated for column 'column_name' at row 1
今天下午,我们的一个开发来找我,说线上有个环境报了"Warning(1265)Data truncated for column 'column_name' at row 1",定 ...
- Zookeeper .Net客户端代码
本来此客户端可以通过NuGet获取,如果会使用NuGet, 则可以使用命令Install-Package ZooKeeperNet(需要最新版本的NuGet) 如果不会,就去 NuGet官网了解htt ...
- openvpn server setup
Set Up an OpenVPN Server client access 内部环境搭建
- shell实现每天0点备份mysql数据库
就两个文件, 本人学识尚浅,不解释,怕大佬喷. back.sh #/bin/bash MYSQLUSER=root MYSQLPWD=lizhenghua DATABASES=zskdb MYSQLD ...
- ORA-16038 ORA-19809 ORA-00312
问题表现: 连接数据库启动报错,ORA-03113, 查看详细的alert日志发现更多报错,如下 ORA-19809: 超出了恢复文件数的限制ORA-19804: 无法回收 209715200 字节磁 ...
- topcoder srm 315 div1
problem1 link 直接模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; publi ...
- Ubuntu: repository/PPA 源
在Ubuntu中,每个PPA源是单独存放在/etc/apt/sources.list.d/文件夹中的,进入到该文件夹,使用ls命令查询即可列出当前系统添加的PPA源. 添加 sudo add-apt- ...
- 更新32位Spyder从3.0.0-> 3.2.3
https://stackoverflow.com/questions/51222550/how-to-update-spyder-3-3-0 It works!! 1. went to the An ...