首页 新闻 会员 周边

ASP.NET中怎么能同时下载多个文件或者一个文件夹?

0
悬赏园豆:20 [已解决问题] 解决于 2011-01-07 21:50

就是下载多个文件的时候直接是压缩包。一定要用线程吗?

我要精彩的主页 我要精彩 | 初学一级 | 园豆:130
提问于:2011-01-02 12:07
< >
分享
最佳答案
0

不用你可以用ICSharpCode.SharpZipLib.dll这个第三方组件进行压缩后下载

这个是压缩与解压缩的部分代码你参考下

代码
1 一、压缩文件
2
3  using System;
4 using ICSharpCode.SharpZipLib;
5 using ICSharpCode.SharpZipLib.Checksums;
6 using System.IO;
7 using ICSharpCode.SharpZipLib.Zip;
8 using System.Collections;
9
10 namespace wsUpFiles
11 {
12 /// <summary>
13 /// Common 的摘要说明。
14 /// </summary>
15 public class Common
16 {
17 public Common()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 /// <summary>
25 /// 压缩文件
26 /// </summary>
27 /// <param name="sourceFileNames">压缩文件名称集合</param>
28 /// <param name="destFileName">压缩后文件名称</param>
29 /// <param name="password">密码</param>
30 public static void zipFile(string path,string destFileName)
31 {
32 Crc32 crc = new Crc32();
33 ZipOutputStream s = new ZipOutputStream(File.Create(destFileName));
34 s.Password="";
35 s.SetLevel(6); // 0 - store only to 9 - means best compression
36 //定义
37 System.IO.DirectoryInfo myDir = new DirectoryInfo(path);
38 if(myDir.Exists == true)
39 {
40 System.IO.FileInfo[] myFileAry = myDir.GetFiles();
41
42 //循环提取文件夹下每一个文件,提取信息,
43 foreach (FileInfo objFiles in myFileAry)
44 {
45 FileStream fs = File.OpenRead(objFiles.FullName);
46 byte[] buffer = new byte[fs.Length];
47 fs.Read(buffer, 0, buffer.Length);
48 ZipEntry entry = new ZipEntry(objFiles.FullName);
49 entry.DateTime = DateTime.Now;
50 entry.Size = fs.Length;
51 fs.Close();
52 crc.Reset();
53 crc.Update(buffer);
54 entry.Crc = crc.Value;
55 s.PutNextEntry(entry);
56 s.Write(buffer, 0, buffer.Length);
57 }
58 s.Finish();
59 s.Close();
60 }
61 }
62
63 }
64 }
65
66 要对压缩文件加密时,要s.Password = "aaa"; aaa为密码。
67
68 二、解压文件
69
70 /// <summary>
71 /// 解压文件
72 /// </summary>
73 /// <param name="sourceFileName">被解压文件名称</param>
74 /// <param name="destPath">解压后文件目录</param>
75 /// <param name="password">密码</param>
76 public static void unzipFile(string sourceFileName,string destPath,string fileType)
77 {
78 ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFileName));
79 ZipEntry theEntry;
80 ArrayList al=new ArrayList();
81
82 while ((theEntry = s.GetNextEntry()) != null)
83 {
84 string fileName=Path.GetFileName(theEntry.Name);
85 if(fileName!="")
86 {
87 fileName=destPath+"\\"+fileName;
88 if(!Directory.Exists(destPath))
89 {
90 Directory.CreateDirectory(destPath);
91 }
92 FileStream streamWriter = File.Create(fileName);
93 int size = 2048;
94 byte[] data = new byte[2048];
95 s.Password="";
96 while (true)
97 {
98 size = s.Read(data, 0, data.Length);
99 if (size > 0)
100 {
101 streamWriter.Write(data, 0, size);
102 }
103 else
104 {
105 break;
106 }
107 }
108 streamWriter.Close();
109 }
110
111 }
112 s.Close();
113
114 }
115
116 注意:程序的压缩过的文件,要通过系统上的工具解压出来的路径会相当多,因其在压缩时保留了原来的绝对路径,但压缩的文件中只包含所压缩的目标文件,当用程序解压出来的文件是相对的文件路径。
收获园豆:20
Tear Y | 小虾三级 |园豆:784 | 2011-01-04 13:17
谢谢!
我要精彩 | 园豆:130 (初学一级) | 2011-01-07 21:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册