.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来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更 ...
随机推荐
- bootstrap的tree使用
效果图: 先引用,顺序很重要 <script src="~/Content/bootstrap-table/bootstrap-table.min.js"></s ...
- restTemplate源码解析(目录)
restTemplate是spring实现的,基于restful风格的http请求模板.使用restTemplate可以简化请求操作的复杂性,同时规范了代码风格.本系列文章,将根据以下目录顺序,从源码 ...
- 基于【 建造者模式】一 || 网关zuul过滤器封装
一.springcloud的zuul网关拦截 1.黑名单拦截 2.参数验签 3.Api接口权限验证 二.网关拦截实现方式 1.继承ZuulFilter方法,实现业务逻辑 @Component @Slf ...
- 【转载】Sqlserver根据生日计算年龄
在Sqlserver中,可以根据存储的出生年月字段计算出该用户的当前年龄信息,主要使用到DateDiff函数来实现.DateDiff函数的格式为DATEDIFF(datepart,startdate, ...
- 高并发编程系列:ConcurrentHashMap的实现原理(JDK1.7和JDK1.8)
HashMap.CurrentHashMap 的实现原理基本都是BAT面试必考内容,阿里P8架构师谈:深入探讨HashMap的底层结构.原理.扩容机制深入谈过hashmap的实现原理以及在JDK 1. ...
- 【前端开发】】ES6属性promise封装js动画
如下是我写的demo源码: 可以直接复制用浏览器打开看到效果哦: <!DOCTYPE html> <html> <head> <meta charset=&q ...
- unittest管理测试用例
#coding=utf-8 from selenium import webdriver from time import sleep import unittest #导入unittest库 imp ...
- Mysql 中的SSL 连接
Mysql 中的SSL 连接 以下来自网络参考和自己测试整理,没有查找相关资料.若有错误之处,欢迎指正. 当前的Mysql 客户端版本基本都不太能支持 caching_sha2_password 认证 ...
- python网络爬虫之爬取图片
今天使用requests和BeautifulSoup爬取了一些图片,还是很有成就感的,注释可能有误,希望大家多提意见: 方法一:requests import requests from bs4 im ...
- unittest 运行slenium(一)---创建配置类
文章主要是创建: log : 日志文件 excel :文档的读写 ini 及 yaml :文件的读取 一:创建log日志文件 主要是对logging框架进行二次封装并输出自己需要的日志格式 1. 首先 ...