• Windows Live Writer 测试 2007年11月7日


    
    namespace Microshaoft
    {
    	using System;
    	using System.Text;
    	using System.IO;
    	using System.IO.Compression;
    	/// <summary>
    	/// Crypto 的摘要说明。
    	/// </summary>
    	public class CompressHelper
    	{
    
    		private static byte[] ReadStreamToBytes(Stream stream)
    		{
    			byte[] buffer = new byte[64 * 1024];
    			MemoryStream ms = new MemoryStream();
    			int r = 0;
    			int l = 0;
    			long position = -1;
    
    			if (stream.CanSeek)
    			{
    				position = stream.Position;
    				stream.Position = 0;
    			}
    
    			while (true)
    			{
    				r = stream.Read(buffer, 0, buffer.Length);
    				if (r > 0)
    				{
    					l += r;
    					ms.Write(buffer, 0, r);
    				}
    				else
    				{
    					break;
    				}
    			}
    			byte[] bytes = new byte[l];
    			ms.Position = 0;
    			ms.Read(bytes, 0, (int) l);
    			ms.Close();
    			ms = null;
    			if (position >= 0)
    			{
    				stream.Position = position;
    			}
    			return bytes;
    
    		}
    
    		public static byte[] GZipCompress(byte[] data)
    		{
    			MemoryStream ms = new MemoryStream();
    			GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
    			stream.Write(data, 0, data.Length);
    			stream.Close();
    			stream.Dispose();
    			stream = null;
    			byte[] buffer = ReadStreamToBytes(ms);
    			ms.Close();
    			ms.Dispose();
    			ms = null;
    			return buffer;
    		}
    		public static byte[] GZipDecompress(byte[] data)
    		{
    			MemoryStream ms = new MemoryStream(data);
    			GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
    			byte[] buffer = ReadStreamToBytes(stream);
    			ms.Close();
    			ms.Dispose();
    			ms = null;
    			stream.Close();
    			stream.Dispose();
    			stream = null;
    			return buffer;
    		}
    
    		public static byte[] DeflateCompress(byte[] data)
    		{
    			MemoryStream ms = new MemoryStream();
    			DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true);
    			stream.Write(data, 0, data.Length);
    			stream.Close();
    			stream.Dispose();
    			stream = null;
    			byte[] buffer = ReadStreamToBytes(ms);
    			ms.Close();
    			ms.Dispose();
    			ms = null;
    			return buffer;
    		}
    		public static byte[] DeflateDecompress(byte[] data)
    		{
    			MemoryStream ms = new MemoryStream(data);
    			DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress);
    			byte[] buffer = ReadStreamToBytes(stream);
    			ms.Close();
    			ms.Dispose();
    			ms = null;
    			stream.Close();
    			stream.Dispose();
    			stream = null;
    			return buffer;
    		}
    	}
    }
    
    namespace TestConsoleApplication
    {
    	using System.Text;
    	using System;
    	using Microshaoft;
    
    	/// <summary>
    	/// Class1 的摘要说明。
    	/// </summary>
    	public class Class1
    	{
    		/// <summary>
    		/// 应用程序的主入口点。
    		/// </summary>
    		//[STAThread]
    		static void Main(string[] args)
    		{
    			//
    			// TODO: 在此处添加代码以启动应用程序
    			//
    			Console.WriteLine("Hello World");
    			Console.WriteLine(Environment.Version.ToString());
    			string s = "阿斯个贷哈根室电话个撒谎干大事个贷伽师将阿斯个贷哈根室电话个撒谎干大事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将";
    			byte[] buffer = Encoding.UTF8.GetBytes(s);
    
    			byte[] bytes;
    			bytes = CompressHelper.GZipCompress
    									(
    										buffer
    									);
    			Console.WriteLine("{0},GZip: {1}; {2}", buffer.Length, bytes.Length, s.Length);
    
    			string ss = Encoding.UTF8.GetString
    				(
    					(
    						CompressHelper.GZipDecompress
    							(
    								bytes
    							)
    					)
    				);
    			Console.WriteLine(ss);
    
    			bytes = CompressHelper.DeflateCompress
    								(
    									buffer
    								);
    			Console.WriteLine("{0},Deflate: {1}; {2}", buffer.Length, bytes.Length, s.Length);
    			//Console.WriteLine("{0},Deflate: {1}", buffer.Length, bytes.Length);
    			ss = Encoding.UTF8.GetString
    				(
    					(
    						CompressHelper.DeflateDecompress
    							(
    								bytes
    							)
    					)
    				);
    
    			Console.WriteLine(ss);
    		}
    	}
    }
    
    
  • 相关阅读:
    node的二进制权限比对设计
    如何获取和杀死node中子进程的pid,以及系统上的小坑
    node express框架下接收、发送和解析iso-8869-1编码
    node.js 小端十六进制的十进制互转以及十六进制大小端转换
    node加密rsa公钥和python解密私钥的问题
    html-pdf在centos上安装报错
    node.js 的knex 连接数据库表情编码问题
    node.js怎么调用lua脚本操作redis
    mongodb查询非空数组
    位运算
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/952743.html
Copyright © 2020-2023  润新知