コード例 #1
0
        /// <summary>
        /// Get data, if contentEncodingType == 0, then data is compressed with zlib
        /// </summary>
        /// <param name="matroskaTrackInfo"></param>
        /// <returns>Data byte array (uncompressed)</returns>
        public byte[] GetData(MatroskaTrackInfo matroskaTrackInfo)
        {
            if (matroskaTrackInfo.ContentEncodingType != MatroskaTrackInfo.ContentEncodingTypeCompression ||  // no compression
                (matroskaTrackInfo.ContentEncodingScope & MatroskaTrackInfo.ContentEncodingScopeTracks) == 0) // tracks not compressed
            {
                return(Data);
            }

            var outStream  = new MemoryStream();
            var outZStream = new ZOutputStream(outStream);
            var inStream   = new MemoryStream(Data);

            byte[] buffer;
            try
            {
                MatroskaTrackInfo.CopyStream(inStream, outZStream);
                buffer             = new byte[outZStream.TotalOut];
                outStream.Position = 0;
                outStream.Read(buffer, 0, buffer.Length);
            }
            finally
            {
                outZStream.Close();
                inStream.Close();
            }
            return(buffer);
        }
コード例 #2
0
        public static byte[] Inflate(byte[] buffer)
        {
            var iStream = new MemoryStream(buffer);
            var oStream = new MemoryStream();
            var zStream = new ZOutputStream(oStream);

            try
            {
                CopyStream(iStream, zStream);
                zStream.finish();

                return(oStream.ToArray());
            }
            catch (Exception ex)
            {
                ServerContext.Logger(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
                ServerContext.Logger(ex.StackTrace, Microsoft.Extensions.Logging.LogLevel.Error);
                return(null);
            }
            finally
            {
                zStream.Close();
                oStream.Close();
                iStream.Close();
            }
        }
コード例 #3
0
ファイル: TestZip.cs プロジェクト: uvbs/DDTank-3.0
        public void Compress()
        {
            //压缩
            byte[]       byteData = System.Text.Encoding.UTF8.GetBytes("ul{\"state\":1,\"nick\":\"小龙\",\"sex\":true,\"email\":\"[email protected]\",\"exp\":109040,\"isguest\":false,\"hotpoint\":26,\"money\":7099,\"styletype\":\"stand\",\"style\":\"2/2/2/1\",\"id\":\"2ef38760-9000-4f10-b96b-af4da8c656ae\",\"vertify\":1,\"level\":12,\"sign\":\"小龙\",\"score\":6323}");
            MemoryStream ms       = new MemoryStream();
            Stream       s        = new ZOutputStream(ms, 9);

            s.Write(byteData, 0, byteData.Length);
            s.Close();
            byte[] compressData = (byte[])ms.ToArray();
            ms.Flush();
            ms.Close();
            Console.WriteLine(compressData.Length);

            //解压
            MemoryStream md = new MemoryStream();
            Stream       d  = new ZOutputStream(md);

            d.Write(compressData, 0, compressData.Length);
            d.Close();
            byte[] dompressData = (byte[])md.ToArray();
            md.Flush();
            md.Close();

            Console.WriteLine(dompressData.Length);
        }
コード例 #4
0
        /// <summary>
        /// Распаковать массив байт
        /// </summary>
        /// <param name="xmlInfo">Array for unpacking</param>
        /// <returns></returns>
        private static byte[] UnpackXML(byte[] xmlInfo)
        {
            var newByffer = new byte[xmlInfo.Length * 100];
            var stream    = new MemoryStream(newByffer);
            var zStream   = new ZOutputStream(stream);

            try
            {
                zStream.Write(xmlInfo, 0, xmlInfo.Length);
            }
            catch
            {
                MessageBox.Show("Ошибка в файле с базой фонотеки", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            zStream.Close();

            int i = xmlInfo.Length * 100 - 1;

            while (i >= 0 && newByffer[i] == 0)
            {
                i--;
            }

            var rez = new byte[i + 1];

            for (int j = 0; j <= i; j++)
            {
                rez[j] = newByffer[j];
            }

            return(rez);
        }
コード例 #5
0
        /// <summary>
        /// KRC歌词解密
        /// </summary>
        /// <param name="data">歌词加密数据</param>
        /// <returns></returns>
        private byte[] decodeKRC(byte[] data)
        {
            if (data[0] != 107 || data[1] != 114 || data[2] != 99 || data[3] != 49)
            {
                return(null);
            }
            byte[] key = { 64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105 }; //秘钥
            //解密
            for (int i = 4; i < data.Length; i++)
            {
                data[i - 4] = (byte)(data[i] ^ key[(i - 4) % 16]);
            }
            //zlib解压
            MemoryStream  outfile    = new MemoryStream();
            ZOutputStream outZStream = new ZOutputStream(outfile);

            byte[] ret;
            try
            {
                outZStream.Write(data, 0, data.Length - 4);
                outZStream.Flush();
                outfile.Flush();
                ret = outfile.ToArray();
            }
            finally
            {
                outZStream.Close();
            }
            return(ret);
        }
コード例 #6
0
        //壓縮送出
        private void SendZip(string method)
        {
            try
            {
                if (method == "POST")
                {
                    //將body壓縮
                    byte[]       bodyBytes = UTF8Encoding.UTF8.GetBytes(txbBody.Text);
                    MemoryStream ms        = new MemoryStream();
                    Stream       bodyZip   = new ZOutputStream(ms, zlibConst.Z_STREAM_END);
                    bodyZip.Write(bodyBytes, 0, bodyBytes.Length);
                    bodyZip.Close();
                    byte[] compressData = (byte[])ms.ToArray();
                    ms.Flush();
                    ms.Close();

                    //送出請求
                    hc.PostAsync(txbUrl.Text, new StreamContent(new MemoryStream(compressData)));
                }
                else
                {
                    hc.GetAsync(txbUrl.Text);
                }
            }catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
コード例 #7
0
ファイル: SendResult.cs プロジェクト: samdubey/NDG-for-WP
        private byte[] CompressResult(string strResult)
        {
            byte[] byteArrayIn = Encoding.UTF8.GetBytes(strResult);

            MemoryStream  outStream     = new MemoryStream();
            ZOutputStream zOutputStream = new ZOutputStream(outStream, zlibConst.Z_BEST_COMPRESSION);

            zOutputStream.Write(byteArrayIn, 0, byteArrayIn.Length);
            zOutputStream.Close();

            byte[] byteArrayOut = outStream.ToArray();

            byte[] compresedDataLengthBytes = BitConverter.GetBytes(byteArrayOut.Length);
            byte[] dataLengthBytes          = BitConverter.GetBytes(strResult.Length);

            byte[] bytesToSend = new byte[byteArrayOut.Length + 8];

            bytesToSend[0] = dataLengthBytes[3];
            bytesToSend[1] = dataLengthBytes[2];
            bytesToSend[2] = dataLengthBytes[1];
            bytesToSend[3] = dataLengthBytes[0];
            bytesToSend[4] = compresedDataLengthBytes[3];
            bytesToSend[5] = compresedDataLengthBytes[2];
            bytesToSend[6] = compresedDataLengthBytes[1];
            bytesToSend[7] = compresedDataLengthBytes[0];
            int index = 8;

            foreach (byte b in byteArrayOut)
            {
                bytesToSend[index] = b;
                index++;
            }
            return(bytesToSend);
        }
コード例 #8
0
        /// <summary>
        /// Упаковать массив байт
        /// </summary>
        /// <param name="xmlInfo">Array for packing</param>
        /// <returns></returns>
        private static byte[] PackXML(byte[] xmlInfo)
        {
            var newByffer = new byte[xmlInfo.Length * 100];
            var stream    = new MemoryStream(newByffer);
            var zStream   = new ZOutputStream(stream, zlibConst.Z_DEFAULT_COMPRESSION);

            zStream.Write(xmlInfo, 0, xmlInfo.Length);
            zStream.Close();

            int i = xmlInfo.Length * 100 - 1;

            while (i >= 0 && newByffer[i] == 0)
            {
                i--;
            }

            var rez = new byte[i + 1];

            for (int j = 0; j <= i; j++)
            {
                rez[j] = newByffer[j];
            }

            return(rez);
        }
コード例 #9
0
        public string GetCodecPrivate()
        {
            if (CodecPrivateRaw == null || CodecPrivateRaw.Length == 0)
            {
                return(string.Empty);
            }

            if (ContentEncodingType == ContentEncodingTypeCompression &&
                (ContentEncodingScope & ContentEncodingScopePrivateDate) > 0 && // second bit set = private data encoded
                CodecPrivateRaw.Length > 2)
            {
                var outStream  = new MemoryStream();
                var outZStream = new ZOutputStream(outStream);
                var inStream   = new MemoryStream(CodecPrivateRaw);
                try
                {
                    CopyStream(inStream, outZStream);
                    var buffer = new byte[outZStream.TotalOut];
                    outStream.Position = 0;
                    outStream.Read(buffer, 0, buffer.Length);
                    return(Encoding.UTF8.GetString(buffer));
                }
                catch
                {
                    return(Encoding.UTF8.GetString(CodecPrivateRaw));
                }
                finally
                {
                    outZStream.Close();
                    inStream.Close();
                }
            }
            return(Encoding.UTF8.GetString(CodecPrivateRaw));
        }
コード例 #10
0
        /// <summary>
        /// Unpack byte array
        /// </summary>
        /// <param name="xmlInfo">Array for unpacking</param>
        /// <returns></returns>
        public byte[] Unpack(byte[] xmlInfo)
        {
            byte[]        newByffer = new byte[xmlInfo.Length * 100];
            MemoryStream  stream    = new MemoryStream(newByffer);
            ZOutputStream zStream   = new ZOutputStream(stream);

            try
            {
                zStream.Write(xmlInfo, 0, xmlInfo.Length);
            }
            catch { }
            zStream.Close();

            int i = xmlInfo.Length * 100 - 1;

            while (i >= 0 && newByffer[i] == 0)
            {
                i--;
            }
            byte[] rez = new byte[i + 1];
            for (int j = 0; j <= i; j++)
            {
                rez[j] = newByffer[j];
            }
            return(rez);
        }
コード例 #11
0
        /**
         * Generate an object that contains an CMS Compressed Data
         */
        public CmsCompressedData Generate(
            CmsProcessable content,
            string compressionOid)
        {
            AlgorithmIdentifier comAlgId;
            Asn1OctetString     comOcts;

            try
            {
                MemoryStream  bOut = new MemoryStream();
                ZOutputStream zOut = new ZOutputStream(bOut, JZlib.Z_DEFAULT_COMPRESSION);

                content.Write(zOut);

                zOut.Close();

                comAlgId = new AlgorithmIdentifier(new DerObjectIdentifier(compressionOid));
                comOcts  = new BerOctetString(bOut.ToArray());
            }
            catch (IOException e)
            {
                throw new CmsException("exception encoding data.", e);
            }

            ContentInfo comContent  = new ContentInfo(CmsObjectIdentifiers.Data, comOcts);
            ContentInfo contentInfo = new ContentInfo(
                CmsObjectIdentifiers.CompressedData,
                new CompressedData(comAlgId, comContent));

            return(new CmsCompressedData(contentInfo));
        }
コード例 #12
0
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileName">被压缩文件名(必须输入绝对路径)</param>
        /// <param name="CompressedFileName">压缩后保存的文件名(必须输入绝对路径)</param>
        /// <returns></returns>
        public static bool CompressFile(string FileName, string CompressedFileName)
        {
            bool bResult = false;

            FileStream    outFileStream = new FileStream(CompressedFileName, FileMode.Create);
            ZOutputStream outZStream    = new ZOutputStream(outFileStream, zlibConst.Z_DEFAULT_COMPRESSION);
            FileStream    inFileStream  = new FileStream(FileName, FileMode.Open);

            try
            {
                CopyStream(inFileStream, outZStream);
                bResult = true;
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                outZStream.Close();
                outFileStream.Close();
                inFileStream.Close();
            }
            return(bResult);
        }
コード例 #13
0
        /**
         * 数据解压缩
         * _srcArr byte[]
         * _isCompress bool
         */
        protected byte[] DecompressPacket(byte[] _srcArr)
        {
            byte[] rtnArr;

            byte[]        tmpArr;
            MemoryStream  mInStream;
            MemoryStream  mOutStream;
            ZOutputStream zouStream;

            tmpArr     = new byte[_srcArr.Length];
            mInStream  = new MemoryStream();
            mOutStream = new MemoryStream();

            Buffer.BlockCopy(_srcArr, 0, tmpArr, 0, _srcArr.Length);
            mInStream.Write(tmpArr, 0, tmpArr.Length);
            mInStream.Position = 0;
            zouStream          = new ZOutputStream(mOutStream);
            try
            {
                CopyStream(mInStream, zouStream);
                rtnArr = new byte[mOutStream.Length];
                mOutStream.Position = 0;
                mOutStream.Read(rtnArr, 0, rtnArr.Length);
            }
            finally
            {
                mInStream.Close();
                zouStream.Close();
                mOutStream.Close();
            }

            return(rtnArr);
        }
コード例 #14
0
        public ZLibError Decompress(Stream inStream, Stream outStream, ref int destLength)
        {
            ZOutputStream outZStream = new ZOutputStream(outStream);

            outZStream.FlushMode = zlibConst.Z_FULL_FLUSH;

            try
            {
                CopyStream(inStream, outZStream);
                outZStream.Flush();
                outStream.Flush();
                destLength = (int)outStream.Position;
            }
            // TODO: Catch exceptions, return error codes
            //catch (ZStreamException)
            //{
            //    return ZLibError.StreamError;
            //}
            finally
            {
                outZStream.Close();
                inStream.Close();
            }

            return(ZLibError.Okay);
        }
コード例 #15
0
 public override void Close()
 {
     _out.Close();
     _eiGen.Close();
     _cGen.Close();
     _sGen.Close();
     base.Close();
 }
コード例 #16
0
ファイル: StaticFunction.cs プロジェクト: uvbs/DDTank-3.0
        /// <summary>
        /// Zip compress data with offset and length.
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] src, int offset, int length)
        {
            MemoryStream ms = new MemoryStream();
            Stream       s  = new ZOutputStream(ms, 9);

            s.Write(src, offset, length);
            s.Close();
            return((byte[])ms.ToArray());
        }
コード例 #17
0
ファイル: Marshal.cs プロジェクト: votruongan/TATank
        public static byte[] Uncompress(byte[] src)
        {
            MemoryStream memoryStream = new MemoryStream();
            Stream       stream       = new ZOutputStream(memoryStream);

            stream.Write(src, 0, src.Length);
            stream.Close();
            return(memoryStream.ToArray());
        }
コード例 #18
0
ファイル: StaticFunction.cs プロジェクト: uvbs/DDTank-3.0
        /// <summary>
        /// Zip uncompress data.
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static byte[] Uncompress(byte[] src)
        {
            MemoryStream md = new MemoryStream();
            Stream       d  = new ZOutputStream(md);

            d.Write(src, 0, src.Length);
            d.Close();
            return((byte[])md.ToArray());
        }
コード例 #19
0
        /// <summary>
        /// </summary>
        /// <param name="filename">
        /// </param>
        /// <param name="version">
        /// </param>
        /// <param name="dataList">
        /// </param>
        /// <param name="packCount">
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <exception cref="Exception">
        /// </exception>
        public static void CompressData <T>(string filename, string version, List <T> dataList, int packCount = 500)
        {
            Console.WriteLine("Compressing " + typeof(T).Name + "s");

            if (packCount == 0)
            {
                throw new Exception("Dont use 0 as packCount!!");
            }

            Stream fileStream   = new FileStream(filename, FileMode.Create);
            var    zipStream    = new ZOutputStream(fileStream, zlibConst.Z_BEST_COMPRESSION);
            var    bufferStream = new MemoryStream(20 * 1024 * 1024);

            byte[] versionbuffer = Encoding.ASCII.GetBytes(version);
            bufferStream.WriteByte((byte)versionbuffer.Length);
            bufferStream.Write(versionbuffer, 0, versionbuffer.Length);

            byte[] buffer = BitConverter.GetBytes(packCount);
            bufferStream.Write(buffer, 0, buffer.Length);
            int tempCapacity = dataList.Count;

            buffer = BitConverter.GetBytes(tempCapacity);
            bufferStream.Write(buffer, 0, buffer.Length);
            MessagePackSerializer <List <T> > bf = MessagePackSerializer.Create <List <T> >();

            int      counter  = 0;
            int      maxCount = dataList.Count;
            List <T> tempList = new List <T>(tempCapacity);

            while (counter < maxCount)
            {
                tempList.Add(dataList[counter]);
                counter++;

                if (counter % packCount == 0)
                {
                    bf.Pack(bufferStream, tempList);
                    bufferStream.Flush();
                    tempList.Clear();
                }
            }

            if (tempList.Count > 0)
            {
                bf.Pack(bufferStream, tempList);
                bufferStream.Flush();
            }

            // bf.Pack(bufferStream, dataList);

            Console.WriteLine("100% serialized");
            bufferStream.Seek(0, SeekOrigin.Begin);
            CopyStream(bufferStream, zipStream);
            bufferStream.Close();
            zipStream.Close();
            fileStream.Close();
        }
コード例 #20
0
ファイル: Marshal.cs プロジェクト: votruongan/TATank
        public static byte[] Compress(byte[] src, int offset, int length)
        {
            MemoryStream memoryStream = new MemoryStream();
            Stream       stream       = new ZOutputStream(memoryStream, 9);

            stream.Write(src, offset, length);
            stream.Close();
            return(memoryStream.ToArray());
        }
コード例 #21
0
        private void StoreLayerDataBase64(List <int> tileIndices, XmlWriter xmlWriter, string dataCompression)
        {
            byte[] tileBytes = ConvertTileIndicesToBytes(tileIndices);

            if (dataCompression == "none")
            {
                string base64Data = Convert.ToBase64String(tileBytes);
                xmlWriter.WriteString(base64Data);
            }
            else if (dataCompression == "gzip")
            {
                MemoryStream inMemoryStream  = new MemoryStream(tileBytes);
                MemoryStream outMemoryStream = new MemoryStream();
                GZipStream   outGZipStream   = new GZipStream(outMemoryStream, CompressionMode.Compress);

                byte[] buffer = new byte[1024];
                while (true)
                {
                    int bytesRead = inMemoryStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    outGZipStream.Write(buffer, 0, bytesRead);
                }
                outGZipStream.Close();

                byte[] compressedBytes = outMemoryStream.ToArray();
                string base64Data      = Convert.ToBase64String(compressedBytes);
                xmlWriter.WriteString(base64Data);
            }
            else if (dataCompression == "zlib")
            {
                MemoryStream  inMemoryStream  = new MemoryStream(tileBytes);
                MemoryStream  outMemoryStream = new MemoryStream();
                ZOutputStream zOutputStream   = new ZOutputStream(outMemoryStream, 9);

                byte[] buffer = new byte[1024];
                while (true)
                {
                    int bytesRead = inMemoryStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    zOutputStream.Write(buffer, 0, bytesRead);
                }
                zOutputStream.Close();

                byte[] compressedBytes = outMemoryStream.ToArray();
                string base64Data      = Convert.ToBase64String(compressedBytes);
                xmlWriter.WriteString(base64Data);
            }
        }
コード例 #22
0
            public override void Close()
            {
                _out.Close();

                // TODO Parent context(s) should really be be closed explicitly

                _eiGen.Close();
                _cGen.Close();
                _sGen.Close();
                base.Close();
            }
コード例 #23
0
ファイル: Algo.cs プロジェクト: marcussacana/CatSceneEditor
        internal static void CompressData(Stream inData, Stream outData, int CompressLevel = 9)
        {
            MemoryStream  tmp        = new MemoryStream();
            ZOutputStream outZStream = new ZOutputStream(tmp, CompressLevel);

            CopyStream(inData, outZStream);
            outZStream.Finish();
            tmp.Position = 0;
            CopyStream(tmp, outData);
            outZStream.Close();
            tmp.Close();
        }
コード例 #24
0
        private byte[] Compress(byte[] data)
        {
            MemoryStream  compressed_out      = new MemoryStream();
            ZOutputStream z_compressor_stream = new ZOutputStream(compressed_out, zlibConst.Z_DEFAULT_COMPRESSION);

            z_compressor_stream.Write(data, 0, data.Length);
            z_compressor_stream.Close();
            byte[] result = compressed_out.ToArray();

            compressed_out.Close();

            return(result);
        }
コード例 #25
0
        private byte[] Decompress(byte[] data)
        {
            MemoryStream  decompressed_out      = new MemoryStream();
            ZOutputStream z_decompressor_stream = new ZOutputStream(decompressed_out);

            z_decompressor_stream.Write(data, 0, data.Length);
            z_decompressor_stream.Close();
            byte[] result = decompressed_out.ToArray();

            decompressed_out.Close();

            return(result);
        }
コード例 #26
0
        public static byte[] decompress(byte[] data, UInt32 realsize)
        {
            MemoryStream  o = new MemoryStream();
            ZOutputStream z = new ZOutputStream(o);
            MemoryStream  i = new MemoryStream(data);

            CopyStream(i, z);
            byte[] res = o.ToArray();
            i.Close();
            z.Close();
            o.Close();
            return(res);
        }
コード例 #27
0
        public byte[] Encompress(byte[] data)
        {
            MemoryStream outputStream = new MemoryStream();
            //压缩
            ZOutputStream zStream = new ZOutputStream(outputStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            zStream.Write(data, 0, data.Length);
            zStream.finish();
            byte[] result = outputStream.ToArray();
            zStream.Close();
            outputStream.Close();
            outputStream.Dispose();
            return(result);
        }
コード例 #28
0
        public byte[] Decompress(byte[] data)
        {
            MemoryStream outStream = new MemoryStream();
            //解压
            ZOutputStream zStream = new ZOutputStream(outStream);

            zStream.Write(data, 0, data.Length);
            outStream.Flush();
            byte[] result = outStream.ToArray();
            zStream.Close();
            outStream.Close();
            outStream.Dispose();
            return(result);
        }
コード例 #29
0
        /** 字节压缩buffer池 */
        //private static ObjectPoolSafe<BytesCompressData> _compressBufferPool = new ObjectPoolSafe<BytesCompressData>(()=> { return new BytesCompressData(); });

        /// <summary>
        /// 字节压缩(暂时未优化性能)
        /// </summary>
        public static byte[] compressByteArr(byte[] bytes, int off, int len)
        {
            MemoryStream  stream = new MemoryStream();
            ZOutputStream zs     = new ZOutputStream(stream, zlibConst.Z_BEST_COMPRESSION);

            zs.Write(bytes, off, len);
            zs.finish();

            byte[] re = stream.ToArray();

            zs.Close();
            stream.Close();

            return(re);
        }
コード例 #30
0
        /// <summary>
        /// 字节解压缩(暂时未优化性能)
        /// </summary>
        public static byte[] uncompressByteArr(byte[] bytes, int off, int len)
        {
            MemoryStream  stream = new MemoryStream();
            ZOutputStream zs     = new ZOutputStream(stream);

            zs.Write(bytes, off, len);
            zs.finish();

            byte[] re = stream.ToArray();

            zs.Close();
            stream.Close();

            return(re);
        }