[压缩]C#下使用SevenZipSharp压缩解压文本
using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SLibrary
{
public class SevenZipUtil
{
/// <summary>
/// 压缩字符串
/// </summary>
/// <param name="input">源字符串</param>
/// <returns>压缩后字节数组</returns>
public static byte[] Compress(string input)
{
byte[] compressed = null;
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.CompressionMethod = CompressionMethod.Ppmd;
compressor.CompressionLevel = CompressionLevel.High;
using (MemoryStream msin = GetUTF8MemorySteam(input))
{
using (MemoryStream msout = new MemoryStream())
{
compressor.CompressStream(msin, msout); msout.Position = ;
compressed = new byte[msout.Length];
msout.Read(compressed, , compressed.Length);
/*
Console.WriteLine("compressed: ");
foreach (byte b in compressed)
{
Console.Write(b);
Console.Write(" ");
}
Console.WriteLine();
*/
}
}
return compressed;
} /// <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, , bytes.Length);
return ms;
} /// <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, , input.Length);
uncompressedbuffer = new byte[input.Length];
msin.Position = ;
using (SevenZipExtractor extractor = new SevenZipExtractor(msin))
{
using (MemoryStream msout = new MemoryStream())
{
extractor.ExtractFile(, msout);
msout.Position = ;
uncompressedbuffer = new byte[msout.Length];
msout.Read(uncompressedbuffer, , uncompressedbuffer.Length);
}
}
}
return Encoding.UTF8.GetString(uncompressedbuffer);
} }
}
[压缩]C#下使用SevenZipSharp压缩解压文本的更多相关文章
- Linux tar (打包.压缩.解压缩)命令说明 | tar如何解压文件到指定的目录?
打包举例:将 /usr/local/src/zlib-1.2.5目录下的文件打包成 zlib-1.2.5.tar.gz cd /usr/local/src tar -czvf ./zlib-1.2.5 ...
- ubuntu下各种压缩包的解压命令
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...
- linux下rar包的解压方法
linux下rar包的解压方法 学习了:https://blog.csdn.net/yonggeit/article/details/72190246?utm_source=itdadao&u ...
- Windows下安装zip包解压版mysql
Windows下安装zip包解压版mysql 虽然官方提供了非常好的安装文件,但是有的时候不想每次再重装系统之后都要安装一遍MySQL,需要使用zip包版本的MySQL.在安装时需如下三步: 1. 新 ...
- linux下的三种解压文件的命令?
那要看你的压缩文件使用哪种压缩方式:gzip,压缩文件名:zip或gz,解压命令:unzipbzip2,压缩文件名:bz,解压命令:bzip2 -d上面两个是最常用的压缩方式,一般在linux下可以通 ...
- Windows下MySQL8.0.13解压版安装教程
下载 MySQL8.0.13-64位下载地址 在下载页面的底部,有三种安装包,第一种是MySQL的安装程序,下载完点击安装即可. 第二种是普通的压缩版,体积较小. 第三种是自带debug和测试的压缩版 ...
- Winserver2012下mysql 5.7解压版(zip)配置安装
一.安装 下载mysqlzip版本mysql不需要运行可执行文件,解压即可,下载zip版本mysqlmsi版本mysql双击文件即可安装,相对简单,本文不介绍此版本安装 配置环境变量打开环境变量配置页 ...
- linux下tar.xz 文件解压
在linux下下载源码文件安装时有些会遇到tar.xz文件的解压,习惯了tar解压缩,第一次遇到.xz文件还是有点迷惑,google 如下,解压这种格式的文件需要xz工具,如果xz工具没有安装,则安装 ...
- Win10下Mysql5.7.13,解压版安装流程
一.环境变量配置 1.将下载好的压宿包解压到安装目录,我的安装目录就是:D:\DevelopmentTool\Mysql5.7.13\mysql-5.7.13-winx64 2.鼠标选择计算机右键,点 ...
随机推荐
- bootstrapValidator 表单验证
官网下载地址:http://plugins.jquery.com/bootstrapValidator/ html代码 <!DOCTYPE html> <html> <h ...
- thinkphp 外部js语言包
Thinkphp php文件也外部js文件公用同一个语言包 一 . php语言包转json数据格式 1.新建验证字段的语言包 application\common\lang\validate-cn.p ...
- centos 上安装nodejs v8.0.0
新建目录www 下载nodejs wget https://npm.taobao.org/mirrors/node/v8.0.0/node-v8.0.0-linux-x64.tar.xz 解压 tar ...
- 如果BarTender出现卸载不干净的问题如何处理
自从BarTender 2016出了之后,好多小伙伴都想试试新功能咋样,这就意味着首先要卸载电脑上旧版BarTender.然而就是这个操作,难倒了好一批人,他们表示BarTender卸载卸不干净,不仅 ...
- spring数据连接池配置参数
1.使用jdbc连接,每次使用完毕需要关闭连接. 2.使用数据库连接池就会方便很多,但是如果参数配置不对,也会引起mysql连接数不够,导致mysql挂掉,基本原理是:不使用的连接数及时回收回来,而不 ...
- QT 随机数生成
下面总结了QT中随机生成的方法(仅供学习参考),分为旧方法和新方法,一般来说,旧的方法已经被抛弃,在开发新的应用中推荐使用新方法. C++ Code 12345678910111213141516 ...
- linux环境安装nagiosgraph将nagios的性能数据绘制成动态图表?
需求描述: 在安装完成nagios之后,比如有监控磁盘负载信息的,连接数的,进程数的,可以通过安装nagiosgraph软件, 将nagios的性能数据绘制成图表,可以看到一段时间内数据的变化 环境说 ...
- Android跑指定包Monkey脚本
Android跑指定包Monkey脚本 adb shell monkey –p com.android.mms --throttle 1000 -v -v -v -s 1 --ignore-secur ...
- Wcf使用Net.Tcp做回调操作
契约: [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = Se ...
- JavaScript-this理解
javascript this可以绑定到:全局对象,自己定义的对象,用构造函数生成的对象,通过call或者apply更改绑定的对象 1.全局对象 function globalTest(nam ...