.Net类库 压缩文件 与 Ionic.Zip 批量压缩不同目录文件与解压 文件
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Ionic.Zip;
using ZipFile = Ionic.Zip.ZipFile;
namespace WebApplication5.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
private string zipFile = $@"e:\压缩文件全部下载测试_{DateTime.Now.ToString("yyyyMMdd")}.rar";
private string[] files = new[]
{
"F:/BQ/xx/-2ed47457e8d3d0a0.jpg",
"F:/BQ/战无不胜表情包/............. - 副本.bmp",
"E:/------/1.柱状图的用法/HighCharts柱状图的使用.png",
};
#region 第一种
/// <summary>
/// 第三方 Ionic.Zip(NuGet 搜索安装)
/// </summary>
/// <returns></returns>
public string DownZip1()
{
string res = string.Empty;
//判断是否存在
if (IsExist(zipFile))
{
//增量压缩
using (ZipFile zip = new ZipFile(zipFile, Encoding.Default))
{
foreach (var item in zip.Entries)
{
res += item.FileName+',';
}
zip.UpdateFiles(files, "");
zip.Save();
}
//删除文件
// DeleteFile(zipFile);
//下载文件
// Download1(zipFile.Replace(@"e:\", ""), zipFile);
// Download2(zipFile.Replace(@"e:\", ""), zipFile);
}
else
{
//设置路径和编码.设置编码为了防止 中文文件名乱码
using (ZipFile zip = new ZipFile(zipFile, Encoding.Default))
{
//zip.Password = "123456"; //加密
zip.AddFiles(files, "");//添加文件
zip.Save();
}
res += $"共压缩{files.Count()}个文件\n";
res += string.Join(Environment.NewLine, files.ToArray());
res += Environment.NewLine;
//删除压缩包中的某个文件
//DeleteFileInZip(zipFile, "-2ed47457e8d3d0a0.jpg");
}
return res;
}
#endregion 第一种
#region 第二种
/// <summary>
/// 微软的(在添加引用中搜索)
/// 1.system.IO.Compression
/// 2.system.IO.Compression.FileSystem
/// </summary>
/// <returns></returns>
public string DownZip2()
{
string res = string.Empty;
try
{
//压缩
using (ZipArchive archive = new ZipArchive(new FileStream(zipFile, FileMode.OpenOrCreate), ZipArchiveMode.Create))//Create,Read,Update
{
string path = string.Empty;
string filename = string.Empty;
for (int i = 0; i < files.Length; i++)
{
path = files[i];
ZipArchiveEntry readMeEntry = archive.CreateEntry(Path.GetFileName(path));
using (System.IO.Stream stream = readMeEntry.Open())
{
byte[] bytes = System.IO.File.ReadAllBytes(path);
stream.Write(bytes, 0, bytes.Length);
}
}
}
//读取文件
using (ZipArchive archive = new ZipArchive(new FileStream(zipFile, FileMode.OpenOrCreate), ZipArchiveMode.Read))
{
int i = 1;
foreach (var zipArchiveEntry in archive.Entries)
{
res += i + ":" + zipArchiveEntry.FullName + ", ";
i++;
}
}
}
catch (Exception e)
{
throw e;
}
return res;
}
#endregion 第二种
#region DownHelper
/// <summary>
/// 删除一个文件
/// </summary>
/// <param name="filePath"></param>
public void DeleteFile(string filePath)
{
System.IO.File.Delete(filePath);
}
/// <summary>
/// 从zip文件中删除一个文件,注意无法直接删除一个文件夹
/// </summary>
/// <param name="zipFilePath">zip路径 eg:E:\\yangfeizai\\test.zip</param>
/// <param name="deleteFileName">要删除文件的名称(无法直接删除文件夹) eg:Jayzai.xml</param>
public void DeleteFileInZip(string zipFilePath, string deleteFileName)
{
using (ZipFile zip = ZipFile.Read(zipFilePath))
{
//zip["Jayzai.xml"] = null;
zip.RemoveEntry(deleteFileName);
zip.Save();
}
}
/// <summary>
/// 检查文件是否存在
/// </summary>
/// <returns></returns>
public bool IsExist(string filePath)
{
if (System.IO.File.Exists(filePath))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 下载文件1
/// </summary>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
public void Download1(string fileName, string filePath)
{
//string fileName = "aaa.txt";//客户端保存的文件名
//string filePath = Server.MapPath("~/Document/123.txt");//路径
FileInfo fileinfo = new FileInfo(filePath);
Response.Clear(); //清除缓冲区流中的所有内容输出
Response.ClearContent(); //清除缓冲区流中的所有内容输出
Response.ClearHeaders(); //清除缓冲区流中的所有头
Response.Buffer = true; //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileinfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/unknow"; //获取或设置输出流的 HTTP MIME 类型
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //获取或设置输出流的 HTTP 字符集
Response.TransmitFile(filePath);
Response.End();
}
/// <summary>
/// 下载文件2
/// </summary>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
public void Download2(string fileName, string filePath)
{
//string fileName = "aaa.txt";//客户端保存的文件名
//string filePath = Server.MapPath("~/Document/123.txt");//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
}
#endregion DownHelper
#region 解压
//// 从 zip 文件中解压出一个文件
//public void ExeSingleDeComp(string fileName)
//{
// using (ZipFile zip = ZipFile.Read(fileName))
// {
// // zip.Password = "123456";// 密码解压
// //Extract 解压 zip 文件包的方法,参数是保存解压后文件的路基
// zip["Jayzai.xml"].Extract(@"c:\\");
// }
//
//// 从 zip 文件中解压全部文件
//public void ExeAllDeComp(string fileName)
//{
// using (ZipFile zip = ZipFile.Read(fileName))
// {
// //zip.Password = "123456";// 密码解压
// foreach (ZipEntry entry in zip)
// {
// //Extract 解压 zip 文件包的方法,参数是保存解压后文件的路基
// entry.Extract(@"c:\\");
// }
// }
//}
#endregion
}
}
.Net类库 压缩文件 与 Ionic.Zip 批量压缩不同目录文件与解压 文件的更多相关文章
- 将ZIP文件添加到程序集资源文件然后在运行时解压文件
今天做安装打包程序研究,之前同事将很多零散的文件发布成一个安装文件夹给用户,这样体验不好,我希望将所有文件打包成一个.net程序,运行此程序的时候自解压然后执行后续的安装步骤. 解决过程: 1,将所有 ...
- 对于使用secureFX上传文件到centos7 的时候,以及不同的用户解压文件,对于文件操作权限的实验
本以为以一个用户胡如root登录了SecureFx,之后选择了root的家目录下的一个software目录,之后上传 以root用户远程登录LINUX系统 查看文件 之后再验证普通用户zhaijh登录 ...
- mac通过自带的ssh连接Linux服务器并上传解压文件
需求: 1:mac连接linux服务器 2:将mac上的文件上传到linux服务器指定位置 3:解压文件 mac上使用命令,推荐使用 iterm2 .当然,也可以使用mac自带的终端工具. 操作过程: ...
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
- Unity3D研究院之LZMA压缩文件与解压文件
原地址:http://www.xuanyusong.com/archives/3095 前两天有朋友告诉我Unity的Assetbundle是LZMA压缩的,刚好今天有时间那么就研究研究LZMA.它是 ...
- 【转载】.NET压缩/解压文件/夹组件
转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...
- C#工具类:使用SharpZipLib进行压缩、解压文件
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...
- 基于Python——实现解压文件夹中的.zip文件
[背景]当一个文件夹里存好好多.zip文件需要解压时,手动一个个解压再给文件重命名是一件很麻烦的事情,基于此,今天介绍一种使用python实现批量解压文件夹中的压缩文件并给文件重命名的方法—— [代码 ...
- 通过SharpZipLib来压缩解压文件
在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库SharpZipLib来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更 ...
随机推荐
- css 点击样式,水波纹(记录备用)
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- entity-framework-core – 实体框架核心RC2表名称复数
参考地址:https://docs.microsoft.com/zh-cn/ef/core/modeling/relational/tables http://www.voidcn.com/artic ...
- linux Ubuntu14.04 make编译文件报错:No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so'. Stop.
错误过程:当“make”编译文件时报错No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so ...
- linux环境weblogic的安装及新建域
环境:inux 64位,jdk 64位, jdk 安装用户应使用weblogic.若使用其他用户安装,须将jdk安装目录整体授权给wblogic用户 安装包:wls1036_dev.zi ...
- 关于/var/log/maillog 时间和系统时间不对应的问题 -- 我出现的是日志时间比系统时间慢12个小时
那么让我们来见证奇迹的时刻吧!! 首先你要看下/etc/localtime的软连接,到哪了 一般就是这块出问题了 检查这里就绝对不会错的 对比图 : 这种情况, 删除/etc/localtime : ...
- django.http.response中HttpResponse 子类
HttpResponse的子类 Django包含许多处理不同类型的HTTP请求的 HttpResponse 子类.像 HttpResponse 一样,这些类在 django.http 中. HttpR ...
- iOS - Scenekit3D引擎初探之 - 给材质贴图
今天简单说一下 SceneKit 给材质贴图. 1,最简单的一种方法,直接打开dae 或者 scn 文件直接设置 如上图,这个dae 文件中只有一个几何体,几何体中只有一个材质球,然后设置材质球的d ...
- 搭建一个简单的React项目
我是使用了create-react-app来搭建的基本框架,其中的原理和vue-cli差不多的脚手架.(当然也可以自己配置项目目录,这里我偷了一下懒) npm install -g create-re ...
- 使用SAP Cloud Platform Leonardo机器学习提取图片的特征向量
选中一个需要进行测试的Leonardo机器学习服务,点击Configure Environments: 因为我不想使用sandbox环境,所以我选择了eu10这个region: 维护clientid和 ...
- centos7创建共享文件夹
0.检查是否已经安装samba rpm -qi samba 1.未安装,安装samba, 如果已安装,请忽略: yum -y install samba samba-client 2.共享一个目录,使 ...