今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为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压缩分散的文件及整理文件夹的方法的更多相关文章

  1. python按照文件创建日期整理文件至文件夹

    # -*- coding: utf-8 -*- # @Time : 2019-02-15 13:31 # @Author : cxa # @File : sortbydate.py # @Softwa ...

  2. C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件

    参考:http://blog.csdn.net/kongwei521/article/details/51167903#

  3. Python定期删除文件、整理文件夹

    1.根据传入的参数,文件所在目录,匹配文件的正则表达式,过期天数进行删除,这些可写在配置文件del_file.conf. del_file3.py #!/usr/bin/env python # en ...

  4. PHP 遍历一个文件夹下所有文件和子文件夹的方法

    话不多说,直接上代码 <?php function my_dir($dir) { $files = []; if(@$handle = opendir($dir)) { while(($file ...

  5. 从零開始学习制作H5应用——V5.0:懊悔机制,整理文件夹,压缩,模板化

    经过前面四个版本号的迭代.我们已经制作了一个从视觉和听觉上都非常舒服的H5微场景应用,没有看过的请戳以下: V1.0--简单页面滑动切换 V2.0--多页切换.透明过渡及交互指示 V3.0--增加lo ...

  6. Freemaker基于word模板动态导出压缩文件汇总整理

    Freemaker基于word模板动态导出压缩文件汇总整理 Freemaker基于word模板动态导出单个文件思路和代码详情见连接: https://www.cnblogs.com/lsy-blogs ...

  7. C#使用SharpZipLib创建压缩文件,并指定压缩文件夹路径(解决SharpZipLib压缩长路径显示问题)

    在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题.比如,压缩当前程序目录下的某个文件夹(D:\cx\code\program\bin\debug ...

  8. C#使用SharpZipLib压缩解压文件

    #region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// </summary> // ...

  9. SharpZipLib 压缩后传输给第三方平台无法识别问题

    问题描述:在项目中需要将文件压缩然后传输给三方进行彩信发送,使用SharpZipLib 进行压缩,原先使用J#进行压缩处理,但是用SharpZipLib压缩后的zip文件传输过去之后,总会报发送失败. ...

随机推荐

  1. storm 配置,呵呵。

    配置项 配置说明 storm.zookeeper.servers ZooKeeper服务器列表 storm.zookeeper.port ZooKeeper连接端口 storm.local.dir s ...

  2. Android自学笔记:Git下载源代码

    Info:做J2ME几年了,现在基本没有公司用了,是时候向Android领域进军了. 自学中,难免会有疏漏,有问题请及时提出,共同学习共同进步. 2014-10-13:初版 2014-10-14:添加 ...

  3. border:none;与border:0;的区别

    border:none表示边框样式无,border:0表示边框宽度为0;当定义了border:none,即隐藏了边框的显示,实际就是边框宽度为0. 当定义边框时,必须定义边框的显示样式.因为边框默认样 ...

  4. python已字典为元素的数组排序

    dict = [ {','name':'b'}, {','name':'c'}, {','name':'a'}, {','name':'g'}, {','name':'f'} ] dict.sort( ...

  5. java小白来报道

    即将开启我的搬码生涯,奋斗吧,少年!

  6. git入门札记

    分布式版本控制(个人主机即版本库,有一台作为“中央服务器”来方便“交换”修改,管理修改 而非文件) vs.  SVN CVS git 安装后设置: git config - -global user. ...

  7. char wchar 互转 多字符 宽字符 的N种方式

    1:  用 CString  如果没有mfc 可以用 ATL 中的 CString  #include <atlstr.h>     CStringA v1 = "111&quo ...

  8. 接口与继承ppt作业

    问:为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来? 答:子类拥有父的成员变量和成员方法,如果不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化.不 ...

  9. JPA merge(obj) 方法

    JPA中的merge类似Hibernate中的saveOrUpdate方法,当数据库中存在id=2的Person,在em.close()时会发送一条update语句,而当数据库中不存在id=2的Per ...

  10. Orchard分类Taxonomies图文教程

    Orchard分类和标签都实现对内容的分类管理,两者区别是分类的子项之间是具有级别(同级.上下级)关系,而标签是很随意的,子项之间可以有关系也可以没有,今天给大家分享分类的使用方法. 一.环境说明 O ...