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 ...
随机推荐
- c++ int string互转
1. ){ monthNumber = "; itoa(i+,iCharArray,); monthNumber += iCharArray; } else { itoa(i+,iCharA ...
- 加密算法(扩展知识:Base64编码)
在某些考虑数据安全的场景下,我们常常会用到加密解密.编码解码知识.比如把用户密码保存到数据库上,常用的方式是通过MD5或SHA1不可逆算法进行加密后密文保存. 这里主要介绍三种常用的加密算法: (1) ...
- vue父组件中获取子组件中的数据
<FormItem label="上传头像" prop="image"> <uploadImg :width="150" ...
- redhat enterprise edition 6.8:禁止ipv6后,nfs文件系统无法挂载:no such device
如题:谨记. 附注:如何禁止ipv6? 方法一 第一种方法是通过 /etc/sysctl.conf 文件对 /proc 进行永久修改. 换句话说,就是用文本编辑器打开 /etc/sysctl.conf ...
- java用substring函数截取string中一段字符串
在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...
- Bootstrap——导航居中
这是采用了栅格,设置缩进,使看起来居中,但是手机浏览会靠到最左边.另外center-block类好像也不管用. <div class="row"> <ul cla ...
- mysql中如何修改表的名字?修改表名?
需求描述: 今天在进行mysql表的历史数据迁移,需要将某张表进行备份,修改表的名字,在此记录下操作过程. 操作过程: mysql> create table ts01 like ti_o_sm ...
- Dubbo调用链(version:2.5.3)
Consumer 调用 Provider的过程: (CONSUMER)Dubbo服务调用处 --> 调用RPC代理 --> InvokerInvocationHandler#invoke( ...
- 利用Visio绘制数据流图与组织结构图
绘制数据流图: 利用Visio 2007来绘制网上书店系统的数据流图.利用Visio 2007创建Gane- Sarson数据流图,可以选择“软件和数据库”模板,然后再选择“数据流模型图”,创建之后可 ...
- 使用CountDownLatch模拟高并发场景
import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java ...