CSharp_SevenZipSharp压缩解压文本

特别说明:此文为网上资料整理,在这里非常感谢作者的无私奉献;

功能:在内存中对字符串进行压缩解压操作。

 

SevenZipSharp相关文档下载地址:

http://sevenzipsharp.codeplex.com/releases/view/51254

 

1. 解决方案中添加引用:SevenZipSharp.dll

2. 复制粘贴两个dll文件:7z.dll  7z64.dll,并将属性“复制到输出目录”修改为“如果较新则复制”。

3. 工具类实现代码如下:

using System;
using System.Text;
using SevenZip;
using System.IO;

namespace util
{
    class SevenZipUtil
  {
    
    /// <summary>
        /// 获取输入字符串的UTF8编码
        /// </summary>
        /// <param name="input">源字符串</param>
        /// <returns>内存数据流</returns>
        private static MemoryStream GetUTF8MemorySteam(string input)
        {
            MemoryStream ms = new MemoryStream();
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            ms.Write(bytes, 0, bytes.Length);
            return ms;
            }
    
        /// <summary>
        /// 压缩字符串成字节数组
        /// </summary>
        /// <param name="input">源字符串</param>
        /// <returns>压缩后字节数组</returns>
        public static byte[] Compress(string input)
        {
            byte[] compressed = null;

            // 1、压缩对象_SevenZipCompressor
            SevenZipCompressor compressor = new SevenZipCompressor();
            compressor.CompressionMethod = CompressionMethod.Ppmd;	// 压缩方法;
            compressor.CompressionLevel = CompressionLevel.High;	// 高级压缩;
  		   // 2、流对象
            using (MemoryStream msin = GetUTF8MemorySteam(input))
            {
                using (MemoryStream msout = new MemoryStream())
                {
 				  // 3、压缩流
                    compressor.CompressStream(msin, msout);
                    
                    // 4、读入数组
                    msout.Position = 0;		//获取或设置流中的当前位置;
                    compressed = new byte[msout.Length];	// 字节数组;
                    msout.Read(compressed, 0, compressed.Length);		// 读入数组;
                    /*
                    Console.WriteLine("compressed: ");
                    foreach (byte b in compressed)
                    {
                        Console.Write(b);
                        Console.Write(" ");
                    }
                    Console.WriteLine();
                     */
                }
            }
            return compressed;
        }



    /// <summary>
        /// 解压字节数组成字符串
        /// </summary>
        /// <param name="input">源字节数组</param>
        /// <returns>解压后字符串</returns>
        public static string Decompress(byte[] input)
        {
            /*
            Console.WriteLine("input:");
            foreach (byte b in input)
            {
                Console.Write(b);
                Console.Write(" ");
            }
            Console.WriteLine();
            */
            byte[] uncompressedbuffer = null;

            using (MemoryStream msin = new MemoryStream())
            {
                msin.Write(input, 0, input.Length);	// 把字节数组写入流;
                msin.Position = 0;

    			  // 1、解压对象_SevenZipExtractor;
                using (SevenZipExtractor extractor = new SevenZipExtractor(msin))
                {
    // 2、流对象
                    using (MemoryStream msout = new MemoryStream())
                    {
   					 // 3、解压
                        extractor.ExtractFile(0, msout);

  					 // 4、读入数据
                        msout.Position = 0;
                        uncompressedbuffer = new byte[msout.Length];
                        msout.Read(uncompressedbuffer, 0, uncompressedbuffer.Length);
                    }
                }
            }
            return Encoding.UTF8.GetString(uncompressedbuffer);
        }
    }
}


21043779_csharp_opcclient_rcw_code.zip_opc是一个C#语言编写的OPC客户端的代码包。OPC(OLE for Process Control,过程控制的OLE)是一种标准化的通信协议,用于在工业自动化系统中实现设备之间的数据交换和通信。 这个代码包包含了一个C#语言编写的OPC客户端的实现代码。在这个代码包中,我们可以看到一些主要的功能和类,用于与OPC服务器进行连接、读取数据、写入数据等操作。这些代码可以帮助开发人员快速实现自己的OPC客户端应用。 在使用这个代码包时,我们可以根据自己的需要进行一些配置和修改。首先,我们需要配置OPC服务器的相关信息,如服务器IP地址、端口号、用户名和密码等。然后,我们可以根据需要进行数据的读取和写入操作,并对读取到的数据进行相应的处理和展示。 这个代码包使用了C#语言来实现,所以在使用之前,我们需要先安装相应的开发环境和依赖库。同时,我们还需要了解一些OPC的基本概念和原理,以便更好地理解和使用这个代码包。 总而言之,21043779_csharp_opcclient_rcw_code.zip_opc是一个用于实现OPC客户端的C#代码包,可以帮助开发人员快速构建自己的OPC客户端应用。通过这个代码包,我们可以连接OPC服务器,读取和写入数据,并对数据进行相应的处理和展示。在使用之前,我们还需要进行一些配置和准备工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值