Zlib.net 解压压缩 数据流 对应java 使用
来源:互联网 发布:不编程的app 编辑:程序博客网 时间:2024/06/04 19:24
首先下载:http://zlibnet.codeplex.com/ 最新的zlib.net.dll
对应java 压缩解压的要导入的包为:import java.util.zip.Deflater;
这里吐槽下,为毛java 有自带的 zlib的算法,为毛.net 没有啊!
1 、 .net 的压缩和解压 方法
/// <summary> /// zlib.net 解压函数 /// </summary> /// <param name="strSource">带解压数据源</param> /// <returns>解压后的数据</returns> public static string DeflateDecompress(string strSource) { int data = 0; int stopByte = -1; byte[] Buffer = Convert.FromBase64String(strSource); // 解base64 MemoryStream intms = new MemoryStream(Buffer); zlib.ZInputStream inZStream = new zlib.ZInputStream(intms); int count = 1024 * 1024; byte[] inByteList = new byte[count]; int i = 0; while (stopByte != (data = inZStream.Read())) { inByteList[i] = (byte)data; i++; } inZStream.Close(); return System.Text.Encoding.UTF8.GetString(inByteList, 0, inByteList.Length); } /// <summary> /// zlib.net 压缩函数 /// </summary> /// <param name="strSource">待压缩数据</param> /// <returns>压缩后数据</returns> public static string DeflateCompress(string strSource) { MemoryStream outms = new MemoryStream(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strSource); MemoryStream inms = new MemoryStream(bytes); zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outms, zlib.zlibConst.Z_DEFAULT_COMPRESSION); try { CopyStream(inms, outZStream); } catch (Exception ex) { throw ex; } finally { outZStream.Close(); } return Convert.ToBase64String(outms.ToArray()); } public static void CopyStream(System.IO.Stream input, System.IO.Stream output) { byte[] buffer = new byte[2000]; int len; while ((len = input.Read(buffer, 0, 2000)) > 0) { output.Write(buffer, 0, len); } output.Flush(); }
2 、 对应java 的压缩和解压函数
/** * 解压. * */public static byte[] inflater(final byte[] inputByte) throws IOException {int compressedDataLength = 0;Inflater compresser = new Inflater(false);compresser.setInput(inputByte, 0, inputByte.length);ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);byte[] result = new byte[1024];try {while (!compresser.finished()) {compressedDataLength = compresser.inflate(result);if (compressedDataLength == 0) {break;}o.write(result, 0, compressedDataLength);}} catch (Exception ex) {System.err.println("Data format error!\n");ex.printStackTrace();} finally {o.close();}compresser.end();return o.toByteArray();}/** * 压缩. * */public static byte[] deflater(final byte[] inputByte) throws IOException {int compressedDataLength = 0;Deflater compresser = new Deflater();compresser.setInput(inputByte);compresser.finish();ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);byte[] result = new byte[1024];try {while (!compresser.finished()) {compressedDataLength = compresser.deflate(result);o.write(result, 0, compressedDataLength);}} finally {o.close();}compresser.end();return o.toByteArray();}/** * BASE64解码 * * @param inputByte 待解码数据 * @return 解码后的数据 * @throws IOException */public static byte[] base64Decode(byte[] inputByte) throws IOException {return Base64.decodeBase64(inputByte);}/** * BASE64编码 * * @param inputByte 待编码数据 * @return 解码后的数据 * @throws IOException */public static byte[] base64Encode(byte[] inputByte) throws IOException {return Base64.encodeBase64(inputByte);}
0 0
- Zlib.net 解压压缩 数据流 对应java 使用
- 使用zlib压缩/解压文件
- C#使用 DeflateStream类 解压zlib数据流
- 转贴: 【python】使用zlib进行压缩解压
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用 ZLib 压缩/解压 ZIP 文件
- zlib压缩和解压的使用
- 使用开源库zlib压缩和解压文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用zlib压缩和解压gzip
- 使用开源库zlib压缩和解压文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用开源库zlib压缩和解压文件
- lua使用zlib压缩和解压
- zlib压缩解压库
- zlib压缩解压
- zlib压缩解压库
- mongoose源码分析系列之listening_sock
- [BZOJ1303][CQOI2009]中位数图
- 超宽带射频信号高速记录回放系统——模拟带宽3GHZ,记录回放速度高达6000MB/S!
- mongoose源码分析系列之active_connections
- poj 3237 Tree(树链剖分)
- Zlib.net 解压压缩 数据流 对应java 使用
- java如何添加外部包
- 对快速排序的分析
- Go语言 1.3主要改动翻译
- Cocos2dx如何引用第三方SO文件(Android NDK)
- 查找svn版本大于当前版本的文件夹
- android网络编程
- 打印日志的10个建议
- asp.net数学运算表达式形式的验证码