客户有时会有这样的需求:把服务器上的某个文件和文件夹下载到本地,我们的解决方法是使用在线压缩技术。就是把文件先在服务器上压缩,创建一个.ZIP,然后下载到本地。
网上提供了原代码,但是有些不足之处。第一:中文文件名的文件压缩之后,压缩包里的中文文件名都是乱码。不过我已经改了源文件,并且编译了一个新的dll,大家直接引用这个dll就可以了。有需要的给我要,我的邮件ligc@langchao.com.第二:只能往压缩包里方一个文件,而我写了一个可以放文件夹的函数AddFolder,给大家带来了极大的方便。
如有不足之处,请大家多多指出。
1 using System;
2 //File Reference
3 using System.IO;
4 //Zip Component Reference
5 using ICSharpCode.SharpZipLib.Checksums;
6 using ICSharpCode.SharpZipLib.Zip;
7 using ICSharpCode.SharpZipLib.Zip.Compression;
8 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
9
10 //test
11 using System.Web;
12
13 namespace ZipUnzip
14 {
15 /// <summary>
16 /// ZipUnzip 主要用于在线压缩和解压缩文件
17 /// </summary>
18 public class ZipUnzip
19 {
20 #region 定义私有变量
21 private Crc32 crc;
22 private ZipOutputStream s;
23 private string errorString;
24 #endregion
25
26 public ZipUnzip()
27 {
28 }
29
30 /// <summary>
31 /// 开始压缩并设置压缩文件(.zip)路径
32 /// </summary>
33 /// <param name="fileName">fileName</param>
34 public void BeginZip( string fileName )
35 {
36 crc = new Crc32();
37 s = new ZipOutputStream( File.Create( fileName ) );
38 }
39
40 /// <summary>
41 /// 结束压缩
42 /// </summary>
43 public void FinishZip()
44 {
45 s.Finish();
46 s.Close();
47 }
48
49 /// <summary>
50 /// 添加文件到压缩文件
51 /// </summary>
52 /// <param name="fileName"></param>
53 /// <param name="zipName"></param>
54 /// <returns>错误原因</returns>
55 public string AddZip( string fileName, string zipName )
56 {
57 try
58 {
59 FileStream fs = File.OpenRead( fileName );
60 byte[] buffer = new byte[fs.Length];
61 fs.Read( buffer, 0, buffer.Length );
62 fileName = Path.GetFileName( fileName );
63 long fileLength = fs.Length;
64 fs.Close();
65
66 ZipEntry entry = new ZipEntry( zipName );
67 entry.DateTime = DateTime.Now;
68 entry.Size = fileLength;
69
70 crc.Reset();
71 crc.Update( buffer );
72 entry.Crc = crc.Value;
73 s.PutNextEntry( entry );
74 s.Write( buffer, 0, buffer.Length );
75
76 return string.Empty;
77 }
78 catch( Exception addEx )
79 {
80 errorString = addEx.Message;
81 this.FinishZip();
82 return addEx.Message;
83 }
84 }
85
86 public void AddFolder( string folderName, string zipName )
87 {
88 if( Directory.Exists( folderName ) )
89 {
90 foreach( string str in Directory.GetFileSystemEntries( folderName ) )
91 {
92 if( File.Exists( str ) )
93 AddZip( str, zipName + "\\" + str.Substring( str.LastIndexOf( "\\" ) + 1 ) );
94 else
95 AddFolder( str, zipName + "\\" + str.Substring( str.LastIndexOf( "\\" ) + 1 ) );
96 }
97 }
98 }
99
100 /// <summary>
101 /// 获得错误原因
102 /// </summary>
103 /// <returns></returns>
104 public string GetErrorString()
105 {
106 return errorString;
107 }
108 }
109 }
110
111 Class Finish
112
113 以下为调用方法
114 using System;
115 using System.Collections;
116 using System.ComponentModel;
117 using System.Data;
118 using System.Drawing;
119 using System.Web;
120 using System.Web.SessionState;
121 using System.Web.UI;
122 using System.Web.UI.WebControls;
123 using System.Web.UI.HtmlControls;
124
125 using System.IO;
126
127 using ICSharpCode.SharpZipLib.Checksums;
128 using ICSharpCode.SharpZipLib.Zip;
129 using ICSharpCode.SharpZipLib.Zip.Compression;
130 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
131
132 namespace ZipUnzip
133 {
134 /// <summary>
135 /// WebForm1 的摘要说明。
136 /// </summary>
137 public class WebForm1 : System.Web.UI.Page
138 {
139 protected System.Web.UI.WebControls.TextBox TextBox1;
140 protected System.Web.UI.WebControls.TextBox TextBox2;
141 protected System.Web.UI.WebControls.Button Button2;
142
143 private void Page_Load(object sender, System.EventArgs e)
144 {
145 // 在此处放置用户代码以初始化页面
146 }
147
148 #region Web 窗体设计器生成的代码
149 override protected void OnInit(EventArgs e)
150 {
151 //
152 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
153 //
154 InitializeComponent();
155 base.OnInit(e);
156 }
157
158 /// <summary>
159 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
160 /// 此方法的内容。
161 /// </summary>
162 private void InitializeComponent()
163 {
164 this.Button2.Click += new System.EventHandler(this.Button2_Click);
165 this.Load += new System.EventHandler(this.Page_Load);
166
167 }
168 #endregion
169
170 private void Button1_Click(object sender, System.EventArgs e)
171 {
172
173 }
174
175 private void Button2_Click(object sender, System.EventArgs e)
176 {
177 ZipUnzip zuz = new ZipUnzip();
178 zuz.BeginZip( Server.MapPath( "." ) + "\\" + "Temp.zip" );
179 zuz.AddFolder( Server.MapPath( "." ) + "\\" + "C301-K", "C301-M" );
180 zuz.FinishZip();
181 }
182
183
184 }
185 }
186
187
2 //File Reference
3 using System.IO;
4 //Zip Component Reference
5 using ICSharpCode.SharpZipLib.Checksums;
6 using ICSharpCode.SharpZipLib.Zip;
7 using ICSharpCode.SharpZipLib.Zip.Compression;
8 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
9
10 //test
11 using System.Web;
12
13 namespace ZipUnzip
14 {
15 /// <summary>
16 /// ZipUnzip 主要用于在线压缩和解压缩文件
17 /// </summary>
18 public class ZipUnzip
19 {
20 #region 定义私有变量
21 private Crc32 crc;
22 private ZipOutputStream s;
23 private string errorString;
24 #endregion
25
26 public ZipUnzip()
27 {
28 }
29
30 /// <summary>
31 /// 开始压缩并设置压缩文件(.zip)路径
32 /// </summary>
33 /// <param name="fileName">fileName</param>
34 public void BeginZip( string fileName )
35 {
36 crc = new Crc32();
37 s = new ZipOutputStream( File.Create( fileName ) );
38 }
39
40 /// <summary>
41 /// 结束压缩
42 /// </summary>
43 public void FinishZip()
44 {
45 s.Finish();
46 s.Close();
47 }
48
49 /// <summary>
50 /// 添加文件到压缩文件
51 /// </summary>
52 /// <param name="fileName"></param>
53 /// <param name="zipName"></param>
54 /// <returns>错误原因</returns>
55 public string AddZip( string fileName, string zipName )
56 {
57 try
58 {
59 FileStream fs = File.OpenRead( fileName );
60 byte[] buffer = new byte[fs.Length];
61 fs.Read( buffer, 0, buffer.Length );
62 fileName = Path.GetFileName( fileName );
63 long fileLength = fs.Length;
64 fs.Close();
65
66 ZipEntry entry = new ZipEntry( zipName );
67 entry.DateTime = DateTime.Now;
68 entry.Size = fileLength;
69
70 crc.Reset();
71 crc.Update( buffer );
72 entry.Crc = crc.Value;
73 s.PutNextEntry( entry );
74 s.Write( buffer, 0, buffer.Length );
75
76 return string.Empty;
77 }
78 catch( Exception addEx )
79 {
80 errorString = addEx.Message;
81 this.FinishZip();
82 return addEx.Message;
83 }
84 }
85
86 public void AddFolder( string folderName, string zipName )
87 {
88 if( Directory.Exists( folderName ) )
89 {
90 foreach( string str in Directory.GetFileSystemEntries( folderName ) )
91 {
92 if( File.Exists( str ) )
93 AddZip( str, zipName + "\\" + str.Substring( str.LastIndexOf( "\\" ) + 1 ) );
94 else
95 AddFolder( str, zipName + "\\" + str.Substring( str.LastIndexOf( "\\" ) + 1 ) );
96 }
97 }
98 }
99
100 /// <summary>
101 /// 获得错误原因
102 /// </summary>
103 /// <returns></returns>
104 public string GetErrorString()
105 {
106 return errorString;
107 }
108 }
109 }
110
111 Class Finish
112
113 以下为调用方法
114 using System;
115 using System.Collections;
116 using System.ComponentModel;
117 using System.Data;
118 using System.Drawing;
119 using System.Web;
120 using System.Web.SessionState;
121 using System.Web.UI;
122 using System.Web.UI.WebControls;
123 using System.Web.UI.HtmlControls;
124
125 using System.IO;
126
127 using ICSharpCode.SharpZipLib.Checksums;
128 using ICSharpCode.SharpZipLib.Zip;
129 using ICSharpCode.SharpZipLib.Zip.Compression;
130 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
131
132 namespace ZipUnzip
133 {
134 /// <summary>
135 /// WebForm1 的摘要说明。
136 /// </summary>
137 public class WebForm1 : System.Web.UI.Page
138 {
139 protected System.Web.UI.WebControls.TextBox TextBox1;
140 protected System.Web.UI.WebControls.TextBox TextBox2;
141 protected System.Web.UI.WebControls.Button Button2;
142
143 private void Page_Load(object sender, System.EventArgs e)
144 {
145 // 在此处放置用户代码以初始化页面
146 }
147
148 #region Web 窗体设计器生成的代码
149 override protected void OnInit(EventArgs e)
150 {
151 //
152 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
153 //
154 InitializeComponent();
155 base.OnInit(e);
156 }
157
158 /// <summary>
159 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
160 /// 此方法的内容。
161 /// </summary>
162 private void InitializeComponent()
163 {
164 this.Button2.Click += new System.EventHandler(this.Button2_Click);
165 this.Load += new System.EventHandler(this.Page_Load);
166
167 }
168 #endregion
169
170 private void Button1_Click(object sender, System.EventArgs e)
171 {
172
173 }
174
175 private void Button2_Click(object sender, System.EventArgs e)
176 {
177 ZipUnzip zuz = new ZipUnzip();
178 zuz.BeginZip( Server.MapPath( "." ) + "\\" + "Temp.zip" );
179 zuz.AddFolder( Server.MapPath( "." ) + "\\" + "C301-K", "C301-M" );
180 zuz.FinishZip();
181 }
182
183
184 }
185 }
186
187
由于时间仓促,所以没有时间整理,日后一定理顺程序。
浙公网安备 33010602011771号