今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为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. RabbitMQ(四)

    RabbitMQ 配置 一.RabbitMQ 配置修改方式 1.修改环境变量 2.修改配置文件(只介绍这个) 3.修改运行时参数和政策 locate rabbitmq vi /var/log/rabb ...

  2. 在网页中怎样给已发布的Flash添加链接的方法(zhuan)

    因为网页中的 Flash 是以控件形式出现的,优先级别较高,所以直接对它加链接是无效的,不过可以用按钮控件 BUTTON 来实现. 具体步骤 1.直接在按钮上加上onClick事件打开指定页面: &l ...

  3. Commons-Collections 集合工具类的使用

    package com.bjsxt.others.commons; import java.util.ArrayList; import java.util.List; import org.apac ...

  4. 移动端网页fixed布局问题解决方案

    问题说明 移动端web的footer常常设计为fixed布局,但是在页面键盘被拉起时fixed的布局会出现问题,自己试了下,在较低版本ios和部分安卓机上会有此问题.具体问题看图示: <body ...

  5. Value must be an existing directory配置tomcat问题

    今天tomcat配置遇到了这么个问题,表示tomcat下少什么文件夹,一般是你的安装目录tomcat文件夹下面少了一个temp文件夹,在tomcat的安装文件夹下建一个temp文件夹再加一次tomca ...

  6. PAT 02-线性结构2 一元多项式的乘法与加法运算 (20分)

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  7. javaWeb学习-----session

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  8. android常用调试工具fiddle、wireshark和android studio的配置

    Fiddle配置android代理 在wifi的同一个局域网环境的windows主机中安装fiddler,并且启动,如本次192.168.3.14 在android手机端配置代理为该主机 还有一种方式 ...

  9. 传说中的requestAnimFrame

    //让浏览器以10ms绘制 兼容写法                window.requestAnimFrame = (function() {                    return ...

  10. sql语句_分页查询

    1.查询数据库中存在某一列名的表 use [db] go SELECT name FROM sysobjects WHERE id IN (SELECT id FROM syscolumns WHER ...