当前位置 : 主页 > 手机开发 > harmonyos >

CSharp_SevenZipSharp压缩解压文本

来源:互联网 收集:自由互联 发布时间:2023-08-26
特别说明:此文为网上资料整理,在这里非常感谢作者的无私奉献; 功能:在内存中对字符串进行压缩解压操作。 SevenZipSharp相关文档下载地址: http://sevenzipsharp.codeplex.com/releases/view


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

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

 

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);
        }
    }
}




【文章转自:游戏服务器 http://www.558idc.com/yz.html 复制请保留原URL】
网友评论