关于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文件传输过去之后,总会报发送失败. ...
随机推荐
- 剑指Offer:解决难题时的三大方法
1.画图 让抽象的东西变得直观生动起来.比如设计二叉树,链表,栈,队列这些数据结构时. 2.举例子 同样可以化抽象为直观.能够更清晰的展现思路.从例子归纳出一般做法. 3.分解 有时问题本身是比较复杂 ...
- Maven(二)使用eclipse创建maven多模块项目
maven作为一种自动化构建工具,在现在的企业应用开发中运用非常普遍. 企业项目一般都比较大,多采用maven管理的多模块项目,下面直接上创建步骤 一.创建一个maven项目
- 图形学基础教程02--顶点数据和SHADER寄存器 (带演示程序)
本文系原创,欢迎转载,请标明链接 http://www.cnblogs.com/luming1979 有问题欢迎加qq群讨论:366239605
- Notepad++的xml文本格式化
1.需要使用插件 2.使用插件
- ASP.NET权限管理
ASP.NET Web Forms权限管理: 我要将一个文件夹只能让一个用户组访问怎么办? 可否在网站根目录下的web.config里这样设置: <location path="adm ...
- JavaScript笔记:变量及其作用域
一.变量的定义及声明 在javascript中变量仅仅是用来保存值的一个占位符而已,定义变量时要使用关键字var后跟一个变量名,如下所示: var message; //定义一个变量message,像 ...
- sublime 工具构建
1 Sublime Text 3 配置react语法校验 原文地址:https://segmentfault.com/a/1190000004369542?_ea=585496 终端安装 npm in ...
- Create a Listlink
#ifndef List_h__ #define List_h__ #include <stdio.h> struct ListNode { int value; ListNode* pN ...
- Linux下apache+phppgadmin安装配置
1.安装pg 安装PostgreSQL数据库 修改pg_hba.conf配置文件,使得数据库可以通过外部访问. 具体可以配置为: # TYPE DATABASE USER ADDRESS METHOD ...
- Apache 配置 WebSocket 协议
本文使用 http proxy 方式 实现 apache 支持 WebSocket 请求(JK 使用的 ajp 协议不能支持websocket) 通过 apache 访问 后端 tomcat上的 w ...