【.Net Core】ZipFile类--文件的压缩解压

NuGet引用官网自带的System.IO.Compression.ZipFile;
var filename = "测试压缩解压文件";
var path = Directory.GetCurrentDirectory() + "/wwwroot/file/" + filename + ".pdf";
//这里是获取到文件的byte数组
byte[] fileBuffer1 = Convert.FromBase64String(invData);
//将byte数组转换为文件并保存到指定地址
FileSystem.Bytes2File(fileBuffer1, path);
//添加文件到指定压缩文件中
ZipFile.CreateFromDirectory(Directory.GetCurrentDirectory() + "/wwwroot/file", Directory.GetCurrentDirectory() + "/wwwroot/" + zipFileName);
//删除文件夹下的所有文件
FileSystem.DeleteDirAllFile(Directory.GetCurrentDirectory() + "/wwwroot/file/");
byte[] fileBuffer;
//文件转成byte二进制数组
fileBuffer = FileSystem.File2Bytes(Directory.GetCurrentDirectory() + "/wwwroot/" + zipFileName);
//这里开始上传文件
//将指定 zip 存档中的所有文件都解压缩到文件系统的一个目录下
ZipFile.ExtractToDirectory(Directory.GetCurrentDirectory() + "/wwwroot/" + zipFileName,Directory.GetCurrentDirectory() + "/wwwroot/file");
public class FileSystem
{
/// <summary>
/// 将byte数组转换为文件并保存到指定地址
/// </summary>
/// <param name="buff">byte数组</param>
/// <param name="savepath">保存地址</param>
public static void Bytes2File(byte[] buff, string savepath)
{
if (System.IO.File.Exists(savepath))
{
System.IO.File.Delete(savepath);
} FileStream fs = new FileStream(savepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff, , buff.Length);
bw.Close();
fs.Close();
} /// <summary>
/// 将文件转换为byte数组
/// </summary>
/// <param name="path">文件地址</param>
/// <returns>转换后的byte数组</returns>
public static byte[] File2Bytes(string path)
{
if (!System.IO.File.Exists(path))
{
return new byte[];
} FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead();
fs.Read(buff, , Convert.ToInt32(fs.Length));
fs.Close(); return buff;
} /// <summary>
/// 删除文件夹下的所有文件
/// </summary>
/// <param name="dirRoot"></param>
public static void DeleteDirAllFile(string dirRoot)
{
DirectoryInfo aDirectoryInfo = new DirectoryInfo(Path.GetDirectoryName(dirRoot));
FileInfo[] files = aDirectoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo f in files)
{
File.Delete(f.FullName);
}
}
}
【.Net Core】ZipFile类--文件的压缩解压的更多相关文章
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...
- python对文件的压缩解压
python自带的zipfile的模块支持对文件的压缩和解压操作 zipfilp.ZipFile 表示创建一个zip对象 zipfile.ZipFile(file[, mode[, compressi ...
- Linux_文件打包,压缩,解压
一.压缩命令 文件格式:*.gz 命令:gzip 文件名 (ps:不能压缩目录,切压缩后不保留原文件) 压缩前 -rw-r--r--. 1 root root 315 Sep 6 21:03 df.t ...
- 使用PHP对文件进行压缩解压(zip)
使用虚拟主机进行文件上传时最常用的工具莫过于FTP了,但是使用FTP有一个弊端就是文件太多时上传或下载速度比较慢,如果上传时将文件打包,上传后在 空间解压缩,同样下载前将文件打包压缩以压缩包的形式下载 ...
- C++ 使用老牌库xzip & unzip对文件进行压缩解压
原文链接 https://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win https://www.code ...
- linux笔记:linux常用命令-压缩解压命令
压缩解压命令:gzip(压缩文件,不保留原文件.这个命令不能压缩目录) 压缩解压命令:gunzip(解压.gz的压缩文件) 压缩解压命令:tar(打包压缩目录或者解压压缩文件.打包的意思是把目录打包成 ...
- Linux下解包/打包,压缩/解压命令
.tar 解包:tar xvf FileName.tar 打包:tar cvf fileName.tar DirName tar.gz和.tgz 解压:tar zxvf FileName.tar.zi ...
- [Linux] 016 压缩解压命令
1. 压缩解压命令:gzip 命令名称:gzip 命令所在路径:/bin/gzip 执行权限:所有用户 语法:gzip [文件] 功能描述:压缩文件 压缩后文件的格式:.gz 补充: 解压 .rar ...
随机推荐
- swust oj 1013
哈希表(开放定址法处理冲突) 1000(ms) 10000(kb) 2698 / 6177 采用除留余数法(H(key)=key %n)建立长度为n的哈希表,处理冲突用开放定址法的线性探测. 输入 第 ...
- History of program (language).
1 编程语言的发展历程及未来趋势 1.1 编程语言的发展 · 1946 Plankalkul Konrad Zuse,一位德国工程师,他躲藏在巴伐利亚附近的阿尔卑斯山上时,独立开发了Plank ...
- TCP协议学习总结(中)
很多人都说TCP协议是一个十分复杂的协议,在学习当中,我对协议每一个问题都分解学习后,每一个分解我都能体会和理解它的要点,并不难理解.但我把这些拆分的细节合并后,确认感觉这样一个协议相对“臃肿”但又好 ...
- SUSE12Sp3-Supervisor 守护.net core进程
1.安装setuptools 将setuptools-0.6c11.tar.gz安装包放到服务器上 tar zxvf setuptools-0.6c11.tar.gz cd setuptools-0. ...
- [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [Swift]LeetCode764. 最大加号标志 | Largest Plus Sign
In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...
- [Swift]LeetCode970.强整数 | Powerful Integers
Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...
- djang-异步——定时操作
django本身是一个同步框架,flask也是,所以要把它变成异步操作的话还得专门设置一下 我的这个系统呢是windows系统,python3.7的 所以有的库是不可以兼容的 ,然后到时候会稍微修改一 ...
- 【Sqoop篇】----Sqoop从搭建到应用案例
一.前述 今天开始讲解Sqoo的用法搭建和使用.Sqoop其实功能非常简单.主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型 ...
- HTML基础知识点
HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) **** ...