zip (ICSharpCode.SharpZipLib.dll文件需要下载)

ZipClass zc=new ZipClass ();
zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩
zc.UnZip(@"E:\1\新建文件夹.zip",@"E:\1\2222");//解压

  cs

 1 class ZipClass
 2 {
 3 public void UnZip(string zipFilePath, string unZipDir)
 4 {
 5 if (zipFilePath == string.Empty)
 6 {
 7 throw new Exception("压缩文件不能为空!");
 8 }
 9 if (!File.Exists(zipFilePath))
10 {
11 throw new System.IO.FileNotFoundException("压缩文件不存在!");
12 }
13 //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 
14 if (unZipDir == string.Empty)
15 unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
16 if (!unZipDir.EndsWith("//"))
17 unZipDir += "//";
18 if (!Directory.Exists(unZipDir))
19 Directory.CreateDirectory(unZipDir);
20 
21 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
22 {
23 
24 ZipEntry theEntry;
25 while ((theEntry = s.GetNextEntry()) != null)
26 {
27 string directoryName = Path.GetDirectoryName(theEntry.Name);
28 string fileName = Path.GetFileName(theEntry.Name);
29 if (directoryName.Length > 0)
30 {
31 Directory.CreateDirectory(unZipDir + directoryName);
32 }
33 if (!directoryName.EndsWith("//"))
34 directoryName += "//";
35 if (fileName != String.Empty)
36 {
37 using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
38 {
39 
40 int size = 2048;
41 byte[] data = new byte[2048];
42 while (true)
43 {
44 size = s.Read(data, 0, data.Length);
45 if (size > 0)
46 {
47 streamWriter.Write(data, 0, size);
48 }
49 else
50 {
51 break;
52 }
53 }
54 }
55 }
56 }
57 }
58 }
59 
60  
61 
62 public static void ZipDir(string sDir, string sZip, ZipOutputStream s)
63 {
64 string[] filenames = Directory.GetFiles(sDir);
65 string[] dirnames = Directory.GetDirectories(sDir);
66 Crc32 crc = new Crc32();
67 if (s == null)
68 {
69 s = new ZipOutputStream(File.Create(sZip));
70 s.SetLevel(6); // 0 - store only to 9 - means best compression
71 }
72 
73 
74 foreach (string file in filenames)
75 fileZipInStream(file, s);
76 
77 foreach (string dir in dirnames)
78 ZipDir(dir, "", s);
79 if (sZip != "")
80 {
81 s.Finish();
82 s.Close();
83 
84 FileInfo fInfo = new FileInfo(sZip);
85 long size = fInfo.Length;
86 //Log.WriteLogD("----------------" + size.ToString());
87 if (size < 100) 
88 File.Delete(sZip);
89 }
90 }
91 
92 }
View Code

ICSharpCode.SharpZipLib.dll下载地址:http://i.cnblogs.com/Files.aspx

感谢提供此类的朋友

转载于:https://www.cnblogs.com/yuanjiehot/p/4320976.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/406243.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

堆与栈的关系与区别

From: http://blog.csdn.net/yfkiss/article/details/6542894 一. 在c中分为这几个存储区 1.栈 - 由编译器自动分配释放 2.堆 - 一般由程序员分配释放&#xff0c;若程序员不释放&#xff0c;程序结束时可能由OS回收 3.全局区&#xff08;静态区&#xff09;&#xff0c;全局…

mysql5.7 新建远程用户_Centos7中 mysql5.7 用户 创建 、授权、远程登录

1、添加用户跟以往版本不同&#xff0c;MySQL5.7 mysql.user表没有password字段&#xff0c;这个字段改成了 authentication_string&#xff1b;这里我们使用命令进行创建用户&#xff1a;CREATE USER usernamehost IDENTIFIED BY password;如创建一个test用户&#xff0c;密码…

宏定义#define

#include<stdio.h> #define exchange(a,b){\int t;\ta;\ab;\bt;\}int main(int arge,char *argv){ int a2,b3; printf("交换前a%d,b%d",a,b); exchange(a,b); printf("交换后a%d,b%d",a,b); return 0;}转载于:https://www.cnblogs.com/zhaoheng/p/43…

mysql ndb 关闭_Mysql NDB 常见问题

在安装和使用mysql cluster集群的过程中&#xff0c;遇到了很多问题&#xff0c;现在把那些问题列出来&#xff0c;并复上解决方法&#xff0c;希望大家在以后的使用中能少花时间少走弯路1、导入数据的时候遇到如下报错&#xff1a;ERROR 1005 (HY000) at line 25: Cant create…

红黑树实现——STL中的map

From: http://blog.csdn.net/zhongjiekangping/article/details/6934571 红黑树实现——STL中的map [ 2009-07-24 13:55:31 | 作者: dklkt ] 字号: 大 | 中 | 小 [转载]STL中map实现技术来源&#xff1a; http://blog.csdn.net/Fandywang_jlu/archive/2008/03/23/2208363.aspx…

Oracle实例和Oracle数据库(Oracle体系结构)

-- --Oracle实例和Oracle数据库(Oracle体系结构) -- /* 对于初接触Oracle 数据库的人来讲&#xff0c;很容易混淆的两个概念即是Oracle 实例和Oracle 数据库。这两 概念不同于SQL sever下的实例与数据库&#xff0c;当然也有些相似之处。只是在SQL server我们根本不需要花费太 …

vue elementui 实现图片上传后拖拽排序功能

vue elementui 实现图片上传后拖拽排序功能

Mysql基础2

清空一个表&#xff0c;自增id从1开始truncate table 表名; 查询 select 列名 from 表名 where 条件 order by 列名 [desc|asc] limit 跳过条数,查多少条 AS 别名 列名 as 新列名 注意as可以省掉不写 NULL值查询 select * from table1 where 字段 is null; 组合列 select conca…

C++ 类访问控制(public/protected/private)

From: http://cnmtjp.blog.51cto.com/204390/36548 C中 public&#xff0c;protected&#xff0c; private 访问标号小结第一&#xff1a;private, public, protected 访问标号的访问范围。private&#xff1a;只能由1.该类中的函数、2.其友元函数访问。不能被任何其他访问&…

vue监听浏览器原生返回按钮,进行路由转跳

vue监听浏览器原生返回按钮&#xff0c;进行路由转跳

WORD关于论文格式设置

现今的论文格式要求越来越无语,封面页 摘要页 目录页 正文页,封面页 摘要页 不能有页眉页脚 目录页 要求倒是不算高,整出个目录就行 正文页 页眉无所谓,但是页脚要显示页码 1 不设置就好,但是有时候设置了页眉又删除了的话,会出现一个横线,这个横线是由 页眉 的样式所定义的,这…

自身经历第二次面试(阿里)

自我介绍 1、哈希表及其实现机制 2、C的三个特征及其解释 3、抽象与接口的区别 4、设计模式 转载于:https://www.cnblogs.com/Vae1990Silence/p/4330577.html

秒杀系统 mysql_秒杀系统-介绍

秒杀系统秒杀系统其实是针对库存做的系统。用户成功秒杀商品&#xff0c;对于我们系统的操作就是减库存和记录用户的购买明细。用户的购买明细包括记录谁购买成功、购买成功的时间和付款信息。而对于减库存操作&#xff0c;需要考虑到以下两个问题&#xff1a;(1)若是用户成功秒…

MOSS中自定义WebService

(转http://tech.ddvip.com/2008-10/122527111786701.html&#xff0c;更详细见msdn&#xff1a;http://msdn.microsoft.com/zh-cn/library/ms464040.aspx) MOSS中已经提供的webservice都放在虚拟目录_vti_bin中&#xff0c;对应的物理目录为c:Program FilesCommon FilesMicroso…

C语言排序算法总结

From: http://www.diybl.com/course/6_system/linux/Linuxjs/20091028/180420.html 排序算法一直都是让我头疼的算法。为了全面掌握排序算法&#xff0c;我就整理了常用的排序算法。 首先我们来了解一些基本概念&#xff1a; &#xff08;1&#xff09;稳定排序和非稳定排序 简…