不用你可以用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 publicclass 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 publicstaticvoid 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 =newbyte[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 publicstaticvoid 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 =newbyte[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 注意:程序的压缩过的文件,要通过系统上的工具解压出来的路径会相当多,因其在压缩时保留了原来的绝对路径,但压缩的文件中只包含所压缩的目标文件,当用程序解压出来的文件是相对的文件路径。
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 publicclass 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 publicstaticvoid 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 =newbyte[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 publicstaticvoid 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 =newbyte[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 注意:程序的压缩过的文件,要通过系统上的工具解压出来的路径会相当多,因其在压缩时保留了原来的绝对路径,但压缩的文件中只包含所压缩的目标文件,当用程序解压出来的文件是相对的文件路径。