Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
(一). 实现功能
对文件及目录的压缩及解压功能
(二). 运行图片示例
(三).代码
1. 压缩类
1/**////<summary>
2///压缩类
3///</summary>4publicclassZipClass
5{
6publicstaticvoidZipFile(stringFileToZip,stringZipedFile,intCompressionLevel,intBlockSize)
7{
8//如果文件没有找到,则报错
9if(!System.IO.File.Exists(FileToZip))
10{
11thrownewSystem.IO.FileNotFoundException("指定要压缩的文件:"+FileToZip+"不存在!");
12}
13
14System.IO.FileStreamStreamToZip=newSystem.IO.FileStream(FileToZip,System.IO.FileMode.Open,System.IO.FileAccess.Read);
15System.IO.FileStreamZipFile=System.IO.File.Create(ZipedFile);
16ZipOutputStreamZipStream=newZipOutputStream(ZipFile);
17ZipEntryZipEntry=newZipEntry("ZippedFile");
18ZipStream.PutNextEntry(ZipEntry);
19ZipStream.SetLevel(CompressionLevel);
20byte[]buffer=newbyte[BlockSize];
21System.Int32size=StreamToZip.Read(buffer,0,buffer.Length);
22ZipStream.Write(buffer,0,size);
23try
24{
25while(size<StreamToZip.Length)
26{
27intsizeRead=StreamToZip.Read(buffer,0,buffer.Length);
28ZipStream.Write(buffer,0,sizeRead);
29size+=sizeRead;
30}
31}
32catch(System.Exceptionex)
33{
34throwex;
35}
36ZipStream.Finish();
37ZipStream.Close();
38StreamToZip.Close();
39}
40
41/**////<summary>
42///压缩目录
43///</summary>
44///<paramname="args">数组(数组[0]:要压缩的目录;数组[1]:压缩的文件名)</param>
45publicstaticvoidZipFileDictory(string[]args)
46{
47string[]filenames=Directory.GetFiles(args[0]);
48
49Crc32crc=newCrc32();
50ZipOutputStreams=newZipOutputStream(File.Create(args[1]));
51s.SetLevel(6);
52foreach(stringfileinfilenames)
53{
54//打开压缩文件
55FileStreamfs=File.OpenRead(file);
56
57byte[]buffer=newbyte[fs.Length];
58fs.Read(buffer,0,buffer.Length);
59ZipEntryentry=newZipEntry(file);
60
61entry.DateTime=DateTime.Now;
62
63entry.Size=fs.Length;
64fs.Close();
65
66crc.Reset();
67crc.Update(buffer);
68
69entry.Crc=crc.Value;
70
71s.PutNextEntry(entry);
72
73s.Write(buffer,0,buffer.Length);
74
75}
76
77s.Finish();
78s.Close();
79}
80
81/**////<summary>
82///压缩文件
83///</summary>
84///<paramname="FileToZip">要进行压缩的文件名</param>
85///<paramname="ZipedFile">压缩后生成的压缩文件名</param>
86publicstaticvoidZipFile(stringFileToZip,stringZipedFile)
87{
88//如果文件没有找到,则报错
89if(!File.Exists(FileToZip))
90{
91thrownewSystem.IO.FileNotFoundException("指定要压缩的文件:"+FileToZip+"不存在!");
92}
93FileStreamfs=File.OpenRead(FileToZip);
94byte[]buffer=newbyte[fs.Length];
95fs.Read(buffer,0,buffer.Length);
96fs.Close();
97
98FileStreamZipFile=File.Create(ZipedFile);
99ZipOutputStreamZipStream=newZipOutputStream(ZipFile);
100ZipEntryZipEntry=newZipEntry("ZippedFile");
101ZipStream.PutNextEntry(ZipEntry);
102ZipStream.SetLevel(6);
103
104ZipStream.Write(buffer,0,buffer.Length);
105ZipStream.Finish();
106ZipStream.Close();
107}
108}
109
110/**////<summary>
111///解压类
112///</summary>113publicclassUnZipClass
114{
115/**////<summary>
116///解压功能(解压压缩文件到指定目录)
117///</summary>
118///<paramname="args">待解压的文件</param>
119///<paramname="args">指定解压目标目录</param>
120publicstaticvoidUnZip(string[]args)
121{
122ZipInputStreams=newZipInputStream(File.OpenRead(@args[0].Trim()));
123ZipEntrytheEntry;
124stringdirectoryName=Path.GetDirectoryName(@args[1].Trim());
125
126if(!Directory.Exists(@args[1].Trim()))
127{
128Directory.CreateDirectory(directoryName);
129}
130while((theEntry=s.GetNextEntry())!=null)
131{
132;
133stringfileName=Path.GetFileName(theEntry.Name);
134
135if(fileName!=String.Empty)
136{
137FileStreamstreamWriter=File.Create(@args[1].Trim()+fileName);
138
139intsize=2048;
140byte[]data=newbyte[2048];
141while(true)
142{
143size=s.Read(data,0,data.Length);
144if(size>0)
145{
146streamWriter.Write(data,0,size);
147}
148else
149{
150break;
151}
152}
153
154streamWriter.Close();
155}
156}
157s.Close();
158}
159
2. 前台页面代码
Global site tag (gtag.js) - Google Analytics
相关推荐
基于ASP.NET2.0的在线压缩-解压源程序代码,实现实现对文档资源的在线压缩与解压操作,采用VS2005+C#+ASP.NET开发。
不要再去花钱买了,对了,我是几乎最大化压缩的,是六个压缩包。废话不言多,事实去证明。好的话,一定要给我做评论,这样就不会误了别人了。要把六个解压包全下载才能解压好!其他的几个压缩包就不要分了。不然就要...
1.2.2 ASP.NET2.0 1.2.3 ASP.NET3.5 1.2.4 ASP.NET4 1.2.5 Silverlight 1.3 总结 第2章 Visual Studio 2.1 Visual Studio 2.1.1 网站和Web项目 2.1.2 创建无项目文件的网站 2.1.3 设计网页 2.2...
文件和目录可进行压缩解压ZIP操作,可以批量上传及批量下载文件。 10.文件提取功能,可同时定义多个文件的提取码,提取码可个性化定义,并可限制下载次数及天数。 11.上传码功能,可定义一个目录的外部允许...
X3-BLOG 是基于XML+XSLT+AJAX技术构建的开源多用户博客门户系统,服务器端采用当前最流行的动态网页开发语言之一ASP.NET(C#) 2.0编写,支持多种数据库,包括SQLSERVER2000\SQLSERVER2005\ORACLE\MYSQL\DB2\ACCESS等...
《VSCMS内容管理系统》是国内CMS市场的新秀、也是国内少有的采用微软的ASP.NET 2.0 + SQL2005 技术框架开发的CMS,充分利用ASP.NET架构的优势,突破传统ASP类CMS的局限性,采用更稳定执行速度更高效的面向对象语言C#...
关于 X3-BLOG <br> X3-BLOG 是基于XML+XSLT+AJAX技术构建的开源多用户博客门户系统,服务器端采用当前最流行的动态网页开发语言ASP.NET(C#) <br>2.0编写,支持多种数据库,包括SQLSERVER2000\SQLSERVER2005\...
下面结合一个ASP.NET2.0的项目来具体看看FCKeditor的安装、配置、使用。在开始之前请先下载FCKeditor文件包和FCKeditor.Net 服务器控件。启用VWD2005新建一个C#的WEB Site工程,取名FCKPro。 FCKeditor安装: 所谓...
111 <br>0184 如何在ASP.NET中获取文件的扩展名 111 <br>0185 如何在ASP.NET中用URL在页面之间传值 112 <br>0186 如何使用IsPostBack实现ASP.NET页面加载 112 <br>0187 如何利用输出缓存技术缓存...
中诺企业网站系统(zncms.cn)是中诺科技开发的...注:1)网站空间需支持ASP.NET3.5及以上版本 2)数据库需要MSSQL2005及以上版本 3)上传时如果最好将Upload内的文件压缩后再上传,然后在线解压,以节省上传时间