// netz.compress.ICompress implementation

		public long Compress(string file, string zipFile)
		{
			long length = -1;
			FileStream ifs = null;
			FileStream ofs = null;
			try
			{
				ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
				ofs = File.Open(zipFile, FileMode.Create, FileAccess.Write, FileShare.None);
				DeflateStream dos = new DeflateStream(ofs, CompressionMode.Compress, true);
				byte[] buff = new byte[ifs.Length];
				while(true)
				{
					int r = ifs.Read(buff, 0, buff.Length);
					if(r <= 0) break;
					dos.Write(buff, 0, r);
				}
				dos.Flush();
				dos.Close();
				length = ofs.Length;
			}
			finally
			{
				if(ifs != null) ifs.Close();
				if(ofs != null) ofs.Close();
			}
			return length;
		}
    public static byte[] Deflate(this byte[] data)
    {
        using (MemoryStream output = new MemoryStream())
        {
            using (DeflateStream gzip = new DeflateStream(output, CompressionMode.Compress))
            {
                gzip.Write(data, 0, data.Length);
            }

            return output.ToArray();
        }
    }
Ejemplo n.º 3
0
 static void CompressProfile(string srcfile, string dstfile)
 {
     try
     {
         Console.WriteLine("Reading source...");
         byte[] x = File.ReadAllBytes(srcfile);
         int usize = x.Length;
         Console.WriteLine("Initializing memory stream...");
         MemoryStream ms = new MemoryStream();
         // write uncompressed size as big endian
         ms.WriteByte((byte)((usize>>24) & 0xFF));
         ms.WriteByte((byte)((usize>>16) & 0xFF));
         ms.WriteByte((byte)((usize>>8) & 0xFF));
         ms.WriteByte((byte)(usize & 0xFF));
         // then, compressed data
         // these two bytes are part of zlib standard, but aren't supported by DeflateStream
         ms.WriteByte(0x78);
         ms.WriteByte(0x9C);
         Console.WriteLine("Compressing data...");
         MemoryStream compData = new MemoryStream();
         DeflateStream ds = new DeflateStream(compData, CompressionMode.Compress);
         ds.Write(x, 0, x.Length);
         ds.Close();
         ms.Write(compData.ToArray(), 0, compData.ToArray().Length);
         // Adler32 checksum as big endian - also not supported by DeflateStream, but required by zlib standard
         int checksum = GetAdler32(x);
         ms.WriteByte((byte)((checksum>>24) & 0xFF));
         ms.WriteByte((byte)((checksum>>16) & 0xFF));
         ms.WriteByte((byte)((checksum>>8) & 0xFF));
         ms.WriteByte((byte)(checksum & 0xFF));
         // start filestream
         Console.WriteLine("Creating file stream...");
         FileStream fs = File.Create(dstfile);
         // write hash
         fs.Write(new SHA1CryptoServiceProvider().ComputeHash(ms.ToArray()), 0, 0x14);
         // write usize + compressed data
         fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
         Console.WriteLine("Compression done.\n" + dstfile);
         fs.Close();
         ms.Close();
         compData.Close();
     }
     catch(Exception ex)
     {
         Console.WriteLine(ex.GetType().Name + " | " + ex.Message);
         Console.Beep();
     }
     return;
 }
Ejemplo n.º 4
0
    public static byte[] Compress(byte[] data)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            Stream s = new DeflateStream(ms, CompressionMode.Compress);

            s.Write(data, 0, data.Length);
            s.Close();

            return ms.ToArray();
        }
        catch
        {
            return null;
        }
    }
Ejemplo n.º 5
0
        // TODO (Dragon): see if theres a good way to minimize memory consumption
        private static void CompressFirstGenCEXbox(string file)
        {
            using (MemoryStream msOutput = new MemoryStream())
            {
                using (FileStream fsInput = new FileStream(file, FileMode.Open))
                {
                    using (BinaryReader brInput = new BinaryReader(fsInput))
                    {
                        //header is uncompressed
                        msOutput.Write(brInput.ReadBytes(0x800), 0, 0x800);

                        int    realsize  = (int)fsInput.Length - 0x800;
                        byte[] chunkData = new byte[realsize];

                        fsInput.Seek(0x800, SeekOrigin.Begin);
                        fsInput.Read(chunkData, 0, realsize);

                        msOutput.WriteByte((byte)0x78);
                        msOutput.WriteByte((byte)0x9C);

                        using (DeflateStream ds = new DeflateStream(msOutput, CompressionMode.Compress, true))
                        {
                            realsize = fsInput.Read(chunkData, 0, chunkData.Length);
                            ds.Write(chunkData, 0, chunkData.Length);
                        }

                        // NOTE: actual zlib has an adler-32 checksum trailer on the end
                        uint adler = Adler32.Calculate(chunkData);

                        // write the bytes
                        msOutput.WriteByte((byte)((adler & 0xFF000000) >> 24));
                        msOutput.WriteByte((byte)((adler & 0xFF0000) >> 16));
                        msOutput.WriteByte((byte)((adler & 0xFF00) >> 8));
                        msOutput.WriteByte((byte)(adler & 0xFF));

                        // CE xbox has some padding on the end to a 0x800 alignment
                        int    pad_size = 0x800 - ((int)msOutput.Length % 0x800);
                        byte[] padding  = new byte[pad_size];
                        msOutput.Write(padding, 0, pad_size);
                    }
                }
                File.WriteAllBytes(file, msOutput.ToArray());
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Compresses a stream into a .gz stream.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Span <byte> Compress(Span <byte> data, CompressionOptions?options)
        {
            options ??= CompressionOptions.Default;
            var buffer = new Span <byte>(new byte[data.Length]);
            var cursor = 0;

            for (var i = 0; i < data.Length; i += options.BlockSize)
            {
                using var ms            = new MemoryStream(options.BlockSize);
                using var deflateStream = new DeflateStream(ms, CompressionLevel.Optimal);

                var block = data.Slice(i, Math.Min(options.BlockSize, data.Length - i));
                deflateStream.Write(block);
                deflateStream.Flush();
                var write      = block.Length;
                var compressed = false;
                if (0x100 < block.Length) // special case where the last block is too small to compress properly.
                {
                    write       = (int)ms.Position + 2;
                    block       = new Span <byte>(new byte[ms.Length]);
                    ms.Position = 0;
                    ms.Read(block);
                    compressed = true;
                }

                var absWrite = write;
                if (compressed)
                {
                    absWrite = write + 0x4000;
                }

                MemoryMarshal.Write(buffer.Slice(cursor), ref absWrite);
                if (compressed)
                {
                    buffer[cursor + 4] = 0x78;
                    buffer[cursor + 5] = 0xDA;
                }

                block.CopyTo(buffer.Slice(cursor + 4 + (compressed ? 2 : 0)));
                cursor = (cursor + write + 4 + (compressed ? 2 : 0)).Align(0x80);
            }

            return(buffer.Slice(0, cursor));
        }
Ejemplo n.º 7
0
        public static byte[] Compress(byte[] data)
        {
            if (data.Length == 0)
            {
                return(data);
            }
            using (MemoryStream input = new MemoryStream(data.Length + 1))
            {
                input.WriteByte((byte)Compressed.NO);
                input.Write(data, 0, data.Length);
                input.Position = 1;
                using (MemoryStream output = new MemoryStream())
                {
                    output.WriteByte((byte)Compressed.YES);

                    DeflateStream compressionStream;
                    using (compressionStream = new DeflateStream(output, CompressionMode.Compress, true))
                    {
                        int    read   = 0;
                        byte[] buffer = new byte[4096];
                        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            compressionStream.Write(buffer, 0, read);
                        }
                    }

                    try
                    {
                        if (output.Length > input.Length)
                        {
                            return(input.ToArray());
                        }
                        else
                        {
                            return(output.ToArray());
                        }
                    }
                    finally
                    {
                        compressionStream.Close();
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private void Compress()
        {
            using (var compressedStream = new MemoryStream())
            {
                compressedStream.Write(_buffer, 0, 8);

                //ZLIB Header
                compressedStream.Write(_header, 0, _header.Length);

                using (var compresser = new DeflateStream(compressedStream, CompressionMode.Compress, true))
                    compresser.Write(_buffer, 8, _buffer.Length - 8);

                //ZLIB Trailer
                compressedStream.Write(_trailer, 0, _trailer.Length);

                _buffer    = compressedStream.ToArray();
                _buffer[0] = (byte)'C';
            }
        }
Ejemplo n.º 9
0
    public static string[] divide(byte[] data)
    {
        int length = data.Length / (10000);
        var list   = new List <string>();

        foreach (var chunk in data.Chunks(10000))
        {
            var bytes = chunk.ToArray();

            MemoryStream  ms = new MemoryStream();
            DeflateStream CompressedStream = new DeflateStream(ms, CompressionMode.Compress, true);
            CompressedStream.Write(bytes, 0, bytes.Length);
            CompressedStream.Close();
            var str = Convert.ToBase64String(ms.ToArray());
            ms.Close();
            list.Add(str);
        }
        return(list.ToArray());
    }
Ejemplo n.º 10
0
        public static DatFile FromFile(string diskPath, string datPath, bool compress)
        {
            var file = new DatFile();
            var name = Path.GetFileName(diskPath);

            file.diskPath   = diskPath;
            file.datPath    = datPath + "\\" + name;
            file.compressed = compress;

            using (FileStream originalFileStream = File.OpenRead(diskPath))
            {
                var mem = new MemoryStream();
                originalFileStream.CopyTo(mem);
                file.bytes = mem.ToArray();
                file.size  = (int)mem.Length;
                if (compress)
                {
                    MemoryStream output = new MemoryStream();
                    using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
                    {
                        dstream.Write(file.bytes, 0, file.size);
                    }
                    var adler = new Adler32Computer();
                    var arr   = new List <byte>(output.ToArray());
                    // Calculate adler checksum and put it at the end in Big-Endian.
                    var check = BitConverter.GetBytes(adler.Checksum(arr.ToArray(), 0, (int)arr.Count)).Reverse().ToArray();
                    arr.AddRange(check);
                    // Add zlib headers
                    // 78 DA - Best Compression
                    arr.Insert(0, (byte)0xDA);
                    arr.Insert(0, (byte)0x78);

                    file.packedBytes = arr.ToArray();
                    file.packedSize  = file.packedBytes.Length;
                }
                else
                {
                    file.packedSize = file.size;
                }
            }

            return(file);
        }
Ejemplo n.º 11
0
    /// GZipStream byte[] 압축한것
    //IEnumerator GetCameraTexture()
    //{
    //    yield return new WaitForEndOfFrame();

    //    while (true)
    //    {
    //        texture2D = ToTexture2D(camera.targetTexture);
    //        targetMaterial.mainTexture = texture2D;

    //        var datas = texture2D.GetRawTextureData();

    //        MemoryStream ms = new MemoryStream();
    //        GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
    //        zip.Write(datas, 0, datas.Length);
    //        zip.Close();
    //        ms.Position = 0;

    //        // Byte[] 압축
    //        MemoryStream output = new MemoryStream();
    //        byte[] compressed = new byte[ms.Length];
    //        ms.Read(compressed, 0, compressed.Length);

    //        byte[] gzBuffer = new byte[compressed.Length + 4];
    //        Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
    //        Buffer.BlockCopy(BitConverter.GetBytes(datas.Length), 0, gzBuffer, 0, 4);

    //        Debug.Log($"data size = {datas.Length},  compress size = {gzBuffer.Length}");

    //        var compData = gzBuffer;
    //        var numOfDatas = compData.Length / maxPacketSize;
    //        var remainOfDatas = compData.Length % maxPacketSize;

    //        if (compData.Length > maxPacketSize)
    //        {
    //            for (int idx = 0; idx <= numOfDatas; idx++)
    //            {
    //                List<byte> newData = new List<byte>();
    //                for (int i = (idx * maxPacketSize); i < ((idx != numOfDatas) ? ((idx + 1) * maxPacketSize) : (idx * maxPacketSize) + remainOfDatas); i++)
    //                {
    //                    newData.Add(compData[i]);
    //                }
    //                SendCameraTexture(numOfDatas, idx, newData.ToArray());
    //            }
    //        }
    //        else
    //        {
    //            SendCameraTexture(numOfDatas, 0, compData);
    //        }

    //        yield return new WaitForSeconds(1f / fps);
    //    }
    //}

    /// DeflateStream으로 byte[] 압축한것
    IEnumerator GetCameraTexture()
    {
        yield return(new WaitForEndOfFrame());

        while (true)
        {
            texture2D = ToTexture2D(camera.targetTexture);
            targetMaterial.mainTexture = texture2D;

            var datas = texture2D.GetRawTextureData();

            // Byte[] 압축
            MemoryStream output = new MemoryStream();
            using (DeflateStream dstream = new DeflateStream(output, System.IO.Compression.CompressionLevel.Optimal))
            {
                dstream.Write(datas, 0, datas.Length);
            }
            Debug.Log($"data size = {datas.Length},  compress size = {output.ToArray().Length}");

            var compData      = output.ToArray();
            var numOfDatas    = compData.Length / maxPacketSize;
            var remainOfDatas = compData.Length % maxPacketSize;

            if (compData.Length > maxPacketSize)
            {
                for (int idx = 0; idx <= numOfDatas; idx++)
                {
                    List <byte> newData = new List <byte>();
                    for (int i = (idx * maxPacketSize); i < ((idx != numOfDatas) ? ((idx + 1) * maxPacketSize) : (idx * maxPacketSize) + remainOfDatas); i++)
                    {
                        newData.Add(compData[i]);
                    }
                    SendCameraTexture(numOfDatas, idx, newData.ToArray());
                }
            }
            else
            {
                SendCameraTexture(numOfDatas, 0, compData);
            }

            yield return(new WaitForSeconds(1f / fps));
        }
    }
Ejemplo n.º 12
0
 //Fonction qui zip array de bytes et retourne un array de bytes
 public static byte[] Zip(byte[] b)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             using (DeflateStream gz = new DeflateStream(ms, CompressionLevel.Optimal))
             {
                 gz.Write(b, 0, b.Length);
             }
             return(ms.ToArray());
         }
     }
     catch (Exception ex)
     {
         Logger.Error(LOGNAME, ex.Message, ex);
     }
     return(null);
 }
Ejemplo n.º 13
0
            private static byte[] Compress(byte[] data)
            {
                if (data == null)
                {
                    return(null);
                }

                try {
                    using (var memory = new MemoryStream()) {
                        using (var deflate = new DeflateStream(memory, CompressionMode.Compress)) {
                            deflate.Write(data, 0, data.Length);
                        }
                        return(memory.ToArray());
                    }
                } catch (Exception e) {
                    Logging.Error(e);
                    return(null);
                }
            }
Ejemplo n.º 14
0
    public static SqlBytes fnCompress(SqlBytes blob)
    {
        if (blob.IsNull)
        {
            return(blob);
        }

        byte[] blobData = blob.Buffer;

        MemoryStream  compressData = new MemoryStream();
        DeflateStream compressor   = new DeflateStream(compressData, CompressionMode.Compress, true);

        compressor.Write(blobData, 0, blobData.Length);
        compressor.Flush();
        compressor.Close();
        compressor = null;

        return(new SqlBytes(compressData));
    }
Ejemplo n.º 15
0
        /// <summary>
        /// 压缩流
        /// </summary>
        /// <param name="sourcestream"></param>
        /// <returns></returns>
        public MemoryStream SerStreamZip(Stream sourcestream)
        {
            MemoryStream  zpistream        = new MemoryStream();
            DeflateStream compressedstream = new DeflateStream(zpistream, CompressionMode.Compress);//

            try
            {
                byte[] buffer = new byte[sourcestream.Length];
                sourcestream.Position = 0;
                sourcestream.Read(buffer, 0, buffer.Length);
                compressedstream.Write(buffer, 0, buffer.Length);
                compressedstream.Close();
                return(zpistream);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 16
0
        public static byte[] DeflateBlock(byte[] inBlock, CompressionType type, int offset = 0)
        {
            if (offset < 0)
            {
                offset = 0;
            }
            byte[] outBlock;

            switch (type)
            {
            case CompressionType.GZIP:
                using (MemoryStream ms = new MemoryStream())
                {
                    // Compresses gzip stream
                    GZipStream gzip = new GZipStream(new MemoryStream(inBlock.Skip(offset).ToArray()), CompressionMode.Compress);

                    gzip.CopyTo(ms);
                    outBlock = ms.ToArray();
                    gzip.Flush();
                }
                break;

            case CompressionType.ZLIB:
                using (MemoryStream ms = new MemoryStream())
                {
                    // Compresses zlib stream
                    using (var outZStream = new DeflateStream(ms, CompressionLevel.Optimal, true))
                    {
                        outZStream.Write(inBlock, offset, inBlock.Length - offset);
                    }

                    outBlock = ms.ToArray();
                }
                return(outBlock);    // Returns without magic

            default:
                outBlock = new byte[inBlock.Length];
                Array.Copy(inBlock, outBlock, inBlock.Length);
                break;
            }

            return(outBlock);
        }
Ejemplo n.º 17
0
        public static byte[] DeflateByte(byte[] str)
        {
            if (str == null)
            {
                return(null);
            }

            using (var output = new MemoryStream())
            {
                using (var compressor = new DeflateStream(
                           output, CompressionMode.Compress,
                           CompressionLevel.BestSpeed))
                {
                    compressor.Write(str, 0, str.Length);
                }

                return(output.ToArray());
            }
        }
Ejemplo n.º 18
0
        public byte[] DeflateCompress(byte[] str)
        {
            var ms = new MemoryStream(str)
            {
                Position = 0
            };
            var outms = new MemoryStream();

            using (var deflateStream = new DeflateStream(outms, CompressionMode.Compress, true))
            {
                var buf = new byte[1024];
                int len;
                while ((len = ms.Read(buf, 0, buf.Length)) > 0)
                {
                    deflateStream.Write(buf, 0, len);
                }
            }
            return(outms.ToArray());
        }
Ejemplo n.º 19
0
        public static void CompressFile(string sourceFile, string destFile)
        {
            FileStream inFs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);

            byte[] inBuffer = new byte[inFs.Length];
            inFs.Read(inBuffer, 0, inBuffer.Length);


            FileStream    outFs          = new FileStream(destFile, FileMode.Create);
            DeflateStream compressStream = new DeflateStream(outFs, CompressionMode.Compress);


            compressStream.Write(inBuffer, 0, inBuffer.Length);

            Console.Write(destFile + " Compress " + outFs.Length);

            compressStream.Close();
            outFs.Close();
        }
Ejemplo n.º 20
0
 protected byte[] GetWriteBuffer(SocketMessager messager)
 {
     using (MemoryStream ms = new MemoryStream()) {
         byte[] buff = Encoding.UTF8.GetBytes(messager.GetCanParseString());
         ms.Write(buff, 0, buff.Length);
         if (messager.Arg != null)
         {
             var data = BaseSocket.Serialize(messager.Arg);
             using (MemoryStream msBuf = new MemoryStream()) {
                 using (DeflateStream ds = new DeflateStream(msBuf, CompressionMode.Compress)) {
                     ds.Write(data, 0, data.Length);
                     buff = msBuf.ToArray();
                     ms.Write(buff, 0, buff.Length);
                 }
             }
         }
         return(this.GetWriteBuffer(ms.ToArray()));
     }
 }
Ejemplo n.º 21
0
        /// <summary>
        /// 将图片转换成压缩过的字节数组
        /// </summary>
        /// <param name="image">图片</param>
        /// <returns>字节数组</returns>
        public static byte[] GetZippedBytesFromImage(Image image)
        {
            if (image == null)
            {
                return(null);
            }

            byte[] bytNew = null;

            using (MemoryStream ms = new MemoryStream())
            {
                using (DeflateStream zipStream = new DeflateStream(ms, CompressionMode.Compress))
                {
                    byte[] bytImage = GetImageBytes(image);

                    zipStream.Write(bytImage, 0, bytImage.Length);
                    zipStream.Flush();
                }

                // 取出压缩后的结果
                bytNew = ms.ToArray();

                // 如果用 GetBuffer(),则后面会有好多 0;而使用 ToArray() 则没有这种情况
                //byte[] byt2 = ms.ToArray();
                //// 压缩后的结果在后段差不多有一半为 0,将其截断,只留一个作为结尾的标志
                //int iCount = 0;
                //for( int i = byt.Length - 1; i >= 0; i-- )
                //{
                //    if( iCount == 0 && i > 0 && ( (int)byt[i - 1] ) != 0 )
                //    {
                //        iCount = i;
                //        bytNew = new byte[iCount + 1];
                //    }

                //    if( iCount > 0 )
                //    {
                //        bytNew[i] = byt[i];
                //    }
                //}
            }

            return(bytNew);
        }
Ejemplo n.º 22
0
        /*
         * static byte[] Compress(byte[] data)
         * {
         *  using (var compressStream = new MemoryStream())
         *  using (var compressor = new DeflateStream(compressStream, CompressionLevel.Fastest))
         *  {
         *      compressStream.Write(FakeDeflateHeader, 0, FakeDeflateHeader.Length);
         *      compressor.Write(data, 0, data.Length);
         *      compressor.Close();
         *      return compressStream.ToArray();
         *  }
         * }
         */

        static byte[] Compress(byte[] data, int height, int stride)
        {
            using var compressStream = new MemoryStream();
            compressStream.Write(FakeDeflateHeader, 0, FakeDeflateHeader.Length);

            using (var compressor = new DeflateStream(compressStream, CompressionLevel.Fastest))
            {
                for (int v = 0, offset = 0; v < height; v++, offset += stride)
                {
                    compressor.WriteByte(0);
                    compressor.Write(data, offset, stride);
                }

                compressor.Flush();
                compressor.Close();
            }

            return(compressStream.ToArray());
        }
Ejemplo n.º 23
0
        /// <summary>
        /// 数据集格式转为二进制,并压缩
        /// </summary>
        /// <param name="dsOriginal"></param>
        /// <returns></returns>
        static public byte[] CompressionDataSet(DataSet dsOriginal)
        {
            // 序列化为二进制
            dsOriginal.RemotingFormat = SerializationFormat.Xml;
            BinaryFormatter bFormatter = new BinaryFormatter();
            MemoryStream    mStream    = new MemoryStream();

            bFormatter.Serialize(mStream, dsOriginal);
            byte[] bytes = mStream.ToArray();
            // 压缩
            MemoryStream  oStream   = new MemoryStream();
            DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);

            zipStream.Write(bytes, 0, bytes.Length);
            zipStream.Flush();
            zipStream.Close();
            //
            return(oStream.ToArray());
        }
Ejemplo n.º 24
0
        public ArraySegment <byte> Compress(ReadOnlyMemory <byte> inputData)
        {
            Guard.AgainstEmpty(inputData, nameof(inputData));

            using var compressedStream = new MemoryStream();
            using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel, false))
            {
                deflateStream.Write(inputData.Span);
            }

            if (compressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                return(compressedStream.ToArray());
            }
        }
Ejemplo n.º 25
0
        private static byte[] Compress(byte[] data, int dataLength, SaveOptions options)
        {
            const int headerLength   = 2;
            const int checksumLength = 4;

            var compressionLevel = options?.AttemptCompression == true ? CompressionLevel.Optimal : CompressionLevel.Fastest;

            using (var compressStream = new MemoryStream())
                using (var compressor = new DeflateStream(compressStream, compressionLevel, true))
                {
                    compressor.Write(data, 0, dataLength);
                    compressor.Close();

                    compressStream.Seek(0, SeekOrigin.Begin);

                    var result = new byte[headerLength + compressStream.Length + checksumLength];

                    // Write the ZLib header.
                    result[0] = Deflate32KbWindow;
                    result[1] = ChecksumBits;

                    // Write the compressed data.
                    int streamValue;
                    var i = 0;
                    while ((streamValue = compressStream.ReadByte()) != -1)
                    {
                        result[headerLength + i] = (byte)streamValue;
                        i++;
                    }

                    // Write Checksum of raw data.
                    var checksum = Adler32Checksum.Calculate(data, dataLength);

                    var offset = headerLength + compressStream.Length;

                    result[offset++] = (byte)(checksum >> 24);
                    result[offset++] = (byte)(checksum >> 16);
                    result[offset++] = (byte)(checksum >> 8);
                    result[offset]   = (byte)(checksum >> 0);

                    return(result);
                }
        }
Ejemplo n.º 26
0
 public static string CompressString(string str)
 {
     using (var msi = new MemoryStream(Encoding.UTF8.GetBytes(str)))
     {
         using (var mso = new MemoryStream())
         {
             using (var gs = new DeflateStream(mso, CompressionMode.Compress))
             {
                 byte[] buffer = new byte[4096];
                 int    cnt;
                 while ((cnt = msi.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     gs.Write(buffer, 0, cnt);
                 }
             }
             return(Convert.ToBase64String(mso.ToArray()));
         }
     }
 }
Ejemplo n.º 27
0
        public ChromelyResponse RunGzip(ChromelyRequest req)
        {
            var body   = req.PostData.ToString().ToJson <Dictionary <string, object> >();
            var source = body["source"].ToString();
            var dest   = body["dest"].ToString();

            if (File.Exists(source))
            {
                var text  = File.ReadAllText(source);
                var bytes = Encoding.Unicode.GetBytes(text);
                using (var fs = new FileStream(dest, FileMode.Create, FileAccess.Write))
                    using (var df = new DeflateStream(fs, CompressionLevel.Fastest))
                    {
                        df.Write(bytes, 0, bytes.Length);
                    }
                return(Response.OK);
            }
            return(Response.BadRequest);
        }
Ejemplo n.º 28
0
        byte[] PrepareGMData()
        {
            byte[]        data           = win1251.GetBytes(GlobalModule);
            DeflateStream ZlibCompressed = null;

            if (IsEncrypted)
            {
                data = Encryption.Encode(data, Encryption.Mode.GlobalModule);
            }
            MemoryStream tempstream = new MemoryStream();

            ZlibCompressed = new DeflateStream(tempstream, CompressionLevel.Optimal, true);
            ZlibCompressed.Write(data, 0, data.Length);
            ZlibCompressed.Close();

            data = tempstream.GetBuffer();
            Array.Resize <byte>(ref data, (int)tempstream.Length);
            return(data);
        }
Ejemplo n.º 29
0
        private void ZlibCompress(MemoryStream dst, MemoryStream src)
        {
            dst.Write(_zlibHeader, 0, _zlibHeader.Length);

            src.TryGetBuffer(out ArraySegment <byte> bufArray);
            _adler32.CalculateChecksum(bufArray.Array, bufArray.Offset, bufArray.Count);

            using (var deflateStream = new DeflateStream(dst, CompressionMode.Compress, true))
                deflateStream.Write(bufArray.Array, bufArray.Offset, bufArray.Count);

            var checksum = _adler32.Checksum;

            _checksumBuf[0] = (byte)(checksum >> 24);
            _checksumBuf[1] = (byte)(checksum >> 16);
            _checksumBuf[2] = (byte)(checksum >> 8);
            _checksumBuf[3] = (byte)(checksum >> 0);
            dst.Write(_checksumBuf, 0, _checksumBuf.Length);
            _adler32.Reset();
        }
Ejemplo n.º 30
0
 public static byte[] Compress(byte[] data)
 {
     byte[] compressArray = null;
     try
     {
         using (MemoryStream memoryStream = new MemoryStream())
         {
             using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionLevel.Optimal))
             {
                 deflateStream.Write(data, 0, data.Length);
             }
             compressArray = memoryStream.ToArray();
         }
     }
     catch (Exception)
     {
     }
     return(compressArray);
 }
Ejemplo n.º 31
0
 public virtual byte[] Compress(byte[] decompressedData)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         if (this.Algorithm == CompressionAlgorithm.Deflate)
         {
             GZipStream stream2 = new GZipStream(stream, CompressionMode.Compress, true);
             stream2.Write(decompressedData, 0, decompressedData.Length);
             stream2.Close();
         }
         else
         {
             DeflateStream stream3 = new DeflateStream(stream, CompressionMode.Compress, true);
             stream3.Write(decompressedData, 0, decompressedData.Length);
             stream3.Close();
         }
         return(stream.ToArray());
     }
 }
Ejemplo n.º 32
0
        /// <summary>
        /// Compresses the data array.
        /// </summary>
        /// <param name="data">The data to compress.</param>
        /// <returns>The compressed data.</returns>
        private static byte[] CompressData(byte[] data)
        {
            var adler = BitConverter.GetBytes(Adler32.Compute(data));

            using (var compressedStream = new MemoryStream())
            {
                compressedStream.WriteByte(0x58);
                compressedStream.WriteByte(0x85);
                using (var outputStream = new DeflateStream(compressedStream, CompressionMode.Compress, true))
                {
                    outputStream.Write(data, 0, data.Length);
                }
                compressedStream.WriteByte(adler[3]);
                compressedStream.WriteByte(adler[2]);
                compressedStream.WriteByte(adler[1]);
                compressedStream.WriteByte(adler[0]);
                return(compressedStream.ToArray());
            }
        }
Ejemplo n.º 33
0
        public static byte[] CompressionObject(object DataOriginal)
        {
            if (DataOriginal == null)
            {
                return(null);
            }
            BinaryFormatter bFormatter = new BinaryFormatter();
            MemoryStream    mStream    = new MemoryStream();

            bFormatter.Serialize(mStream, DataOriginal);
            byte[]        bytes     = mStream.ToArray();
            MemoryStream  oStream   = new MemoryStream();
            DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);

            zipStream.Write(bytes, 0, bytes.Length);
            zipStream.Flush();
            zipStream.Close();
            return(oStream.ToArray());
        }
Ejemplo n.º 34
0
        public static byte[] Compress(Memory <byte> input, bool writeLen = false, CompressionLevel compressionLevel = CompressionLevel.Fastest)
        {
            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                int checksum = 0;
                using (var compressStream = new DeflateStream(stream, compressionLevel, true))
                {
                    if (writeLen)
                    {
                        WriteLength(compressStream, input.Length);
                    }

                    compressStream.Write(input.Span);
                }

                byte[] bytes = stream.ToArray();
                return(bytes);
            }
        }
Ejemplo n.º 35
0
    public static byte[] Compress(byte[] data)
    {
        try
        {
            //Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

            MemoryStream ms = new MemoryStream();
            Stream s = new DeflateStream(ms, CompressionMode.Compress);

            s.Write(data, 0, data.Length);
            s.Close();

            //Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;

            return ms.ToArray();
        }
        catch
        {
            //Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;
            return null;
        }
    }
Ejemplo n.º 36
0
 public void Compress(CompressionType type)
 {
     byte[] bytes = CreateBytesToCompress(type);
     PerfUtils utils = new PerfUtils();
     foreach (var iteration in Benchmark.Iterations)
     {
         string filePath = utils.GetTestFilePath();
         using (FileStream output = File.Create(filePath))
         using (DeflateStream zip = new DeflateStream(output, CompressionMode.Compress))
         using (iteration.StartMeasurement())
         {
             zip.Write(bytes, 0, bytes.Length);
         }
         File.Delete(filePath);
     }
 }
Ejemplo n.º 37
0
	private static void writeMesh(Mesh m) {
		string filepath = folderName + "/" + m.name + ".mesh";
		if (File.Exists(filepath)) {
			File.Delete(filepath);
		}
		MemoryStream ms = new MemoryStream();
		BinaryWriter bw = new BinaryWriter(ms);
		Debug.Log("Write Mesh:" + m.name + " to disk:" + filepath);
		// write length
		bw.Write(m.name.Length);
		// write name
		bw.Write(m.name.ToCharArray());
		// write matrix
		float[] rawData = new float[]{
			1, 0, 0, 0, 
			0, 1, 0, 0, 
			0, 0, 1, 0
		};
		for (int i = 0; i < rawData.Length; i++) {
			bw.Write(rawData[i]);
		}
		// write sub mesh count
		bw.Write(1);
		// write vertex count
		bw.Write(m.vertices.Length);
		Debug.Log("\tVertices:" + m.vertices.Length);
		foreach (Vector3 vert in m.vertices) {
			bw.Write(vert.x);
			bw.Write(vert.y);
			bw.Write(vert.z);
		}
		// write uv0
		bw.Write(m.uv.Length);
		Debug.Log("\tUV0:" + m.uv.Length);
		foreach (Vector2 uv0 in m.uv) {
			bw.Write(uv0.x);
			bw.Write(1 - uv0.y);
		}
		// write uv1
		bw.Write(m.uv2.Length);
		Debug.Log("\tUV1:" + m.uv2.Length);
		foreach (Vector2 uv1 in m.uv2) {
			bw.Write(uv1.x);
			bw.Write(uv1.y);
		}
		Debug.Log("normals:" + m.normals.Length);
		// write normal
		if (normal) {
			bw.Write(m.normals.Length);
			foreach (Vector3 n in m.normals) {
				bw.Write(n.x);
				bw.Write(n.y);
				bw.Write(n.z);
			}
		} else {
			bw.Write(0);
		}
		Debug.Log("tangent:" + m.tangents.Length);
		// write tangent
		if (tangent) {
			bw.Write(m.tangents.Length);
			foreach (Vector3 t in m.tangents) {
				bw.Write(t.x);
				bw.Write(t.y);
				bw.Write(t.z);
			}
		} else {
			bw.Write(0);
		}
		// skeleton weights
		bw.Write(0);
		// skeleton indices
		bw.Write(0);
		// write indices
		bw.Write(m.triangles.Length);
		for (int i = 0; i < m.triangles.Length; i++) {
			bw.Write(m.triangles[i]);
		}
		// bounds
		bw.Write(m.bounds.min.x);
		bw.Write(m.bounds.min.y);
		bw.Write(m.bounds.min.z);
		bw.Write(m.bounds.max.x);
		bw.Write(m.bounds.max.y);
		bw.Write(m.bounds.max.z);
		bw.Close();
		
		int size = ms.GetBuffer().Length;
		MemoryStream compressionBytes = new MemoryStream();
		// write to disk
		DeflateStream compressionStream = new DeflateStream(compressionBytes, CompressionMode.Compress);
		compressionStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
		compressionStream.Close();
		
		FileStream fs = new FileStream(filepath, FileMode.Create);
		BinaryWriter cbw = new BinaryWriter(fs);
		// write compression type
		cbw.Write(3);
		// write original size
		cbw.Write(size);
		// write compressed data
		cbw.Write(compressionBytes.GetBuffer());

		cbw.Close();
		fs.Close();
	}
Ejemplo n.º 38
0
    public const ResponseDataType DefaultDataType = ResponseDataType.PlainText; //PlainText ZipBinary ZipBase64

    #endregion Fields

    #region Methods

    /// <summary>
    /// 将报表XML数据文本输出到HTTP请求
    /// </summary>
    /// <param name="DataPage"></param>
    /// <param name="DataText"></param>
    /// <param name="DataType"></param>
    public static void ResponseData(System.Web.UI.Page DataPage, ref string DataText, ResponseDataType DataType)
    {
        //报表XML数据的前后不能附加任何其它数据,否则XML数据将不能成功解析,所以调用ClearContent方法清理网页中前面多余的数据
            DataPage.Response.ClearContent();

            if (ResponseDataType.PlainText == DataType)
            {
                // 把xml对象发送给客户端
                //DataPage.Response.ContentType = "text/xml";
                DataPage.Response.Write(DataText);
            }
            else
            {
                //将string数据转换为byte[],以便进行压缩
                System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding();
                byte[] XmlBytes = converter.GetBytes(DataText);

                //在 HTTP 头信息中写入报表数据压缩信息
                DataPage.Response.AppendHeader("gr_zip_type", "deflate");                  //指定压缩方法
                DataPage.Response.AppendHeader("gr_zip_size", XmlBytes.Length.ToString()); //指定数据的原始长度
                DataPage.Response.AppendHeader("gr_zip_encode", converter.HeaderName);     //指定数据的编码方式 utf-8 utf-16 ...

                // 把压缩后的xml数据发送给客户端
                if (ResponseDataType.ZipBinary == DataType)
                {
                    System.IO.Compression.DeflateStream compressedzipStream = new DeflateStream(DataPage.Response.OutputStream, CompressionMode.Compress, true);
                    compressedzipStream.Write(XmlBytes, 0, XmlBytes.Length);
                    compressedzipStream.Close();
                }
                else //ResponseDataType.ZipBase64
                {
                    MemoryStream memStream = new MemoryStream();
                    DeflateStream compressedzipStream = new DeflateStream(memStream, CompressionMode.Compress, true);
                    compressedzipStream.Write(XmlBytes, 0, XmlBytes.Length);
                    compressedzipStream.Close(); //这句很重要,这样数据才能全部写入 MemoryStream

                    // Read bytes from the stream.
                    memStream.Seek(0, SeekOrigin.Begin); // Set the position to the beginning of the stream.
                    int count = (int)memStream.Length;
                    byte[] byteArray = new byte[count];
                    count = memStream.Read(byteArray, 0, count);

                    string Base64Text = Convert.ToBase64String(byteArray);
                    DataPage.Response.Write(Base64Text);
                }
            }

            //报表XML数据的前后不能附加任何其它数据,否则XML数据将不能成功解析,所以调用End方法放弃网页中后面不必要的数据
            DataPage.Response.End();
    }
Ejemplo n.º 39
0
        public void ReadWriteArgumentValidation()
        {
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws<ArgumentNullException>(() => ds.Write(null, 0, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Write(new byte[1], -1, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Write(new byte[1], 0, -1));
                Assert.Throws<ArgumentException>(() => ds.Write(new byte[1], 0, 2));
                Assert.Throws<ArgumentException>(() => ds.Write(new byte[1], 1, 1));
                Assert.Throws<InvalidOperationException>(() => ds.Read(new byte[1], 0, 1));
                ds.Write(new byte[1], 0, 0);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws<ArgumentNullException>(() => { ds.WriteAsync(null, 0, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], -1, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], 0, -1); });
                Assert.Throws<ArgumentException>(() => { ds.WriteAsync(new byte[1], 0, 2); });
                Assert.Throws<ArgumentException>(() => { ds.WriteAsync(new byte[1], 1, 1); });
                Assert.Throws<InvalidOperationException>(() => { ds.Read(new byte[1], 0, 1); });
            }

            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws<ArgumentNullException>(() => ds.Read(null, 0, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Read(new byte[1], -1, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Read(new byte[1], 0, -1));
                Assert.Throws<ArgumentException>(() => ds.Read(new byte[1], 0, 2));
                Assert.Throws<ArgumentException>(() => ds.Read(new byte[1], 1, 1));
                Assert.Throws<InvalidOperationException>(() => ds.Write(new byte[1], 0, 1));

                var data = new byte[1] { 42 };
                Assert.Equal(0, ds.Read(data, 0, 0));
                Assert.Equal(42, data[0]);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws<ArgumentNullException>(() => { ds.ReadAsync(null, 0, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], -1, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], 0, -1); });
                Assert.Throws<ArgumentException>(() => { ds.ReadAsync(new byte[1], 0, 2); });
                Assert.Throws<ArgumentException>(() => { ds.ReadAsync(new byte[1], 1, 1); });
                Assert.Throws<InvalidOperationException>(() => { ds.Write(new byte[1], 0, 1); });
            }
        }
Ejemplo n.º 40
0
        public void Roundtrip_Write_ReadByte()
        {
            byte[] data = new byte[1024 * 10];
            new Random(42).NextBytes(data);

            var compressed = new MemoryStream();
            using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true))
            {
                compressor.Write(data, 0, data.Length);
            }
            compressed.Position = 0;

            using (var decompressor = new DeflateStream(compressed, CompressionMode.Decompress, true))
            {
                for (int i = 0; i < data.Length; i++)
                    Assert.Equal(data[i], decompressor.ReadByte());
            }
        }
Ejemplo n.º 41
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arg"></param>
        public void compressSend(Object arg) {
            // Now send this update off
            UInt32 hdr;
            byte[] hdrbuf;
            streamThread parent = (streamThread)arg;

            Debug.Assert(buf != null);

            try {
                if (buf.Length == -1)
                    buf = parent._mirror.GetRect(x, y, w, h);
            } catch {
                Trace.WriteLine("getRect failed");
                return;
            }
            if (buf == null)
                return;

            outbuf = new MemoryStream();

            int checkSum = 1;
            using (DeflateStream compress = new DeflateStream(outbuf, CompressionMode.Compress
#if USE_IONIC_ZLIB
                , CompressionLevel.BestSpeed /* CompressionLevel.BestCompression */)){
                compress.FlushMode = FlushType.Sync;
#else
                )) { 
#endif           

                hdr = ((UInt32)DesktopMirror._bitmapWidth << 16) + ((UInt32)DesktopMirror._bitmapHeight & 0xFFFF);
                hdrbuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int32)hdr));
                compress.Write(hdrbuf, 0, hdrbuf.Length);
                checkSum = Adler32.ComputeChecksum(checkSum, hdrbuf, 0, hdrbuf.Length);

                hdr = ((UInt32)Program.maskX << 16) + ((UInt32)Program.maskY & 0xFFFF);
                hdrbuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int32)hdr));
                compress.Write(hdrbuf, 0, hdrbuf.Length);
                checkSum = Adler32.ComputeChecksum(checkSum, hdrbuf, 0, hdrbuf.Length);

                hdr = ((UInt32)Program.maskWidth << 16) + ((UInt32)Program.maskHeight & 0xFFFF);
                hdrbuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int32)hdr));
                compress.Write(hdrbuf, 0, hdrbuf.Length);
                checkSum = Adler32.ComputeChecksum(checkSum, hdrbuf, 0, hdrbuf.Length);

                hdr = ((UInt32)x << 16) + ((UInt32)y & 0xFFFF);
                hdrbuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int32)hdr));
                compress.Write(hdrbuf, 0, hdrbuf.Length);
                checkSum = Adler32.ComputeChecksum(checkSum, hdrbuf, 0, hdrbuf.Length);

                hdr = ((UInt32)w << 16) + ((UInt32)h & 0xFFFF);
                hdrbuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int32)hdr));
                compress.Write(hdrbuf, 0, hdrbuf.Length);
                checkSum = Adler32.ComputeChecksum(checkSum, hdrbuf, 0, hdrbuf.Length);

                /*
    #if USE_BITMAP_COMPRESS
                byte[] bm = new byte[buf.Length / 4];
                byte[] dt = new byte[buf.Length];
                int bi = 0, di = 0;

                for (int i = 0; i < buf.Length; i += sizeof(UInt32))
                    if (buf.buf[i + 3] == 0)
                        bm[bi++] = 0x00;
                    else {
                        bm[bi++] = 0xFF;
                        dt[di++] = buf.buf[i];
                        dt[di++] = buf.buf[i + 1];
                        dt[di++] = buf.buf[i + 2];
                    }

                compress.Write(bm, 0, bi);
                compress.Write(dt, 0, di);
    #else
                compress.Write(buf.buf, 0, buf.Length);
    #endif
                 */
                compress.Write(buf.buf, 0, buf.Length);
            }

            byte[] compHdr = new byte[] { 0x58, 0x85 };
            
            byte[] compData = outbuf.ToArray();
            
            checkSum = Adler32.ComputeChecksum(checkSum, buf.buf, 0, buf.Length);
            int ncheckSum = IPAddress.HostToNetworkOrder(checkSum);
            // byte[] compCheckSum = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(ncheckSum));
            byte[] compCheckSum = BitConverter.GetBytes(ncheckSum);

            hdr = (UInt32)(compHdr.Length + compData.Length + compCheckSum.Length);
            hdrbuf = BitConverter.GetBytes(hdr);
            // Trace.WriteLine("Size: " + (compData.Length));

            // buf.Dispose();
            // Trying to reduce the memory footprint
            // GC.Collect(0, GCCollectionMode.Optimized);
            // GC.Collect(0, GCCollectionMode.Forced);
#if SHOW_STATS
                    if (DateTime.Now.Second != parent.prevSec) {
                        parent.prevSec = DateTime.Now.Second;
                        Trace.WriteLine("DEBUG: Mbps - " + (parent.dataBytes * 8) / 1000000);
                        parent.dataBytes = 0;
                    }
                    parent.dataBytes += (hdrbuf.Length + compHdr.Length + compData.Length + compCheckSum.Length);
#endif
            try {
                this.proceedToSend.WaitOne();   // Wait till I am told to proceed to send
                // this.proceedToSend.Dispose();   // I no longer need this trigger. Dispose now rather than wait for Dispose()
                lock (events.SyncRoot) {    // Reuse
                    events.Add(proceedToSend);
                    proceedToSend = null;
                }
            } catch (Exception e) {
                MessageBox.Show(e.StackTrace);
                Environment.Exit(1);
            }

            if (this.newStream == null) {
#if !SHOW_STATS
                // Deleting inplace causes invalid invocation exception because it is inside the enumerator
                ArrayList failures = new ArrayList();
                foreach (System.Net.Sockets.NetworkStream clnt in parent.clients) {
                    try {
                        clnt.Write(hdrbuf, 0, hdrbuf.Length);
                        clnt.Write(compHdr, 0, compHdr.Length);
                        clnt.Write(compData, 0, compData.Length);
                        clnt.Write(compCheckSum, 0, compCheckSum.Length);
                    } catch (IOException ioe) {
                        Trace.WriteLine("TIMEOUT : " + ioe.Message);
                        // Could've been a timeout of could've been an actual error
                        clnt.Close();
                        failures.Add(clnt);
                    }
                }
                if (failures.Count > 0) {
                    foreach (System.Net.Sockets.NetworkStream clnt in failures) {
                        parent.clients.Remove(clnt);
                    }
                }
#endif
            } else {
                if (parent.clients.Count == 0) // Its been a while
                    parent._mirror.fillScreen();
                parent.clients.Add(this.newStream);

                try {
                    this.newStream.Write(hdrbuf, 0, hdrbuf.Length);
                    this.newStream.Write(compHdr, 0, compHdr.Length);
                    this.newStream.Write(compData, 0, compData.Length);
                    this.newStream.Write(compCheckSum, 0, compCheckSum.Length);
                } catch (IOException) {
                    this.newStream.Close();
                    parent.clients.Remove(this.newStream);
                }
            }
            buf.Dispose();
            buf = null;
            try {
                parent.proceedToNext.Set();
            } catch (Exception e) {
                MessageBox.Show(e.StackTrace);
                Environment.Exit(1);
            }
        }
    }
Ejemplo n.º 42
0
 private static void SerializeStoreData(SessionStateStoreData item, int initialStreamSize, out byte[] buf, out int length, bool compressionEnabled)
 {
     using (MemoryStream stream = new MemoryStream(initialStreamSize))
     {
         Serialize(item, stream);
         if (compressionEnabled)
         {
             byte[] buffer = stream.GetBuffer();
             int count = (int)stream.Length;
             stream.SetLength(0);
             using (DeflateStream stream2 = new DeflateStream(stream, CompressionMode.Compress, true))
             {
                 stream2.Write(buffer, 0, count);
             }
             stream.WriteByte(0xff);
         }
         buf = stream.GetBuffer();
         length = (int)stream.Length;
     }
 }
Ejemplo n.º 43
0
        public void SequentialReadsOnMemoryStream_Return_SameBytes()
        {
            byte[] data = new byte[1024 * 10];
            new Random(42).NextBytes(data);

            var compressed = new MemoryStream();
            using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true))
            {
                for (int i = 0; i < data.Length; i += 1024)
                {
                    compressor.Write(data, i, 1024);
                }
            }
            compressed.Position = 0;

            using (var decompressor = new DeflateStream(compressed, CompressionMode.Decompress, true))
            {
                int i, j;
                byte[] array = new byte[100];
                byte[] array2 = new byte[100];

                // only read in the first 100 bytes
                decompressor.Read(array, 0, array.Length);
                for (i = 0; i < array.Length; i++)
                    Assert.Equal(data[i], array[i]);

                // read in the next 100 bytes and make sure nothing is missing
                decompressor.Read(array2, 0, array2.Length);
                for (j = 0; j < array2.Length; j++)
                    Assert.Equal(data[j], array[j]);
            }
        }
    public static void DeflateCompressDecompress(string filename)
    {
        Console.WriteLine("Test compression and decompression on file {0}", filename);
        FileStream infile;
        try
        {
            // Open the file as a FileStream object.
            infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] buffer = new byte[infile.Length];

            // Read the file to ensure it is readable.
            int count = infile.Read(buffer, 0, buffer.Length);
            if (count != buffer.Length)
            {
                infile.Close();
                Console.WriteLine("Test Failed: Unable to read data from file");
                return;
            }
            infile.Close();

            MemoryStream ms = new MemoryStream();
            // Use the newly created memory stream for the compressed data.
            DeflateStream compressedzipStream = new DeflateStream(ms, CompressionMode.Compress, true);
            Console.WriteLine("Compression");
            compressedzipStream.Write(buffer, 0, buffer.Length);
            // Close the stream.
            compressedzipStream.Close();
            Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);

            // Reset the memory stream position to begin decompression.
            ms.Position = 0;
            DeflateStream zipStream = new DeflateStream(ms, CompressionMode.Decompress);
            Console.WriteLine("Decompression");
            byte[] decompressedBuffer = new byte[buffer.Length + 100];
            // Use the ReadAllBytesFromStream to read the stream.
            int totalCount = DeflateTest.ReadAllBytesFromStream(zipStream, decompressedBuffer);
            Console.WriteLine("Decompressed {0} bytes", totalCount);

            if (!DeflateTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount))
            {
                Console.WriteLine("Error. The two buffers did not compare.");
            }
            zipStream.Close();
        } // end try
        catch (InvalidDataException)
        {
            Console.WriteLine("Error: The file being read contains invalid data.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Error:The file specified was not found.");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
        }
        catch (PathTooLongException)
        {
            Console.WriteLine("Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.");
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("Error: The specified path is invalid, such as being on an unmapped drive.");
        }
        catch (IOException)
        {
            Console.WriteLine("Error: An I/O error occurred while opening the file.");
        }
        catch (UnauthorizedAccessException)
        {
            Console.WriteLine("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("Error: You must provide parameters for MyGZIP.");
        }
    }
Ejemplo n.º 45
0
        public static IEnumerable<object[]> CopyToAsync_Roundtrip_OutputMatchesInput_MemberData()
        {
            var rand = new Random();
            foreach (int dataSize in new[] { 1, 1024, 4095, 1024 * 1024 })
            {
                var data = new byte[dataSize];
                rand.NextBytes(data);

                var compressed = new MemoryStream();
                using (var ds = new DeflateStream(compressed, CompressionMode.Compress, leaveOpen: true))
                {
                    ds.Write(data, 0, data.Length);
                }
                byte[] compressedData = compressed.ToArray();

                foreach (int copyBufferSize in new[] { 1, 4096, 80 * 1024 })
                {
                    // Memory source
                    var m = new MemoryStream(compressedData, writable: false);
                    yield return new object[] { data, copyBufferSize, m };

                    // File sources, sync and async
                    foreach (bool useAsync in new[] { true, false })
                    {
                        string path = Path.GetTempFileName();
                        File.WriteAllBytes(path, compressedData);

                        FileOptions options = FileOptions.DeleteOnClose;
                        if (useAsync) options |= FileOptions.Asynchronous;
                        yield return new object[] { data, copyBufferSize, new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, options) };
                    }
                }
            }
        }
Ejemplo n.º 46
0
        private void PerformTrialWi8870(byte[] buffer)
        {
            TestContext.WriteLine("Original");

            byte[] compressedBytes = null;
            using (MemoryStream ms1 = new MemoryStream())
            {
                using (DeflateStream compressor = new DeflateStream(ms1, CompressionMode.Compress, false))
                {
                    compressor.Write(buffer, 0, buffer.Length);
                }
                compressedBytes = ms1.ToArray();
            }

            TestContext.WriteLine("Compressed {0} bytes into {1} bytes",
                                  buffer.Length, compressedBytes.Length);

            byte[] decompressed= null;
            using (MemoryStream ms2 = new MemoryStream())
            {
                using (var deflateStream = new DeflateStream(ms2, CompressionMode.Decompress, false))
                {
                    deflateStream.Write(compressedBytes, 0, compressedBytes.Length);
                }
                decompressed = ms2.ToArray();
            }

            TestContext.WriteLine("Decompressed");


            bool check = true;
            if (buffer.Length != decompressed.Length)
            {
                TestContext.WriteLine("Different lengths.");
                check = false;
            }
            else
            {
                for (int i=0; i < buffer.Length; i++)
                {
                    if (buffer[i] != decompressed[i])
                    {
                        TestContext.WriteLine("byte {0} differs", i);
                        check = false;
                        break;
                    }
                }
            }

            Assert.IsTrue(check,"Data check failed.");
        }