今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为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. Android动态加载学习笔记(一)

    前言 上周五DPAndroid小分队就第二阶段分享内容进行了讨论,结果形成了三个主题:性能优化.动态加载.内核远离.我选择的是第二项——动态加载.在目前的Android开发中,这一部分知识还是比较流行 ...

  2. C++设计模式-Composite组合模式

    Composite组合模式作用:将对象组合成树形结构以表示“部分-整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. UML图如下: 在Component中声明所有用来 ...

  3. DispatcherServlet作用

    DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好 ...

  4. 基于谷歌地图的Dijkstra算法水路路径规划

    最终效果图如下: 还是图.邻接表,可以模拟出几个对象=>节点.边.路径.三个类分别如下: Node 节点: using System; using System.Collections.Gene ...

  5. leetcode 172

    172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: ...

  6. 老生长谈,温故知新:css实现右侧固定宽度,左侧宽度自适应

    反过来也可以:左侧宽度固定,右侧自适应.不管是左是右,反正就是一边宽度固定,一边宽度自适应. 这种布局比较常见,博客园很多默认主题就是这种.一般情况下,这种布局中宽度固定的区域是侧边栏,而自适应的区域 ...

  7. 在js中怎么样选择互斥的相邻元素

    在使用jquery中,我们通常会选择siblings()去选择相邻元素,使用eq()方法去匹配元素,使用index()获取对应元素的索引值,具体jquery代码如下: <style> *{ ...

  8. linux下wps,系统缺失字体:wingdings、wingdings 2、wingdings3

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArcAAACdCAIAAAAhV8dZAAAgAElEQVR4nOzdd1wT9/8H8OvXfjvtt8

  9. 监听JVM的几个命令(可用于linux 本机)

    1. jstat 这个命令对于查看Jvm的堆栈信息很有用.能够查看eden,survivor,old,perm等heap的capacity,utility信息 对于查看系统是不是有能存泄漏以及参数设置 ...

  10. Ajax的基本使用

    AJAX AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = ...