ASP.NET中利用ICSharpCode.SharpZipLib压缩多个上传附件成一个压缩包直接保存在数据库中
项目需要,研究了一下ICSharpCode.SharpZipLib,完成了标题需求,希望对大家有帮助,不废话了,代码……
以下是上传按钮事件(本次上传2个文件)
UploadClick
1 Upload#region Upload
2 private void btnUpload_Click(object sender, System.EventArgs e)
3 {
4 Get Files#region Get Files
5 string[] strFiles = new string[2];
6
7 strFiles[0] = tbxUpload1.PostedFile.FileName;
8 strFiles[1] = tbxUpload2.PostedFile.FileName;
9
10 #endregion
11
12 string strFileDirectory = strCurrentHuman + DateTime.Now.ToShortDateString();
13
14 //待压缩的目录
15 string strPhysicalPath = Server.MapPath("/") + strFileDirectory;
16
17 if(!Directory.Exists(strPhysicalPath))
18 {
19 Directory.CreateDirectory(strPhysicalPath);
20 }
21
22 //文件路径
23 string[] strFilePaths = new string[strFiles.Length];
24
25 //Set ZIPFile Name And Path
26 string strZipFileName = strPhysicalPath + ".zip";
27
28 MemoryStream oMemoryStream = new MemoryStream();
29
30// ZipOutputStream oZipStream = new ZipOutputStream(File.Create(strZipFileName));
31 ZipOutputStream oZipStream = new ZipOutputStream(oMemoryStream);
32
33 try
34 {
35
36 for(int i=0; i<=strFiles.Length-1; i++)
37 {
38 Read File Data To Stream#region Read File Data To Stream
39 FileStream oReadFileStream = File.OpenRead(strFiles[i]);
40
41 byte[] btFile = new byte[oReadFileStream.Length];
42
43 oReadFileStream.Read(btFile, 0, btFile.Length);
44 #endregion
45
46 string strCurrentFileName = Path.GetFileName(strFiles[i]);
47
48 strFilePaths[i] = strPhysicalPath + "/" + strCurrentFileName;
49
50 /**//*
51 #region Write File Data To Local
52 FileStream oFileStream = new FileStream(strFilePaths[i], FileMode.CreateNew, FileAccess.Write);
53
54 oFileStream.Write(btFile, 0, btFile.Length);
55
56 oFileStream.Close();
57 #endregion
58 */
59
60 ZipEntry oZipEntry = new ZipEntry(strCurrentFileName);
61
62 oZipEntry.DateTime = DateTime.Now;
63 oZipEntry.Size = oReadFileStream.Length;
64
65 Crc32 oCrc32 = new Crc32();
66 oCrc32.Reset();
67 oCrc32.Update(btFile);
68
69
70 oZipEntry.Crc = oCrc32.Value;
71
72 oZipStream.PutNextEntry(oZipEntry);
73 oZipStream.Write(btFile, 0, btFile.Length);
74
75 //Stream Close
76 oReadFileStream.Close();
77 }
78 }
79 catch(Exception ex)
80 {
81 throw ex;
82 }
83 finally
84 {
85 oZipStream.Finish();
86
87 oZipStream.Close();
88 }
89
90 FileEntity oFileEntity = new FileEntity();
91
92 oFileEntity.strFileName = Path.GetFileNameWithoutExtension(strZipFileName);
93 oFileEntity.strFileExtName = Path.GetExtension(strZipFileName);
94
95 oFileEntity.gFile = oMemoryStream.GetBuffer();
96
97 this.InsertData(oFileEntity);
98
99 Response.Write("Success");
100
101 }
102 #endregion
1 Upload#region Upload
2 private void btnUpload_Click(object sender, System.EventArgs e)
3 {
4 Get Files#region Get Files
5 string[] strFiles = new string[2];
6
7 strFiles[0] = tbxUpload1.PostedFile.FileName;
8 strFiles[1] = tbxUpload2.PostedFile.FileName;
9
10 #endregion
11
12 string strFileDirectory = strCurrentHuman + DateTime.Now.ToShortDateString();
13
14 //待压缩的目录
15 string strPhysicalPath = Server.MapPath("/") + strFileDirectory;
16
17 if(!Directory.Exists(strPhysicalPath))
18 {
19 Directory.CreateDirectory(strPhysicalPath);
20 }
21
22 //文件路径
23 string[] strFilePaths = new string[strFiles.Length];
24
25 //Set ZIPFile Name And Path
26 string strZipFileName = strPhysicalPath + ".zip";
27
28 MemoryStream oMemoryStream = new MemoryStream();
29
30// ZipOutputStream oZipStream = new ZipOutputStream(File.Create(strZipFileName));
31 ZipOutputStream oZipStream = new ZipOutputStream(oMemoryStream);
32
33 try
34 {
35
36 for(int i=0; i<=strFiles.Length-1; i++)
37 {
38 Read File Data To Stream#region Read File Data To Stream
39 FileStream oReadFileStream = File.OpenRead(strFiles[i]);
40
41 byte[] btFile = new byte[oReadFileStream.Length];
42
43 oReadFileStream.Read(btFile, 0, btFile.Length);
44 #endregion
45
46 string strCurrentFileName = Path.GetFileName(strFiles[i]);
47
48 strFilePaths[i] = strPhysicalPath + "/" + strCurrentFileName;
49
50 /**//*
51 #region Write File Data To Local
52 FileStream oFileStream = new FileStream(strFilePaths[i], FileMode.CreateNew, FileAccess.Write);
53
54 oFileStream.Write(btFile, 0, btFile.Length);
55
56 oFileStream.Close();
57 #endregion
58 */
59
60 ZipEntry oZipEntry = new ZipEntry(strCurrentFileName);
61
62 oZipEntry.DateTime = DateTime.Now;
63 oZipEntry.Size = oReadFileStream.Length;
64
65 Crc32 oCrc32 = new Crc32();
66 oCrc32.Reset();
67 oCrc32.Update(btFile);
68
69
70 oZipEntry.Crc = oCrc32.Value;
71
72 oZipStream.PutNextEntry(oZipEntry);
73 oZipStream.Write(btFile, 0, btFile.Length);
74
75 //Stream Close
76 oReadFileStream.Close();
77 }
78 }
79 catch(Exception ex)
80 {
81 throw ex;
82 }
83 finally
84 {
85 oZipStream.Finish();
86
87 oZipStream.Close();
88 }
89
90 FileEntity oFileEntity = new FileEntity();
91
92 oFileEntity.strFileName = Path.GetFileNameWithoutExtension(strZipFileName);
93 oFileEntity.strFileExtName = Path.GetExtension(strZipFileName);
94
95 oFileEntity.gFile = oMemoryStream.GetBuffer();
96
97 this.InsertData(oFileEntity);
98
99 Response.Write("Success");
100
101 }
102 #endregion
以下是数据库插入方法
InsertData
1 private void InsertData(FileEntity oFile)
2 {
3 IDbConnection conn = GetConnection();
4
5 IDbCommand cmd = conn.CreateCommand();
6
7 string strSQL = "insert into tFiles(gFile,cFileName,cFileExtName) values(@gFile,@cFileName,@cFileExtName)";
8
9 cmd.CommandText = strSQL;
10
11
12 SqlParameter[] pParams = new SqlParameter[3];
13 pParams[0] = new SqlParameter("@gFile", oFile.gFile);
14 pParams[1] = new SqlParameter("@cFileName", oFile.strFileName);
15 pParams[2] = new SqlParameter("@cFileExtName", oFile.strFileExtName);
16
17 foreach(SqlParameter param in pParams)
18 {
19 cmd.Parameters.Add(param);
20 }
21
22 try
23 {
24 conn.Open();
25
26 cmd.ExecuteNonQuery();
27 }
28 catch(Exception ex)
29 {
30 throw ex;
31 }
32 finally
33 {
34 cmd.Dispose();
35 conn.Close();
36 conn.Dispose();
37 }
38 }
39
1 private void InsertData(FileEntity oFile)
2 {
3 IDbConnection conn = GetConnection();
4
5 IDbCommand cmd = conn.CreateCommand();
6
7 string strSQL = "insert into tFiles(gFile,cFileName,cFileExtName) values(@gFile,@cFileName,@cFileExtName)";
8
9 cmd.CommandText = strSQL;
10
11
12 SqlParameter[] pParams = new SqlParameter[3];
13 pParams[0] = new SqlParameter("@gFile", oFile.gFile);
14 pParams[1] = new SqlParameter("@cFileName", oFile.strFileName);
15 pParams[2] = new SqlParameter("@cFileExtName", oFile.strFileExtName);
16
17 foreach(SqlParameter param in pParams)
18 {
19 cmd.Parameters.Add(param);
20 }
21
22 try
23 {
24 conn.Open();
25
26 cmd.ExecuteNonQuery();
27 }
28 catch(Exception ex)
29 {
30 throw ex;
31 }
32 finally
33 {
34 cmd.Dispose();
35 conn.Close();
36 conn.Dispose();
37 }
38 }
39
为方便大家研究,代码下载如下: