【.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 ...
随机推荐
- Android Studio 错误: 非法字符: '\ufeff'
右下角:选UTF-8 convert一下,再重新编译..不知道为什么,本来好像就是UTF-8好奇怪. 还看到一个方法但我没试过,放在这里万一下次又遇见了这样的问题呢 右下角将UTF-8 convert ...
- C# 数组结构
数组结构: Array :在内存上是连续分配的,而且元素类型是一致的: 特点:是读取快 可以坐标访问 但是增删慢,长度不能变 比如 int[] intArray=new int[20]; intArr ...
- 业务配置开发平台qMISPlat 2.0 产品介绍
qMISPlat是什么 qMISPlat(业务配置开发平台)是一套基于.net core 2.0.跨平台的,面向开发人员和具有一定技术水平的业务人员使用的业务配置开发平台.基于此平台您只需通过配置和少 ...
- 【安富莱专题教程第7期】终极调试组件Event Recorder,各种Link通吃,支持时间和功耗测量,printf打印,RTX5及中间件调试
说明:1.继前面的专题教程推出SEGGER的RTT,JScope,Micrium的uC/Probe之后,再出一期终极调试方案Event Recoder,之所以叫终极解决方案,是因为所有Link通吃. ...
- 美图App的移动端DNS优化实践:HTTPS请求耗时减小近半
本文引用了颜向群发表于高可用架构公众号上的文章<聊聊HTTPS环境DNS优化:美图App请求耗时节约近半案例>的部分内容,感谢原作者. 1.引言 移动互联网时代,APP 厂商之间的竞争非常 ...
- [Swift]LeetCode206. 反转链表 | Reverse Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- [Swift]LeetCode209. 长度最小的子数组 | Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [Swift]LeetCode263. 丑数 | Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- spring boot - 整合jpa
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- Linux下python2和python3共存
python下载地址: https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tar.xz 下载命令: wget https://www.pytho ...