// php GZip压缩 gzencode($data); // php GZip解压缩 gzinflate(substr($data,10,-8)); // 高版本的php已经拥有名为【gzdecode】的api
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.GZip; public class GZipHelper { public static byte[] EncodeRaw(byte[] bytes) { MemoryStream ms = new MemoryStream(); GZipOutputStream gz = new GZipOutputStream(ms); gz.Write(bytes, 0, bytes.Length); gz.Close(); byte[] ret = ms.ToArray(); ms.Close(); return ret; } public static byte[] DecodeRaw(byte[] bytes) { MemoryStream des = new MemoryStream(); MemoryStream ms = new MemoryStream(bytes); GZipInputStream gz = new GZipInputStream(ms); int count = 0; int offset = 0; byte[] buf = new byte[1024 * 1024]; do { count = gz.Read(buf, 0, buf.Length); des.Write(buf, offset, count); offset += count; } while (count > 0); gz.Close(); byte[] ret = des.ToArray(); ms.Close(); return ret; } }
C#压缩算法扩展包:http://icsharpcode.github.io/SharpZipLib/
注意:
1. C#端传入的参数为byte[],php端使用$str去承接。
2. C#端不要使用System.Text.Encoding.Default.GetBytes(bytes)去转字符串,C#端各种byte[]到string之间的转换都会产生和php之间的冲突。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.GZip; public class GZipHelper { public static byte[] EncodeRaw(byte[] bytes) { MemoryStream ms = new MemoryStream(); GZipOutputStream gz = new GZipOutputStream(ms); gz.Write(bytes, 0, bytes.Length); gz.Close(); byte[] ret = ms.ToArray(); ms.Close(); return ret; } public static byte[] DecodeRaw(byte[] bytes) { MemoryStream des = new MemoryStream(); MemoryStream ms = new MemoryStream(bytes); GZipInputStream gz = new GZipInputStream(ms); int count = 0; int offset = 0; byte[] buf = new byte[1024 * 1024]; do { count = gz.Read(buf, 0, buf.Length); des.Write(buf, offset, count); offset += count; } while (count > 0); gz.Close(); byte[] ret = des.ToArray(); ms.Close(); return ret; } }
C#压缩算法扩展包:http://icsharpcode.github.io/SharpZipLib/
注意:
1. C#端传入的参数为byte[],php端使用$str去承接。
2. C#端不要使用System.Text.Encoding.Default.GetBytes(bytes)去转字符串,C#端各种byte[]到string之间的转换都会产生和php之间的冲突。
扫描二维码关注公众号,回复:
642141 查看本文章