c#实现gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
转载:https://blog.csdn.net/luanpeng825485697/article/details/78165788
我测试了下压缩byte[],是可以的
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using UnityEngine; public class TestByteAttay : MonoBehaviour { // Use this for initialization
void Start () {
//测试字符串
string inputStr = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org";
print("原文:\t" + inputStr); byte[] input = System.Text.Encoding.Default.GetBytes(inputStr);
print("长度:\t" + input.Length); byte[] data = gzipCompress(input);
print("压缩后:\t");
print("长度:\t" + data.Length);
} // Update is called once per frame
void Update () { } void YaSuo()
{ } //gzip字节数组压缩
public static byte[] gzipCompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(data, , data.Length);
zip.Close();
byte[] buffer = new byte[ms.Length];
ms.Position = ;
ms.Read(buffer, , buffer.Length);
ms.Close();
return buffer; }
catch (Exception e)
{
throw new Exception(e.Message);
}
} //gzip字节数组解压缩
public static byte[] gzipDecompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
MemoryStream msreader = new MemoryStream();
byte[] buffer = new byte[0x1000];
while (true)
{
int reader = zip.Read(buffer, , buffer.Length);
if (reader <= )
{
break;
}
msreader.Write(buffer, , reader);
}
zip.Close();
ms.Close();
msreader.Position = ;
buffer = msreader.ToArray();
msreader.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
c#实现gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩的更多相关文章
- spring boot 使用WebSocket与前端进行byte字节数组交互
一.装逼前先热热身 无论是比较传统的 web项目 还是近几年流行的前后端分离,后端只独立提供数据交互接口服务的项目,都避免不了数据之间交互格式的选择. 从很早之前的 xml 格式 到现在最火热的jso ...
- (八)二进制文件在webservice中的处理(以byte[]字节数组方式)
一.介绍 二进制文件在webservice中的处理. A:通过byte[]字节数组的方式来传递.比较适合小文件,字节数组不能太大的情况.(本章所用) B:通过DataHander的方式来进行传递. 1 ...
- Atitit.字节数组转字符串 base64 base16 Quoted-printable 编码原理设计 attilax 总结
Atitit.字节数组转字符串 base64 base16 Quoted-printable 编码原理设计 attilax 总结 1. Base64编码, 1 2. Base64实现转换原理1 3. ...
- C# 进制转换 在什么情况下使用16进制,字节数组,字符串
C# 进制转换 Admin2013年9月18日 名人名言:从工作里爱了生命,就是通彻了生命最深的秘密.——纪伯伦 1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串 //十进制转二进制Con ...
- byte字节数组的压缩
写入内容到文件 public static void writeBytesToFile() throws IOException{ String s = "aaaaaaaaD等等" ...
- golang byte转string 字节数组转字符串的问题
golang语言本身就是c的工具集,开发c的程序用到的大部分结构体,内存管理,携程等,golang基本都有,他只是在这个基础上又加了一些概念这里说一个很小的问题,就是字节数组转string的问题,网上 ...
- Java 中的字符串与 []byte 字节数组
一.字符串 1.比较 String.HashSet.List 中的 contains 方法 其中, String.List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n).而 Has ...
- Java之字节数组和字符串的转换问题
今天在使用字节流复制图片的时候发现一个问题,就是将字节数组转化为字符串的时候会出现长度不同的问题.这其实是个错误的操作. public static void main(String[] args) ...
- 使用 Itext 生成PDF字节数组(文件流不落地)
package com.ulic.gis.customerCenter.controller; import java.io.ByteArrayOutputStream; import java.io ...
随机推荐
- Ubuntu下使用UFW,以及CentOS7的默认防火墙firewalld
UFW是一个简化版的iptables,基于iptables,配置比iptables简单 默认UFW是关闭状态,即Ubuntu默认打开所有端口,比较危险. 检测状态 ufw status 设置默认状态, ...
- [LeetCode 题解]: Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [Erlang11] 那些经历过的Erlang小坑11-20
11.每次重装系统时都会重新安装Erlang,Ubuntu安装sh秒杀一切. https://gist.github.com/zhongwencool/11174620 12. Erlang Shel ...
- Transaction And Lock--锁相关基础
--=======================================================--锁提示--holdlock :将共享锁保留到事务完成,而不是在相应的表.行或数据页 ...
- UML uml高级知识之用例图
uml高级知识之用例图 建模工具推荐使用 visio2010: include:选择菜单栏中的'UML'->单击’构造型‘->新建->构造型那里输入include->基类那里选 ...
- 《Beginning Java 7》 - 2 - Cloning 克隆
Cloning 分两类:影子克隆 shallow cloning 深度克隆 deep cloning * 调用 clone() 需要 implments Cloneable.此函数为 protecte ...
- 870. Advantage Shuffle
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...
- 6.iptables常用规则
开启ip段192.168.1.0/24端的80口 开启ip段211.123.16.123/24端ip段的80口 # iptables -I INPUT -p tcp --dport 80 -j DRO ...
- “全栈2019”Java第七十九章:类中可以嵌套接口吗?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Postman使用手册4——API test
一.Pre Request Scripts Postman v0.10+ 版本支持pre-request scripts. prerequest script.png pre-request scri ...