суббота, 2 января 2016 г.

ArrayOfString to Byte Array

http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte

http://stackoverflow.com/questions/14669820/how-to-convert-a-string-array-to-a-byte-array-java


Compress/Decompress

http://www.dotnetperls.com/compress

http://www.dotnetperls.com/decompress


 private byte[] Serialize(IEnumerable<Dto.Bar> bs)
        {
            BinaryFormatter bf = new BinaryFormatter();
            byte[] bytes;
            MemoryStream ms = new MemoryStream();

            var lst = bs.Select(b => b.ToStr()).ToList();

            bf.Serialize(ms, lst);
            ms.Seek(0, 0);
            
            var bts = ms.ToArray();

            return bts;
        }

 private IEnumerable<IBarSimple> DeSerialize(byte[] bytes)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ms.Write(bytes, 0, bytes.Length);
            ms.Seek(0, 0);

            var ss = (List<string>)bf.Deserialize(ms);
            var bs = ss.Select(s => s.ToBarDto()).ToList();
            return bs;

        }

 private byte[] Compress(byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    gzip.Write(bytes, 0, bytes.Length);
                }
                return ms.ToArray();
            }

        }

 private byte[] DeCompress(byte[] bytes)
        {
            using (GZipStream gzstream = new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress))
            {
                const int size = 4096;
                byte[] buffer = new byte[size];
                using (MemoryStream mstr = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        count = gzstream.Read(buffer, 0, size);
                        if (count > 0)
                        {
                            mstr.Write(buffer, 0, count);
                        }
                    } while (count > 0);
                    return mstr.ToArray();
                }
            }

        }

Комментариев нет:

Отправить комментарий