Flush() публичный Метод

public Flush ( ) : void
Результат void
Пример #1
0
 public override void Flush()
 {
     if (deflateStream != null)
     {
         deflateStream.Flush();
     }
 }
Пример #2
0
 /// <summary>
 /// 压缩
 /// </summary>
 /// <param name="inStream">输入流</param>
 /// <param name="outStream">输出流</param>
 public static void Compress(this Stream inStream, Stream outStream)
 {
     Stream stream = new DeflateStream(outStream, CompressionMode.Compress, true);
     CopyTo(inStream, stream);
     stream.Flush();
     stream.Close();
 }
        /// <summary>
        /// Get EditorsTable from server response.
        /// </summary>
        /// <param name="subResponse">The sub response from server.</param>
        /// <param name="site">Transfer ITestSite into this operation, for this operation to use ITestSite's function.</param>
        /// <returns>The instance of EditorsTable.</returns>
        public static EditorsTable GetEditorsTableFromResponse(FsshttpbResponse subResponse, ITestSite site)
        {
            if (subResponse == null || subResponse.DataElementPackage == null || subResponse.DataElementPackage.DataElements == null)
            {
                site.Assert.Fail("The parameter CellResponse is not valid, check whether the CellResponse::DataElementPackage or CellResponse::DataElementPackage::DataElements is null.");
            }

            foreach (DataElement de in subResponse.DataElementPackage.DataElements)
            {
                if (de.Data.GetType() == typeof(ObjectGroupDataElementData))
                {
                    ObjectGroupDataElementData ogde = de.Data as ObjectGroupDataElementData;

                    if (ogde.ObjectGroupData == null || ogde.ObjectGroupData.ObjectGroupObjectDataList.Count == 0)
                    {
                        continue;
                    }

                    for (int i = 0; i < ogde.ObjectGroupData.ObjectGroupObjectDataList.Count; i++)
                    {
                        if (IsEditorsTableHeader(ogde.ObjectGroupData.ObjectGroupObjectDataList[i].Data.Content.ToArray()))
                        {
                            string editorsTableXml = null;

                            // If the current object group object data is the header byte array 0x1a, 0x5a, 0x3a, 0x30, 0, 0, 0, 0, then the immediate following object group object data will contain the Editor table xml. 
                            byte[] buffer = ogde.ObjectGroupData.ObjectGroupObjectDataList[i + 1].Data.Content.ToArray();
                            System.IO.MemoryStream ms = null;
                            try
                            {
                                ms = new System.IO.MemoryStream();
                                ms.Write(buffer, 0, buffer.Length);
                                ms.Position = 0;
                                using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress))
                                {
                                    stream.Flush();
                                    byte[] decompressBuffer = new byte[buffer.Length * 3];
                                    stream.Read(decompressBuffer, 0, buffer.Length * 3);
                                    stream.Close();
                                    editorsTableXml = System.Text.Encoding.UTF8.GetString(decompressBuffer);
                                }

                                ms.Close();
                            }
                            finally
                            {
                                if (ms != null)
                                {
                                    ms.Dispose();
                                }
                            }

                            return GetEditorsTable(editorsTableXml);
                        }
                    }
                }
            }

            throw new InvalidOperationException("Cannot find any data group object data contain editor tables information.");
        }
Пример #4
0
 /// <summary>
 /// Compresses the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <returns></returns>
 public static byte[] Compress(byte[] data)
 {
     MemoryStream ms = new MemoryStream();
     DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
     ds.Write(data, 0, data.Length);
     ds.Flush();
     ds.Close();
     return ms.ToArray();
 }
Пример #5
0
 public override void Flush()
 {
     if (deflateStream == null)
     {
         throw new ObjectDisposedException(null, SR.GetString(SR.ObjectDisposed_StreamClosed));
     }
     deflateStream.Flush();
     return;
 }
Пример #6
0
        public override void Flush()
        {
            if (deflateStream == null)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            deflateStream.Flush();
        }
Пример #7
0
 /// <summary>压缩数据流</summary>
 /// <param name="inStream">输入流</param>
 /// <param name="outStream">输出流</param>
 public static void Compress(this Stream inStream, Stream outStream)
 {
     // 第三个参数为true,保持数据流打开,内部不应该干涉外部,不要关闭外部的数据流
     using (Stream stream = new DeflateStream(outStream, CompressionMode.Compress, true))
     {
         inStream.CopyTo(stream);
         stream.Flush();
         stream.Close();
     }
 }
Пример #8
0
        public static byte[] Compress(byte[] data)
        {
            var memoryStream = new MemoryStream();
            var deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress);
            deflateStream.Write(data, 0, data.Length);
            deflateStream.Flush();
            deflateStream.Close();

            return memoryStream.ToArray();
        }
Пример #9
0
 /// <summary>
 /// 转换指定字节数组的指定区域。
 /// </summary>
 /// <param name="inputBuffer">要为其计算转换的输入。</param>
 /// <param name="inputOffset">字节数组中的偏移量,从该位置开始使用数据。</param>
 /// <param name="inputCount">字节数组中用作数据的字节数。</param>
 /// <returns>计算所得的转换。</returns>
 public override byte[] CompressFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
 {
     using (var ms = new MemoryStream()) {
         using (var outStream = new DeflateStream(ms, CompressionMode.Compress)) {
             outStream.Write(inputBuffer, inputOffset, inputCount);
             outStream.Flush();
         }
         return ms.ToArray();
     }
 }
Пример #10
0
 /// <summary>
 /// 转换指定字节数组的指定区域。
 /// </summary>
 /// <param name="inputBuffer">要为其计算转换的输入。</param>
 /// <param name="inputOffset">字节数组中的偏移量,从该位置开始使用数据。</param>
 /// <param name="inputCount">字节数组中用作数据的字节数。</param>
 /// <param name="outputBuffer">将转换写入的输出。</param>
 /// <param name="outputOffset">输出字节数组中的偏移量,从该位置开始写入数据。</param>
 /// <returns>计算所得的转换的字节数。</returns>
 public override int CompressBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
 {
     using (var ms = new MemoryStream(outputBuffer, outputOffset, outputBuffer.Length - outputOffset, true)) {
         using (var outStream = new DeflateStream(ms, CompressionMode.Compress)) {
             outStream.Write(inputBuffer, inputOffset, inputCount);
             outStream.Flush();
         }
         return (int)ms.Position;
     }
 }
 internal static byte[] Compress(byte[] data)
 {
     using (var ms = new MemoryStream())
     {
         var ds = new DeflateStream(ms, CompressionMode.Compress);
         ds.Write(data, 0, data.Length);
         ds.Flush();
         ds.Close();
         return ms.ToArray();
     }
 }
Пример #12
0
 public static string CompressToBinary(object obj)
 {
     MemoryStream ms = new MemoryStream();
     new BinaryFormatter().Serialize(ms, obj);
     byte[] inbyt = ms.ToArray();
     MemoryStream objStream = new MemoryStream();
     DeflateStream objZS = new DeflateStream(objStream, CompressionMode.Compress);
     objZS.Write(inbyt, 0, inbyt.Length);
     objZS.Flush();
     objZS.Close();
     return Convert.ToBase64String(objStream.ToArray());
 }
Пример #13
0
 /// <summary>Flushes the contents of the internal buffer of the current stream object to the underlying stream.</summary>
 /// <exception cref="T:System.ObjectDisposedException">The stream is closed.</exception>
 /// <PermissionSet>
 ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
 /// </PermissionSet>
 public override void Flush()
 {
     if (this.disposed)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (this.CanWrite)
     {
         int result = DeflateStream.Flush(this.z_stream);
         DeflateStream.CheckResult(result, "Flush");
     }
 }
Пример #14
0
        static Patch processChunk(byte[] orig, byte[] mod, int chunkStart, int chunkEnd) {
            int patchSize = chunkEnd - chunkStart;
            Debug.Assert(patchSize < Patch.MaxPatchSize);
            int matchAreaSize = Patch.MatchAreaSize(patchSize);
            int matchBefore = (matchAreaSize - patchSize) / 2;
            int matchAfter = matchAreaSize - (patchSize + matchBefore);
            Debug.Assert(matchBefore <= chunkStart);
            Debug.Assert(chunkEnd + matchAfter <= orig.Length);

            byte[] origBytes = new byte[matchAreaSize];
            byte[] patchBytes = new byte[patchSize];
            Array.ConstrainedCopy(orig, chunkStart - matchBefore, origBytes, 0, matchAreaSize);
            Array.ConstrainedCopy(mod, chunkStart, patchBytes, 0, patchSize);
            MemoryStream ms = new MemoryStream();
            DeflateStream comp = new DeflateStream(ms, CompressionMode.Compress);
            comp.WriteByte(0);
            comp.Flush();
            long base_len = ms.Length;
            comp.Write(origBytes, 0, origBytes.Length);
            comp.Flush();
            return new Patch(origBytes, patchBytes, matchBefore, Convert.ToInt32(ms.Length - base_len));
        }
Пример #15
0
 /// <summary>
 /// Compresses binary data with Deflate method.
 /// </summary>
 /// <param name="Data">Byte array with data to compress.</param>
 /// <returns>Returns the result byte array with compressed data if successful, otherwise returns NULL.</returns>
 public static byte[] CompressData(byte[] Data)
 {
     try
     {
         var MS = new MemoryStream();
         var CS = new DeflateStream(MS, CompressionMode.Compress, true);
         CS.Write(Data, 0, Data.Length);
         CS.Flush();
         CS.Close();
         return MS.ToArray();
     }
     catch	{	return null;	}
 }
Пример #16
0
 public static byte[] CompressionObject(object DataOriginal) {
     if (DataOriginal == null) return null;
     var bFormatter = new BinaryFormatter();
     var mStream = new MemoryStream();
     bFormatter.Serialize(mStream, DataOriginal);
     var bytes = mStream.ToArray();
     var oStream = new MemoryStream();
     var zipStream = new DeflateStream(oStream, CompressionMode.Compress);
     zipStream.Write(bytes, 0, bytes.Length);
     zipStream.Flush();
     zipStream.Close();
     return oStream.ToArray();
 }
Пример #17
0
        static void Main(string[] args)
        {
            string file;

            if (args.Length > 0) file = args[0];
            else
            {
                Console.Write("File: ");
                file = Console.ReadLine();
            }

            if (file == null || !File.Exists(file)) Environment.Exit(1);

            var input = new FileStream(file, FileMode.Open);
            var output = new FileStream(file + ".sd0", FileMode.Create);

            using (var writer = new BinaryWriter(output, Encoding.UTF8))
            {
                writer.Write('s');
                writer.Write('d');
                writer.Write('0');
                writer.Write((byte)0x01);
                writer.Write((byte)0xff);

                const int chunkMaxLength = 1024 * 256;

                int read;
                var buffer = new byte[chunkMaxLength];

                var chunkNumber = 1;

                while ((read = input.Read(buffer, 0, chunkMaxLength)) > 0)
                {
                    Console.WriteLine("Writing chunk {0}...", chunkNumber);
                    using (var outputStream = new MemoryStream())
                    using (var compressionStream = new DeflateStream(outputStream, CompressionMode.Compress, true))
                    {
                        compressionStream.Write(buffer, 0, read);
                        compressionStream.Flush();
                        compressionStream.Close();

                        writer.Write(outputStream.ToArray().Length);
                        writer.Write(outputStream.ToArray());
                    }
                    Console.WriteLine(" ->OK");
                    chunkNumber++;
                }

                Console.WriteLine("Done.");
            }
        }
Пример #18
0
 public static object DecompressFromBinary(string text)
 {
     byte[] bytCook = Convert.FromBase64String(text);
     MemoryStream inMs = new MemoryStream(bytCook);
     inMs.Seek(0, 0);
     DeflateStream zipStream = new DeflateStream(inMs, CompressionMode.Decompress, true);
     byte[] outByt = ReadFullStream(zipStream);
     zipStream.Flush();
     zipStream.Close();
     MemoryStream outMs = new MemoryStream(outByt);
     outMs.Seek(0, 0);
     BinaryFormatter bf = new BinaryFormatter();
     return (object)bf.Deserialize(outMs, null);
 }
Пример #19
0
 public static int CompressTileBlock(int xStart, int yStart, short width, short height, byte[] buffer, int bufferStart)
 {
     int result;
     using (MemoryStream memoryStream = new MemoryStream())
     {
         using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
         {
             binaryWriter.Write(xStart);
             binaryWriter.Write(yStart);
             binaryWriter.Write(width);
             binaryWriter.Write(height);
             NetMessage.CompressTileBlock_Inner(binaryWriter, xStart, yStart, (int)width, (int)height);
             int num = buffer.Length;
             if ((long)bufferStart + memoryStream.Length > (long)num)
             {
                 result = (int)((long)(num - bufferStart) + memoryStream.Length);
             }
             else
             {
                 memoryStream.Position = 0L;
                 MemoryStream memoryStream2 = new MemoryStream();
                 using (DeflateStream deflateStream = new DeflateStream(memoryStream2, CompressionMode.Compress, true))
                 {
                     memoryStream.CopyTo(deflateStream);
                     deflateStream.Flush();
                     deflateStream.Close();
                     deflateStream.Dispose();
                 }
                 if (memoryStream.Length <= memoryStream2.Length)
                 {
                     memoryStream.Position = 0L;
                     buffer[bufferStart] = 0;
                     bufferStart++;
                     memoryStream.Read(buffer, bufferStart, (int)memoryStream.Length);
                     result = (int)memoryStream.Length + 1;
                 }
                 else
                 {
                     memoryStream2.Position = 0L;
                     buffer[bufferStart] = 1;
                     bufferStart++;
                     memoryStream2.Read(buffer, bufferStart, (int)memoryStream2.Length);
                     result = (int)memoryStream2.Length + 1;
                 }
             }
         }
     }
     return result;
 }
Пример #20
0
 public static byte[] CompressionDataSet(DataSet dsOriginal)
 {
     if (dsOriginal == null) return null;
     dsOriginal.RemotingFormat = SerializationFormat.Binary;
     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();
 }
Пример #21
0
 public static byte[] CompressionArrayList(IList 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();
 }
Пример #22
0
        public static byte[] CompressDataSet(DataSet ds)
        {
            ds.RemotingFormat = SerializationFormat.Binary;
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream    ms = new MemoryStream();

            bf.Serialize(ms, ds);
            byte[] inbyt = ms.ToArray();
            System.IO.MemoryStream objStream          = new MemoryStream();
            System.IO.Compression.DeflateStream objZS = new System.IO.Compression.DeflateStream(objStream, System.IO.Compression.CompressionMode.Compress);
            objZS.Write(inbyt, 0, inbyt.Length);
            objZS.Flush();
            objZS.Close();
            return(objStream.ToArray());
        }
Пример #23
0
        public static void ScaleNumericValues(string srcTrainPath, string srcTestPath, string dstTrainPath, string dstTestPath)
        {
            Console.WriteLine("Computing means");
            var means = OneHotRecord.GetMeans(srcTrainPath, srcTestPath);
            Console.WriteLine("Computing stddevs");
            var stdevs = OneHotRecord.GetStdDevs(means, srcTrainPath, srcTestPath);
            var paths = new List<string> { srcTrainPath + "^" + dstTrainPath, srcTestPath + "^" + dstTestPath };

            foreach (var pathItem in paths)
            {
                var pathItems = pathItem.Split('^');
                var srcPath = pathItems[0];
                var dstPath = pathItems[1];

                if (File.Exists(dstPath)) File.Delete(dstPath);
                var fileStream = File.OpenWrite(dstPath);
                var compressedStream = new DeflateStream(fileStream, CompressionMode.Compress);
                var writer = new BinaryWriter(compressedStream);

                Console.WriteLine("Standardizing" + Path.GetFileName(srcPath));
                var writeNo = 0;
                foreach (var rec in OneHotRecord.EnumerateBinLines(srcPath))
                {
                    for (var i = 0; i < RawRecord.NUMERIC_COUNT; i++)
                    {
                        var val = rec.NumericData[i];
                        if (float.IsNaN(val))
                        {
                            rec.NumericData[i] = 0f;
                        }
                        else
                        {
                            var newVal = (rec.NumericData[i] - means[i]) / stdevs[i];
                            if (newVal > 3f) newVal = 3f;
                            if (newVal < -3f) newVal = -3f;
                            rec.NumericData[i] = newVal;
                        }
                    }
                    rec.WriteBinary(writer);
                    writeNo++;
                }
                writer.Flush();
                compressedStream.Flush();
                compressedStream.Close();
                fileStream.Close();
            }
        }
Пример #24
0
        byte[] Compress(byte[] data)
        {
            byte[] compressedData;

            using (var ms = new MemoryStream())
            {
                using (var ds = new DeflateStream(ms, CompressionLevel.Optimal))
                {
                    ds.Write(data, 0, data.Length);
                    ds.Flush();
                }

                compressedData = ms.ToArray();
            }

            return compressedData;
        }
        public static string ZipBase64(string s)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream    ms = new MemoryStream();

            bf.Serialize(ms, s);
            byte[] inbyt = ms.ToArray();
            System.IO.MemoryStream objStream          = new MemoryStream();
            System.IO.Compression.DeflateStream objZS = new System.IO.Compression.DeflateStream(objStream, System.IO.Compression.CompressionMode.Compress);
            objZS.Write(inbyt, 0, inbyt.Length);
            objZS.Flush();
            objZS.Close();
            byte[] b             = objStream.ToArray();
            string ZippedBased64 = Convert.ToBase64String(b);

            return(ZippedBased64);
        }
Пример #26
0
        public static byte[] Deflate(byte[] data, CompressionLevel level = CompressionLevel.Fastest)
        {
            byte[] compressedData;

            using (var ms = new MemoryStream())
            {
                using (var ds = new DeflateStream(ms, level))
                {
                    ds.Write(data, 0, data.Length);
                    ds.Flush();
                }

                compressedData = ms.ToArray();
            }

            return compressedData;
        }
Пример #27
0
        public static bool Set(string cookieName, object cookieValue)
        {
            bool retval = true;

            try
            {
                BinaryFormatter bf = new BinaryFormatter();

                MemoryStream ms = new MemoryStream();

                bf.Serialize(ms, cookieValue);

                byte[] inbyt = ms.ToArray();

                System.IO.MemoryStream objStream = new MemoryStream();

                System.IO.Compression.DeflateStream objZS = new System.IO.Compression.DeflateStream(objStream, System.IO.Compression.CompressionMode.Compress);

                objZS.Write(inbyt, 0, inbyt.Length);

                objZS.Flush();

                objZS.Close();

                byte[] b = objStream.ToArray();

                string sCookieVal = Convert.ToBase64String(b);

                HttpCookie cook = new HttpCookie(cookieName);

                cook.Value = sCookieVal;

                cook.Expires = DateTime.Today.AddDays(30);

                HttpContext.Current.Response.Cookies.Add(cook);
            }

            catch
            {
                retval = false;

                throw;
            }

            return(retval);
        }
Пример #28
0
 /// <summary>
 /// Deflate解压函数
 /// JS:var details = eval_r('(' + utf8to16(zip_depress(base64decode(hidEnCode.value))) + ')')对应的C#压缩方法
 /// </summary>
 /// <param name="strSource"></param>
 /// <returns></returns>
 public static string DeflateDecompress(this string strSource)
 {
     byte[] buffer = Convert.FromBase64String(strSource);
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
     {
         ms.Write(buffer, 0, buffer.Length);
         ms.Position = 0;
         using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress))
         {
             stream.Flush();
             int    nSize            = 16 * 1024 + 256; //假设字符串不会超过16K
             byte[] decompressBuffer = new byte[nSize];
             int    nSizeIncept      = stream.Read(decompressBuffer, 0, nSize);
             stream.Close();
             return(System.Text.Encoding.UTF8.GetString(decompressBuffer, 0, nSizeIncept));   //转换为普通的字符串
         }
     }
 }
        public static void ConvertCSVToBinary(string csvPath, string binaryPath)
        {
            Console.WriteLine("Converting CSV to binary");
            if (File.Exists(binaryPath)) File.Delete(binaryPath);
            var fileStream = File.OpenWrite(binaryPath);
            var deflateStream = new DeflateStream(fileStream, CompressionMode.Compress);
            var writer = new BinaryWriter(deflateStream);

            foreach (var rawRecord in RawRecord.EnumerateCSVFile(csvPath))
            {
                rawRecord.WriteBinary(writer);
            }

            writer.Flush();
            deflateStream.Flush();
            deflateStream.Close();
            fileStream.Close();
        }
Пример #30
0
 public Idat(byte[] imageData, bool needToDeflate = false)
     : base(ChunkType.IDAT)
 {
     if(!needToDeflate)
         MemStream = new MemoryStream(imageData);
     else
         {
         MemStream = new MemoryStream();
         MemStream.WriteByte(0x78);
         MemStream.WriteByte(0x9c);
         using(var gzip = new DeflateStream(MemStream, CompressionMode.Compress, true))
             {
             gzip.Write(imageData, 0, imageData.Length);
             gzip.Flush();
             }
         MemStream.Write(Adler32(imageData, 0, imageData.Length), 0, 4);
         }
 }
        public static Stream CompressStream(Stream instream)
        {
            MemoryStream outstream = new MemoryStream((int)instream.Length);
            DeflateStream comp = new DeflateStream(outstream, CompressionMode.Compress, true);

            int numBytes;
            byte[] buffer = new byte[4096];
            while ((numBytes = instream.Read(buffer, 0, 4096)) != 0) {
                comp.Write(buffer, 0, numBytes);
            }
            comp.Flush();
            comp.Dispose();

            // return to the beginning of the stream
            outstream.Position = 0;

            //Debug.WriteLine("Compression: " + instream.Length.ToString() + " to " + outstream.Length.ToString());
            return outstream;
        }
Пример #32
0
		public static void SaveToMTZ(String inFile, String outFile)
		{
			FileStream ifs = new FileStream(inFile, FileMode.Open, FileAccess.Read);
			FileStream ofs = new FileStream(outFile, FileMode.Create, FileAccess.Write);

			Byte[] buffer = new Byte[11];

			Int64 uncompressedSize = ifs.Length;

			buffer[0] = (Byte)'M';
			buffer[1] = (Byte)'T';
			buffer[2] = (Byte)'Z';

			buffer[3] = (Byte)(uncompressedSize & 0xFF);
			buffer[4] = (Byte)((uncompressedSize & 0xFF00) >> 8);
			buffer[5] = (Byte)((uncompressedSize & 0xFF0000) >> 16);
			buffer[6] = (Byte)((uncompressedSize & 0xFF000000) >> 24);

			buffer[7] = 0;
			buffer[8] = 0;
			buffer[9] = 0;
			buffer[10] = 0;

			ofs.Write(buffer, 0, 11);

			DeflateStream ds = new DeflateStream(ofs, CompressionMode.Compress, true);
			ifs.CopyTo(ds);
			ds.Flush();
			ds.Close();
			ifs.Close();

			ofs.Seek(7, SeekOrigin.Begin);

			Int64 compressedSize = (ofs.Length - 11);
			buffer[3] = (Byte)(compressedSize & 0xFF);
			buffer[4] = (Byte)((compressedSize & 0xFF00) >> 8);
			buffer[5] = (Byte)((compressedSize & 0xFF0000) >> 16);
			buffer[6] = (Byte)((compressedSize & 0xFF000000) >> 24);

			ofs.Write(buffer, 3, 4);
			ofs.Close();
		}
Пример #33
0
 public static string DeflateDecompress(string strSource)
 {
     string str;
     byte[] buffer = Convert.FromBase64String(strSource);
     using (MemoryStream stream = new MemoryStream())
     {
         stream.Write(buffer, 0, buffer.Length);
         stream.Position = 0L;
         using (DeflateStream stream2 = new DeflateStream(stream, CompressionMode.Decompress))
         {
             stream2.Flush();
             int count = 0x4100;
             byte[] buffer2 = new byte[count];
             int num2 = stream2.Read(buffer2, 0, count);
             stream2.Close();
             str = Encoding.UTF8.GetString(buffer2, 0, num2);
         }
     }
     return str;
 }
Пример #34
0
        public Fdat(UInt32 frame, byte[] imageData, bool needToDeflate = false)
            : base(ChunkType.fdAT)
        {
            _frame = frame;

            if(!needToDeflate)
                MemStream = new MemoryStream(imageData);
            else
                {
                MemStream = new MemoryStream();
                MemStream.Write(NoHelper.GetBytes(_frame),0,4);
                MemStream.WriteByte(0x78);
                MemStream.WriteByte(0x9c);
                using(var gzip = new DeflateStream(MemStream, CompressionMode.Compress, true))
                    {
                    gzip.Write(imageData, 0, imageData.Length);
                    gzip.Flush();
                    }
                MemStream.Write(Adler32(imageData, 0, imageData.Length), 0, 4);
                }
        }
Пример #35
0
    internal static unsafe T[] Compress <T>(ReadOnlySpan <byte> data) where T : unmanaged
    {
        if (typeof(T) == typeof(byte))
        {
            return((T[])(object)CompressBytes(data));
        }

        long requiredSize = CompressedLengthMax(data);
        long capacity     = AlignCount <T>(requiredSize);

        if (capacity > int.MaxValue)
        {
            var resultArray  = CompressBytes(data);
            T[] copiedResult = GC.AllocateUninitializedArray <T>(AlignCount <T>(resultArray.Length));
            resultArray.AsReadOnlySpan().CopyTo(copiedResult.AsSpan().AsBytes());
            return(copiedResult);
        }

        T[]  result = GC.AllocateUninitializedArray <T>((int)capacity);
        long resultLength;

        fixed(T *resultPtr = result)
        {
            using var resultStream = new UnmanagedMemoryStream((byte *)resultPtr, result.Length * sizeof(T));

            using var compressor = new IOC.DeflateStream(resultStream, IOC.CompressionLevel.Optimal);
            compressor.Write(data.ToArray(), 0, data.Length);
            compressor.Flush();
            resultLength = resultStream.Position;
        }

        if (result.Length != resultLength)
        {
            Array.Resize(ref result, (int)resultLength);
        }

        return(result);
    }
Пример #36
0
        public static ServerPacket CompressPacket(Packet packet)
        {
            // We need the opcode as uint.
            var msg = BitConverter.GetBytes((uint)packet.Header.Message);

            packet.Data[0] = msg[0];
            packet.Data[1] = msg[1];
            packet.Data[2] = msg[2];
            packet.Data[3] = msg[3];

            var compression = new Compression
            {
                UncompressedSize = packet.Data.Length,
                UncompressedAdler = Helper.Adler32(packet.Data)
            };

            // Compress.
            using (var ms = new MemoryStream())
            {
                using (var ds = new DeflateStream(ms, CompressionLevel.Fastest))
                {
                    ds.Write(packet.Data, 0, packet.Data.Length);
                    ds.Flush();
                }

                compression.CompressedData = ms.ToArray();
            }

            compression.CompressedData[0] -= 1;

            if ((compression.CompressedData[compression.CompressedData.Length - 1] & 8) == 8)
                compression.CompressedData = compression.CompressedData.Combine(new byte[1]);

            compression.CompressedData = compression.CompressedData.Combine(new byte[] { 0x00, 0x00, 0xFF, 0xFF });
            compression.CompressedAdler = Helper.Adler32(compression.CompressedData);

            return compression;
        }
Пример #37
0
        public static Stream Compress(Stream inputStream)
        {
            MemoryStream compressedStream = new MemoryStream();
            compressedStream.Write(new byte[] { (byte)'O', (byte)'W', (byte)'Z', (byte)'!' }, 0, 4);

            int uncompressedSize = (int)inputStream.Length;

            inputStream.Seek(0, SeekOrigin.Begin);

            byte[] inputReadBuffer = new byte[uncompressedSize];

            using (DeflateStream deflateStream = new DeflateStream(compressedStream, CompressionMode.Compress, true))
            {
                int bytesRead = inputStream.Read(inputReadBuffer, 0, inputReadBuffer.Length);

                deflateStream.Write(inputReadBuffer, 0, bytesRead);
                deflateStream.Flush();
            }

            compressedStream.Flush();
            compressedStream.Seek(0, SeekOrigin.Begin);
            return compressedStream;
        }
Пример #38
0
        public static int CompressTileBlock(int xStart, int yStart, short width, short height, byte[] buffer, int bufferStart)
        {
            int result;
            using (MemoryStream memoryStream = new MemoryStream(buffer, bufferStart, SendQueue.kSendQueueLargeBlockSize))
            {
                using (DeflateStream ds = new DeflateStream(memoryStream, CompressionMode.Compress, leaveOpen: true))
                using (BinaryWriter binaryWriter = new BinaryWriter(ds))
                {
                    binaryWriter.Write(xStart);
                    binaryWriter.Write(yStart);
                    binaryWriter.Write(width);
                    binaryWriter.Write(height);

                    NetMessage.CompressTileBlock_Inner(binaryWriter, xStart, yStart, width, height);

                    ds.Flush();
                }

                result = (int) memoryStream.Position;
            }

            return result;
        }
        public void CompressorNotClosed_DecompressorStillSuccessful(bool closeCompressorBeforeDecompression)
        {
            const string Input = "example";

            var ms = new MemoryStream();

            using (var compressor = new DeflateStream(ms, CompressionLevel.Optimal, leaveOpen: closeCompressorBeforeDecompression))
            {
                compressor.Write(Encoding.ASCII.GetBytes(Input));
                compressor.Flush();
                if (closeCompressorBeforeDecompression)
                {
                    compressor.Dispose();
                }

                ms.Position = 0;
                using (var decompressor = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                {
                    var decompressed = new MemoryStream();
                    decompressor.CopyTo(decompressed);
                    Assert.Equal(Input, Encoding.ASCII.GetString(decompressed.ToArray()));
                }
            }
        }
        protected static void Process(string srcPath, string dstPath, bool test)
        {
            if (File.Exists(dstPath)) File.Delete(dstPath);
            var fileStream = File.OpenWrite(dstPath);
            var deflateStream = new DeflateStream(fileStream, CompressionMode.Compress);
            var writer = new BinaryWriter(deflateStream);
            foreach (var rec in RawRecord.EnumerateBinLines(srcPath))
            {
                for (var i = 0; i < RawRecord.CATEGORICAL_COUNT; i++)
                {
                    var catNo = i + 1;
                    var idx = RawRecord.NUMERIC_COUNT + i;
                    var val = rec.Values[idx];
                    var testCount = _testCounts[catNo][val];
                    var trainCount = _trainCounts[catNo][val];


                    if (testCount == 0)
                    {
                        rec.Values[idx] = Constants.VALUE_TRAINNOTTEST;
                        continue;
                    }
                    if (trainCount == 0)
                    {
                        rec.Values[idx] = Constants.VALUE_TESTNOTTRAIN;
                        //rec.Values[idx] = RawRecord.MISSING;
                        continue;
                    }

                    var threshHold = _categoricalValueFrequencyFilter[catNo];
                    if (trainCount < threshHold)
                    {
                        rec.Values[idx] = Constants.VALUE_LOWFREQUENCY;
                    }
                }
                rec.WriteBinary(writer);
            }
            writer.Flush();
            deflateStream.Flush();
            deflateStream.Close();
            fileStream.Close();
        }
Пример #41
0
 public override void Flush()
 {
     CheckDeflateStream();
     _deflateStream.Flush();
     return;
 }
Пример #42
0
 public override void Flush()
 {
     deflateStream.Flush();
 }