字符串WebService上先zip压缩再base64编码后传输
来源:互联网 发布:数据魔方没有了吗 编辑:程序博客网 时间:2024/05/21 06:54
先对字符串进行zip压缩,再进行base64编码
public string ZipBase64(string xml) { //压缩 string compressStr = ""; byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(xml); try { MemoryStream ms = new MemoryStream(); GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true); zip.Write(compressBeforeByte, 0, compressBeforeByte.Length); zip.Close(); byte[] buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); ms.Close(); compressStr= Convert.ToBase64String(buffer); } catch (Exception e) { throw new Exception(e.Message); } //base64编码 byte[] encData_byte = new byte[compressStr.Length]; encData_byte = System.Text.Encoding.UTF8.GetBytes(compressStr); xml= Convert.ToBase64String(encData_byte); return xml; }
得到zip压缩再base64编码的字符串后对其进行base64解码再解压
public string UnzipBase64(string xml) { //base64解码 System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(xml); int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); string decoded = new String(decoded_char); //解压缩 byte[] compressBeforeByte = Convert.FromBase64String(decoded); byte[] buffer = new byte[0x1000]; try { MemoryStream ms = new MemoryStream(compressBeforeByte); GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true); MemoryStream msreader = new MemoryStream(); while (true) { int reader = zip.Read(buffer, 0, buffer.Length); if (reader <= 0) { break; } msreader.Write(buffer, 0, reader); } zip.Close(); ms.Close(); msreader.Position = 0; buffer = msreader.ToArray(); msreader.Close(); } catch (Exception e) { throw new Exception(e.Message); } byte[] compressAfterByte = buffer; xml = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); return xml; }
- 字符串WebService上先zip压缩再base64编码后传输
- 图片压缩,Base64编码后上传服务器
- 字符串压缩,先zlib再base64转码示例(以Delphi为例)
- Android 图片先gzip压缩然后在Base64转成字符串
- WebService压缩传输
- 使用图片的base64编码字符串压缩图片代码
- 在传输层上压缩WebService的请求和响应
- iOS网路传输zip压缩
- zip压缩&解压缩字符串
- 字符串的编码Base64
- 字符串base64编码
- C# XML 分包、ZIP压缩、BASE64转码
- android zip压缩base64解密加密
- js压缩上传的图片,并转化为base64字符串传输
- 对文件进行base64编码成字符串进行保存或传输
- Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示
- Java 字符串压缩ZIP GZIP
- XML传输图片数据-Base64编码
- 也谈SDH、MSTP、OTN和PTN的区别和联系
- 解决mysql的“Access denied for user ‘root’@'localhost’”
- 设计优秀API的五大规则
- XP如何自动登录?
- 如何让表格自动换行
- 字符串WebService上先zip压缩再base64编码后传输
- 有人可以为你笑,有人可以为你哭
- 从事软件开发工作前应该做好哪些准备?
- 很多人总喜欢争对错输赢
- Bootstrap栅格系统研究
- 简述amixer及其用法
- Red Hat Enterprise 5的root密码忘记
- 关于 Beep、MessageBeep 和 Windows.Beep
- Visual Studio 遇到了异常。这可能是由某个扩展导致的。