当前位置: 首页>>代码示例>>C#>>正文


C# DeflateStream.Write方法代码示例

本文整理汇总了C#中System.IO.Compression.DeflateStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# DeflateStream.Write方法的具体用法?C# DeflateStream.Write怎么用?C# DeflateStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Compression.DeflateStream的用法示例。


在下文中一共展示了DeflateStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Compress

 public static byte[] Compress(string data)
 {
     byte[] input = Encoding.ASCII.GetBytes(data);
       MemoryStream ms = new MemoryStream();
       DeflateStream zipper = new DeflateStream(ms, CompressionMode.Compress, true);
       zipper.Write(input, 0, input.Length);
       zipper.Close();
       byte[] output;
       if (ms.Length < input.Length)
       {
     output = new byte[ms.Length + 8];
     SetLength(input.Length, output, 0);
     SetLength((int)ms.Length, output, 4);
     ms.Position = 0;
     ms.Read(output, 8, output.Length - 8);
       }
       else
       {
     output = new byte[input.Length + 8];
     SetLength(input.Length, output, 0);
     SetLength(input.Length, output, 4);
     input.CopyTo(output, 8);
       }
       return output;
 }
开发者ID:VincentRisi,项目名称:jportal,代码行数:25,代码来源:Compression.cs

示例2: Compress

        public static byte[] Compress(byte[] buffer)
        {
            byte[] comp;
            using (var output = new MemoryStream()) {
                using (var deflate = new DeflateStream(output, CompressionMode.Compress))
                    deflate.Write(buffer, 0, buffer.Length);
                comp = output.ToArray();
            }

            // Refer to http://www.ietf.org/rfc/rfc1950.txt for zlib format
            const byte CM = 8;
            const byte CINFO = 7;
            const byte CMF = CM | (CINFO << 4);
            const byte FLG = 0xDA;

            byte[] result = new byte[comp.Length + 6];
            result[0] = CMF;
            result[1] = FLG;
            Buffer.BlockCopy(comp, 0, result, 2, comp.Length);

            uint cksum = ADLER32(buffer);
            var index = result.Length - 4;
            result[index++] = (byte)(cksum >> 24);
            result[index++] = (byte)(cksum >> 16);
            result[index++] = (byte)(cksum >> 8);
            result[index++] = (byte)(cksum >> 0);

            return result;
        }
开发者ID:SirAnuse,项目名称:fabiano-swagger-of-doom,代码行数:29,代码来源:Zlib.cs

示例3: ZipStream

		/// <summary>
		/// ѹËõ¶þ½øÖÆÁ÷
		/// </summary>
		/// <param name="stream">´ýѹËõµÄ¶þ½øÖÆÁ÷</param>
		/// <returns>·µ»ØѹËõºóµÄ¶þ½øÖÆÁ÷</returns>
		/// <remarks>
		/// ѹËõ¶þ½øÖÆÁ÷£ºÍ¨¹ýCreateTempFile()Éú³ÉÁÙʱÎļþ£»Éú³É´ýѹËõµÄ¶þ½øÖÆÁ÷inStream£»·µ»ØѹËõºóµÄ¶þ½øÖÆÁ÷outStream£»
		/// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Compression\GenericZipTest.cs" region="StreamZipTest" lang="cs" title="ѹËõ¶þ½øÖÆÁ÷" />
		/// </remarks>
		public static Stream ZipStream(Stream stream)
		{
			if (false == PreCheck(stream))
				return null;

			#region compress stream by buffers

			MemoryStream result = new MemoryStream();

			Byte[] buff = new Byte[BufferSize];

			using (DeflateStream compressStream = new DeflateStream(result, CompressionMode.Compress, true))
			{
				int cnt = stream.Read(buff, 0, BufferSize);
				while (cnt > 0)
				{
					compressStream.Write(buff, 0, cnt);
					cnt = stream.Read(buff, 0, BufferSize);
				}
			}

			#endregion

			return result;
		}
开发者ID:jerryshi2007,项目名称:AK47Source,代码行数:34,代码来源:GenericZip.cs

示例4: CompressTest

        public void CompressTest()
        {
            var s = "StartTime:13.4.201213:15:26;RunTime:00:01:24";

            var ms = new MemoryStream();
            var ds = new DeflateStream(ms, CompressionMode.Compress);

            var encoding = System.Text.Encoding.UTF8;
            var byteData = encoding.GetBytes(s);

            Trace.WriteLine("original    : {0}", s);
            ds.Write(byteData, 0, byteData.Length);
            ds.Close();
            byte[] compressed = ms.ToArray();

            Trace.WriteLine("compressed  : {0}", System.Convert.ToBase64String(compressed));

            ms = new MemoryStream(compressed);
            ds = new DeflateStream(ms, CompressionMode.Decompress);
            compressed = new byte[compressed.Length + 100];
            var offset = 0;

            while (true)
            {
                int bytesRead = ds.Read(compressed, offset, 1);
                if (bytesRead == 0) { break; }
                offset += bytesRead;
            }

            ds.Close();

            string uncompressed = encoding.GetString(compressed);

            Trace.WriteLine("uncompressed: {0}\n", uncompressed);
        }
开发者ID:ttu,项目名称:scratchpad,代码行数:35,代码来源:DataGetterTest.cs

示例5: Compress

        /// <summary>
        /// 지정된 데이타를 압축한다.
        /// </summary>
        /// <param name="input">압축할 Data</param>
        /// <returns>압축된 Data</returns>
        public override byte[] Compress(byte[] input) {
            if(IsDebugEnabled)
                log.Debug(CompressorTool.SR.CompressStartMsg);

            // check input data
            if(input.IsZeroLength()) {
                if(IsDebugEnabled)
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);

                return CompressorTool.EmptyBytes;
            }

            byte[] output;
            using(var outStream = new MemoryStream()) {
                using(var deflate = new DeflateStream(outStream, CompressionMode.Compress)) {
                    deflate.Write(input, 0, input.Length);
                }
                output = outStream.ToArray();
            }

            if(IsDebugEnabled)
                log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);

            return output;
        }
开发者ID:debop,项目名称:NFramework,代码行数:30,代码来源:DeflateCompressor.cs

示例6: Compress

		public void Compress(ref byte[] buffer, ref int length)
		{
			using (var outS = new MemoryStream())
			{
				using (var ds = new DeflateStream(outS, CompressionMode.Compress, true))
				{
					ds.Write(buffer, 0, length);
				}

				outS.Position = 0;

				// Recycle the buffer?
				if (outS.Length <= buffer.Length)
				{
					length = outS.Read(buffer, 0, buffer.Length);

					// Heartbleed: Nope; zero-fill the remaining buffer!
					for (var i = length; i < buffer.Length; i++)
					{
						buffer[i] = 0;
					}
				}
				else
				{
					buffer = outS.ToArray();
					length = buffer.Length;
				}
			}
		}
开发者ID:Ravenwolfe,项目名称:Core,代码行数:29,代码来源:Client.cs

示例7: Compress

		/// <summary>
		/// Returns compressed version of given string.
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string Compress(string str)
		{
			var barr = Encoding.Unicode.GetBytes(str + '\0'); // Why did we append a null byte... was this required?
			using (var mout = new MemoryStream())
			{
				// Deflate should use optimal compression level by default (0, as defined by .NET 4.5).
				using (var df = new DeflateStream(mout, CompressionMode.Compress))
				{
					// Write compressed data to memory stream.
					df.Write(barr, 0, barr.Length);
				}

				// Compression method
				int cmf = 8; // cm
				cmf += 7 << 4; // cinfo

				// Flags
				int flg = 2 << 6; // Compression level
				int n = ((cmf * 256) + flg) % 31;
				if (n != 0)
					flg += 31 - n; // Check bits

				// <length>;<cmf><flg><data><checksum>
				return string.Format("{0};{1:x02}{2:x02}{3}{4:x08}", barr.Length, cmf, flg, BitConverter.ToString(mout.ToArray()).Replace("-", "").ToLower(), ComputeAdler32(barr));
			}
		}
开发者ID:tkiapril,项目名称:aura,代码行数:31,代码来源:MabiZip.cs

示例8: GetEncodedReportFile

 private string GetEncodedReportFile()
 {
     string report = null;
      try {
     string exePath = Application.ExecutablePath;
     string appPath = Path.GetDirectoryName(exePath);
     string logFileName = Path.Combine(appPath, defaultLogFileName);
     if (!File.Exists(logFileName)) {
        return null;
     }
     byte[] logFileBytes = File.ReadAllBytes(logFileName);
     using (MemoryStream ms = new MemoryStream()) {
        using (DeflateStream deflateStream = new DeflateStream(ms, CompressionMode.Compress, false)) {
           deflateStream.Write(logFileBytes, 0, logFileBytes.Length);
           byte[] compressedBytes = ms.ToArray();
           report = Convert.ToBase64String(compressedBytes);
        }
     }
      }
      catch (ArgumentException e) {
     Trace.TraceWarning(e.ToString());
      }
      catch (IOException e) {
     Trace.TraceWarning(e.ToString());
      }
      catch (SecurityException e) {
     Trace.TraceWarning(e.ToString());
      }
      catch (UnauthorizedAccessException e) {
     Trace.TraceWarning(e.ToString());
      }
      return report;
 }
开发者ID:bspkrs,项目名称:screenrecorder,代码行数:33,代码来源:FeedbackPresenter.cs

示例9: Compress

            public byte[] Compress( byte[] buffer )
            {
                MemoryStream result = new MemoryStream();
                int offset = 0;

                BinaryWriter bw = new BinaryWriter( result );

                bw.Write( new byte[] { 0x54, 0x4C, 0x5A, 0x43 } );
                bw.Write( (byte)0x01 );
                bw.Write( (byte)0x02 );
                bw.Write( (byte)0x00 );
                bw.Write( (byte)0x00 );
                bw.Write( (int)0 );   // compressed size - we'll fill this in once we know it
                bw.Write( (int)buffer.Length );   // decompressed size
                bw.Write( (int)0 );   // unknown, 0
                bw.Write( (int)0 );   // unknown, 0

                using ( DeflateStream compressionStream = new DeflateStream( result, CompressionLevel.Optimal ) ) {
                    compressionStream.Write( buffer );
                }

                byte[] retval = result.ToArray();

                // fill in compressed size
                Util.CopyByteArrayPart( BitConverter.GetBytes( (int)retval.Length ), 0, retval, 8, 4 );

                return retval;
            }
开发者ID:AdmiralCurtiss,项目名称:HyoutaTools,代码行数:28,代码来源:TLZC.cs

示例10: Deflate

 /// <summary>
 ///		Compresses a data buffer using a simple huffman deflate algorithem.
 /// </summary>
 /// <param name="data">Data buffer to compress.</param>
 /// <returns>Compressed version of data buffer.</returns>
 public static byte[] Deflate(byte[] data)
 {
     MemoryStream memoryStream = new MemoryStream();
     DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true);
     deflateStream.Write(data, 0, data.Length);
     deflateStream.Close();
     return memoryStream.ToArray();
 }
开发者ID:HampsterEater,项目名称:FusionGameEngine,代码行数:13,代码来源:Data+Methods.cs

示例11: Compress

 public void Compress(string content, Stream targetStream)
 {
     using (var writer = new DeflateStream(targetStream, CompressionMode.Compress))
     {
         var bytes = Encoding.UTF8.GetBytes(content);
         writer.Write(bytes, 0, bytes.Length);
     }
 }
开发者ID:Sundio,项目名称:combres,代码行数:8,代码来源:DeflateCompressor.cs

示例12: Compress

 /// <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();
 }
开发者ID:Azerothian,项目名称:Empire,代码行数:14,代码来源:ByteArrays.cs

示例13: Compress

 public static byte[] Compress(byte[] raw, int offset, int count)
 {
     var memory = new MemoryStream();
     using (var deflate = new DeflateStream(memory, CompressionMode.Compress, true))
     {
         deflate.Write(raw, offset, count);
         return memory.ToArray();
     }
 }
开发者ID:xiaomiwk,项目名称:Cowboy,代码行数:9,代码来源:DeflateCompression.cs

示例14: Compress

		public static byte[] Compress( byte[] input, bool addFirst2Bytes ) {
			MemoryStream output = new MemoryStream();
			output.Write( new byte[] { 120, 156 }, 0, 2 );

			using( DeflateStream Deflate = new DeflateStream( output, CompressionMode.Compress ) ) {
				Deflate.Write( input, 0, input.Length );
			}

			return output.ToArray();
		}
开发者ID:GodLesZ,项目名称:svn-dump,代码行数:10,代码来源:Deflate.cs

示例15: CompressBlock

 /// <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;
     }
 }
开发者ID:ceeji,项目名称:CeejiCommonLibrary,代码行数:19,代码来源:GZipAlgorithm.cs


注:本文中的System.IO.Compression.DeflateStream.Write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。