[压缩]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.鼠标选择计算机右键,点 ...
随机推荐
- linux echo命令
该篇文章转载于:http://www.cnblogs.com/ZhangShuo/articles/1829589.html linux的echo命令, 在shell编程中极为常用, 在终端下打印变量 ...
- jenkins 如何处理windows batch command
这两天一直被一个问题困扰. 在jenkins的windows batch command 测试好的,拿到bat文件中,再从Execute Windows Batch command 中调用这个bat, ...
- …gen already exists but is not a source folder. Convert to a source folder or rename it [closed]
Right click on the project and go to "Properties" Select "Java Build Path" on th ...
- 求字符串长度StringLength();
// StringLength2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int StringLength(char str[]) ...
- ThreadLocal用法
使用ThreadLocal能实现线程级别的变量定义,同一个类的私有静态变量,在不同的线程中值可以不同. 1.学习文章:http://blog.csdn.net/qjyong/article/detai ...
- python通过标准输入读取内容,读取键盘输入的内容?接收用户输入?
需求说明: 在交互式脚本中,需要用户手动输入内容,并对内容进行处理.在这里记录下通过 python的内置函数input()读取标注输入的内容.默认的标准输入是键盘. 操作过程: 1.通过input() ...
- Unity&C# Time时间相关
1.Unity Time类 1/ Time.time 表示从游戏开发到现在的时间,会随着游戏的暂停而停止计算. 2/ Time.timeSinceLevelLoad 表示从当前Scene开始到目前为止 ...
- Ubuntu 14.04服务器安装及软件配置
1.安装操作系统,配置root账号,通过sudo设置root的密码 如果使用ubuntu server 14.04,开启root需额外配置 1.开启root远程登录权限 sudo vi /etc/ss ...
- MySQL开发面试题
……继上一篇MySQL的开发总结之后,适当的练习还是很有必要的…… SQL语法多变,不敢保证唯一,也不敢保证全对,如果错误欢迎指出,即刻修改. 一.现有表结构如下图 TABLENAME:afinfo ...
- SaltStack 批量执行脚本
这里演示如何使用 salt-master 对多台 salt-minion 批量执行脚本,步骤如下: [root@localhost ~]$ cat /srv/salt/top.sls # 先定义入口配 ...