C# 压缩文件 ICSharpCode.SharpZipLib.dll
效果:
代码只能压缩文件夹里面的文件,不能压缩文件夹。
压缩前:
压缩后:
代码:
需要引用ICSharpCode.SharpZipLib.dll
public ActionResult Index()
{
//路径
string path = "E:/test/xls_Z";
//调用
CreateZip(path ,path + ".zip");
} /// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFilePath">文件路径</param>
/// <param name="destinationZipFilePath">压缩后的地址</param>
public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
{
if (sourceFilePath[sourceFilePath.Length - ] != System.IO.Path.DirectorySeparatorChar)
sourceFilePath += System.IO.Path.DirectorySeparatorChar;
ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(destinationZipFilePath));
zipStream.SetLevel(); // 压缩级别 0-9
CreateZipFiles(sourceFilePath, zipStream);
zipStream.Finish();
zipStream.Close();
} /// <summary>
/// 递归压缩文件
/// </summary>
/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名
/// <param name="staticFile"></param>
private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream)
{
Crc32 crc = new Crc32();
string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
foreach (string file in filesArray)
{
//如果当前是文件夹,递归
if (Directory.Exists(file))
{
CreateZipFiles(file, zipStream);
}
//如果是文件,开始压缩
else
{
FileStream fileStream = System.IO.File.OpenRead(file);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, , buffer.Length);
string tempFile = file.Substring(sourceFilePath.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(tempFile);
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, , buffer.Length);
}
}
}
C# 压缩文件 ICSharpCode.SharpZipLib.dll的更多相关文章
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll 以下是压缩的通用方法: using System; using System.IO; using System.Web ...
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...
- ICSharpCode.SharpZipLib.dll 压缩多文件
网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...
- zip (ICSharpCode.SharpZipLib.dll文件需要下载)
ZipClass zc=new ZipClass (); zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩 ...
- ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用
MyZip.dll : 有BUG,会把子目录的文件解压到根目录.. ICSharpCode.SharpZipLib.dll: 把ICSharpCode.SharpZipLib.dll复制一份,重命名为 ...
- ICSharpCode.SharpZipLib.dll 移植WP
由于众所周知的原因. ICSharpCode.SharpZipLib.dll在Unity移植WP的时候出现诸多API不兼容,解决方案是在在Github上面找ICSharpCode.SharpZipLi ...
随机推荐
- Redis面试题及答案整理
1.什么是Redis?简述它的优缺点? Redis的全称是:Remote Dictionary.Server,本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载 ...
- [Arch] 01. Before Design Patterns - UML
From: 史上最全设计模式导学目录 设计模式,这是软件设计过程中的一个环节. 在这个环节之上,需要overview的事业,就是UML,一种通用的建模语言. Ref: 软件设计之UML—UML的构成[ ...
- Eclipse cdt解决github导入的项目无法打开声明的bug (cannot open declaration)
概述: 我利用eclipse 的git插件clone github上的远程项目(C++)到本地时遇到一个问题:clone下来的项目没有C++特性,无法使用open declaration等操作,下面是 ...
- [AX]AX2012 Interaction class
Ax2012 Client的form如果属性FormTemplate设置为DetailsPage或者ListPage,则必须同时设置属性InteractionClass为相应的Interaction类 ...
- C 修改命令行文本颜色
#include <Windows.h> #include <stdio.h> int main() { HANDLE h = GetStdHandle(STD_OUTPUT_ ...
- iOS UTI(统一类型标识)
同一类型标识符(Uniform Type Identifier,UTI)代表IOS信息共享的中心组件.可以把它看成下一代的MIME类型.UTI是标识资源类型(比如图像和文本)的字符串,他们制定哪些类型 ...
- Netty权威指南之BIO(Block Input/Output,同步阻塞I/O通信)通信模型
网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请求,通过三次握手建 ...
- 【Android】ProgressBar
http://www.cnblogs.com/wangying222/p/5304990.html http://www.cnblogs.com/plokmju/p/android_ProgressB ...
- 虚拟机中多个Linux系统之间配置免秘钥访问
1.三个节点cdh1,cdh2,cdh3 2.在每个机器上分别生产公钥对 ssh-keygen -t rsa 3.复制公钥 cd .ssh cat id_rsa.pub >> auth ...
- kafka进程总是在启动一段时间后自动停止
解决办法: bin/kafka-server-start.sh -daemon ./config/server.properties 进行启动,到现在为止 kafka 还在正常运行.和不加 -daem ...