关于SharpZipLib压缩分散的文件及整理文件夹的方法
今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为SharpZipLib没有提供压缩进某个指定文件夹的功能,在反复分析了SharpZipLib提供的各个接口方法后,终于找到了解决方法,现在贴出来,给需要的同学参考参考。
下面是封装的压缩类:
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO; namespace CompressTools
{
/// <summary>
/// SharpZipLib压缩
/// </summary>
public class Zip
{
/// <summary>
/// 创建压缩对象
/// </summary>
/// <param name="targeFile">目标文件</param>
/// <returns></returns>
public static ZipOutputStream CreateZip(string targeFile)
{
Directory.CreateDirectory(Path.GetDirectoryName(targeFile));
var s = new ZipOutputStream(File.Create(targeFile));
s.SetLevel(6);
return s;
}
/// <summary>
/// 关闭压缩对象
/// </summary>
/// <param name="zip"></param>
public static void CloseZip(ZipOutputStream zip)
{
zip.Finish();
zip.Close();
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="s">压缩文件流</param>
/// <param name="source">不可空,待压缩的文件</param>
/// <param name="folder">可空,目标文件夹(不指定则默认取待压缩文件目录的计算值)</param>
public static bool Compress(ZipOutputStream s, string source, string folder)
{
if (s == null)
{
throw new FileNotFoundException("压缩文件流不可为空");
}
if (!File.Exists(source))
{
throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", source));
}
using (FileStream fs = File.OpenRead(source))
{
ZipEntry entry = null;
if (string.IsNullOrWhiteSpace(folder))
{
entry = new ZipEntry(source.Replace(Path.GetPathRoot(source), ""));
}
else
{
var path = folder.Contains(":\\") ? folder.Replace(Path.GetPathRoot(folder), "") : folder;
entry = new ZipEntry(path + "\\" + Path.GetFileName(source));
}
var buffer = File.ReadAllBytes(source);
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
return true;
} /// <summary>
/// 解压缩
/// </summary>
/// <param name="sourceFile">源文件</param>
/// <param name="targetPath">目标路经</param>
public static bool Decompress(string sourceFile, string targetPath)
{
if (!File.Exists(sourceFile))
{
throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));
}
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFile)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));
string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
if (directorName.Length > 0)
{
Directory.CreateDirectory(directorName);
}
if (!string.IsNullOrWhiteSpace(fileName))
{
using (FileStream streamWriter = File.Create(fileName))
{
int size = (int)theEntry.Size;
byte[] data = new byte[size];
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
}
}
}
}
return true;
}
}
}
测试方法:
public ActionResult Index()
{
//创建压缩对象
var zip = Zip.CreateZip(@"D:\\testZip\\test.zip");
//根据需要添加压缩对象
Zip.Compress(zip, "E:\\Document\\down.png", "");
Zip.Compress(zip, "E:\\Document\\ending.mp4", "");
Zip.Compress(zip, "E:\\Document\\ending.mp4", "D:\\testChildFolder");
Zip.Compress(zip, "E:\\WorkSpace\\test.jpg", "testChildFolder");
Zip.Compress(zip, "E:\\WorkSpace\\test.jpg", "testFolder\\testChildFolder");
//关闭压缩对象
zip.Close();
// 解压缩
Zip.Decompress(@"D:\\testZip\\test.zip", @"D:\\Zip");
}
关于SharpZipLib压缩分散的文件及整理文件夹的方法的更多相关文章
- python按照文件创建日期整理文件至文件夹
# -*- coding: utf-8 -*- # @Time : 2019-02-15 13:31 # @Author : cxa # @File : sortbydate.py # @Softwa ...
- C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件
参考:http://blog.csdn.net/kongwei521/article/details/51167903#
- Python定期删除文件、整理文件夹
1.根据传入的参数,文件所在目录,匹配文件的正则表达式,过期天数进行删除,这些可写在配置文件del_file.conf. del_file3.py #!/usr/bin/env python # en ...
- PHP 遍历一个文件夹下所有文件和子文件夹的方法
话不多说,直接上代码 <?php function my_dir($dir) { $files = []; if(@$handle = opendir($dir)) { while(($file ...
- 从零開始学习制作H5应用——V5.0:懊悔机制,整理文件夹,压缩,模板化
经过前面四个版本号的迭代.我们已经制作了一个从视觉和听觉上都非常舒服的H5微场景应用,没有看过的请戳以下: V1.0--简单页面滑动切换 V2.0--多页切换.透明过渡及交互指示 V3.0--增加lo ...
- Freemaker基于word模板动态导出压缩文件汇总整理
Freemaker基于word模板动态导出压缩文件汇总整理 Freemaker基于word模板动态导出单个文件思路和代码详情见连接: https://www.cnblogs.com/lsy-blogs ...
- C#使用SharpZipLib创建压缩文件,并指定压缩文件夹路径(解决SharpZipLib压缩长路径显示问题)
在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题.比如,压缩当前程序目录下的某个文件夹(D:\cx\code\program\bin\debug ...
- C#使用SharpZipLib压缩解压文件
#region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// </summary> // ...
- SharpZipLib 压缩后传输给第三方平台无法识别问题
问题描述:在项目中需要将文件压缩然后传输给三方进行彩信发送,使用SharpZipLib 进行压缩,原先使用J#进行压缩处理,但是用SharpZipLib压缩后的zip文件传输过去之后,总会报发送失败. ...
随机推荐
- Ice分布式程序设计—IceBox(Hello World Application)
忙了三天,总算浏览完此书.藉此记下 Ice 的 IceBox 服务框架. 在此用 IceBox 框架写 Hello World 程序,即以载体来体现其特性. 第一步:编写 Slice 文件,映射生成 ...
- android下拉框
XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...
- qt 环境下mapx组件的鼠标跟踪
经过两天的研究mapx组件人坐标转换还是没有转换成功,因为不管怎么变,定点转换的经纬度坐标始终与期望的值有较大的偏差.最后还是想老大请教了一下,划了半天功夫就研究出来了(不愧是老大,仰慕之情犹如滔滔江 ...
- clientTop、offsetTop和scrollTop的区分
页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offse ...
- LR12.53—第5课:创建负载测试场景
在前面的课程中,您使用VuGen将验证您的Vuser脚本.在本课中,您将评估多个Vuser的负载下您的系统.您将模拟十个旅行代理同时使用航班预订系统的行动,以及这些用户的负载下观察系统的行为.设计和运 ...
- 前端菜鸟的编程之路之css的用法
/* * * 固定特殊类 * */ /* ===========固定宽度*============= */ .ld-with80{width: 80px} .ld-with50{width: 50px ...
- 解决MD5问题
使用VS时报错此实现不是 Windows 平台 FIPS 验证的加密算法的一部分. 解决方案如下:在window中打开功能里输入regedit,回车打开注册器.然后进入如下路径中 HKEY_LOCAL ...
- 1219 spring3 项目总结
Spring3 项目总结 列志华 (组长) http://www.cnblogs.com/liezhihua/ 团队guihub https://github.com/LWHTF/OrderingFo ...
- python中获取上一个月一号的方法
业务场景: 我们经常会跑一些月级别或者周级别的报表. 周级别的报表还比较好确定,就是七天前的直接用timedelta(days=7)来获取开始日期就可以了; 但是月级别的报表就要麻烦一些,因为time ...
- ajax和json对象
二维数组 首先是输出json字符串的php文件 header("Content-type: text/html; charset=utf-8"); //json设置utf-8 $a ...