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