文件下載與ZIP打包和解壓縮
前段時間在文件下載時遇到點問題, 後來發現網絡上對ZIP包的論題很多, 特將經驗分享下.
文件下載:
對於多個文件同時下載, 一般通過先打包成一個文件,到客戶端再解壓縮的方式實現.
打包操作:
解壓縮操作:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
感謝ZCC在文件下載過程中對我的指導.
文件下載:
private void DownLoadFileByPath(string strFileFullPath)
{
string strFileName = strFileFullPath.Substring(strFileFullPath.LastIndexOf("\\") + 1);
int dot = strFileName.LastIndexOf(".");
string strFileType = string.Empty;
string strContentType = string.Empty;
if (dot != -1 && strFileName.Length > dot + 1)
{
strFileType = strFileName.Substring(dot + 1).ToUpper();
}
if (strFileType.ToUpper() == "PDF")
{
strContentType = "application/pdf";
}
else if (strFileType.ToUpper() == "TIF")
{
strContentType = "image/tiff";
}
else if (strFileType.ToUpper() == "DOC")
{
strContentType = "application/msword";
}
else if (strFileType.ToUpper() == "xls")
{
strContentType = "application/vnd.ms-excel";
}
Response.Clear();
Response.ContentType = strContentType;
Response.BufferOutput = false;
Response.AppendHeader("content-disposition", "attachment;filename=" + strFileName);
Response.WriteFile(strFileFullPath);
Response.Flush();
System.IO.File.Delete(strFileFullPath);
Response.Close();
}
{
string strFileName = strFileFullPath.Substring(strFileFullPath.LastIndexOf("\\") + 1);
int dot = strFileName.LastIndexOf(".");
string strFileType = string.Empty;
string strContentType = string.Empty;
if (dot != -1 && strFileName.Length > dot + 1)
{
strFileType = strFileName.Substring(dot + 1).ToUpper();
}
if (strFileType.ToUpper() == "PDF")
{
strContentType = "application/pdf";
}
else if (strFileType.ToUpper() == "TIF")
{
strContentType = "image/tiff";
}
else if (strFileType.ToUpper() == "DOC")
{
strContentType = "application/msword";
}
else if (strFileType.ToUpper() == "xls")
{
strContentType = "application/vnd.ms-excel";
}
Response.Clear();
Response.ContentType = strContentType;
Response.BufferOutput = false;
Response.AppendHeader("content-disposition", "attachment;filename=" + strFileName);
Response.WriteFile(strFileFullPath);
Response.Flush();
System.IO.File.Delete(strFileFullPath);
Response.Close();
}
對於多個文件同時下載, 一般通過先打包成一個文件,到客戶端再解壓縮的方式實現.
打包操作:
1 public static void ZipDir(string pFoldToZip, string pZipedFileFullName)
2 {
3 string sOldCurDir = Directory.GetCurrentDirectory();
4 string sCurDir = pFoldToZip;//.Substring(0,pFoldToZip.LastIndexOf("\\"));
5 if (!Directory.Exists(sCurDir))
6 Directory.CreateDirectory(sCurDir);
7 Directory.SetCurrentDirectory(sCurDir);
8 string sCurZipFold = pFoldToZip;
9 string[] filenames = Directory.GetFiles(sCurZipFold);//Directory.GetFiles(args[0]);
10 string[] filefolds = Directory.GetDirectories(sCurZipFold);//Directory.GetFiles(args[0]);
11
12
13 Crc32 crc = new Crc32();
14 ZipOutputStream s = new ZipOutputStream(File.Create(pZipedFileFullName)); //File.Create(args[1]));
15 s.SetLevel(6); // 0 - store only to 9 - means best compression
16 while (true)
17 {
18 if ((filenames == null || filenames.Length <= 0) && (filefolds == null || filefolds.Length <= 0)) break;
19 foreach (string file in filenames)
20 {
21 if (file.Trim() == "") continue;
22 //string sFileXiangDui = file.Replace(sCurDir,".");
23 string sFileXiangDui = file.Replace(sCurDir + "\\", "");
24 FileStream fs = File.OpenRead(sFileXiangDui);
25
26 byte[] buffer = new byte[fs.Length];
27 fs.Read(buffer, 0, buffer.Length);
28 ZipEntry entry = new ZipEntry(sFileXiangDui);
29
30 entry.DateTime = DateTime.Now;
31
32 // set Size and the crc, because the information
33 // about the size and crc should be stored in the header
34 // if it is not set it is automatically written in the footer.
35 // (in this case size == crc == -1 in the header)
36 // Some ZIP programs have problems with zip files that don't store
37 // the size and crc in the header.
38 entry.Size = fs.Length;
39 fs.Close();
40
41 crc.Reset();
42 crc.Update(buffer);
43
44 entry.Crc = crc.Value;
45
46 s.PutNextEntry(entry);
47
48 s.Write(buffer, 0, buffer.Length);
49
50 }
51
52 if (filefolds != null && filefolds.Length > 0)
53 {
54 //filenames = GetFiles(filefolds);
55 //filefolds = GetSubFolds(filefolds);
56 }
57 else
58 {
59 filenames = null;
60 filefolds = null;
61 }
62 }
63 s.Finish();
64 s.Close();
65 Directory.SetCurrentDirectory(sOldCurDir);
66 }
67
2 {
3 string sOldCurDir = Directory.GetCurrentDirectory();
4 string sCurDir = pFoldToZip;//.Substring(0,pFoldToZip.LastIndexOf("\\"));
5 if (!Directory.Exists(sCurDir))
6 Directory.CreateDirectory(sCurDir);
7 Directory.SetCurrentDirectory(sCurDir);
8 string sCurZipFold = pFoldToZip;
9 string[] filenames = Directory.GetFiles(sCurZipFold);//Directory.GetFiles(args[0]);
10 string[] filefolds = Directory.GetDirectories(sCurZipFold);//Directory.GetFiles(args[0]);
11
12
13 Crc32 crc = new Crc32();
14 ZipOutputStream s = new ZipOutputStream(File.Create(pZipedFileFullName)); //File.Create(args[1]));
15 s.SetLevel(6); // 0 - store only to 9 - means best compression
16 while (true)
17 {
18 if ((filenames == null || filenames.Length <= 0) && (filefolds == null || filefolds.Length <= 0)) break;
19 foreach (string file in filenames)
20 {
21 if (file.Trim() == "") continue;
22 //string sFileXiangDui = file.Replace(sCurDir,".");
23 string sFileXiangDui = file.Replace(sCurDir + "\\", "");
24 FileStream fs = File.OpenRead(sFileXiangDui);
25
26 byte[] buffer = new byte[fs.Length];
27 fs.Read(buffer, 0, buffer.Length);
28 ZipEntry entry = new ZipEntry(sFileXiangDui);
29
30 entry.DateTime = DateTime.Now;
31
32 // set Size and the crc, because the information
33 // about the size and crc should be stored in the header
34 // if it is not set it is automatically written in the footer.
35 // (in this case size == crc == -1 in the header)
36 // Some ZIP programs have problems with zip files that don't store
37 // the size and crc in the header.
38 entry.Size = fs.Length;
39 fs.Close();
40
41 crc.Reset();
42 crc.Update(buffer);
43
44 entry.Crc = crc.Value;
45
46 s.PutNextEntry(entry);
47
48 s.Write(buffer, 0, buffer.Length);
49
50 }
51
52 if (filefolds != null && filefolds.Length > 0)
53 {
54 //filenames = GetFiles(filefolds);
55 //filefolds = GetSubFolds(filefolds);
56 }
57 else
58 {
59 filenames = null;
60 filefolds = null;
61 }
62 }
63 s.Finish();
64 s.Close();
65 Directory.SetCurrentDirectory(sOldCurDir);
66 }
67
解壓縮操作:
1 public static void UnZip(string pCurDir, string pZipedFileName)
2 {
3 if (!Directory.Exists(pCurDir))
4 {
5 Directory.CreateDirectory(pCurDir);
6 }
7
8 string strOriCurrentDir = Directory.GetCurrentDirectory();
9 Directory.SetCurrentDirectory(pCurDir);
10 ZipInputStream s = new ZipInputStream(File.OpenRead(pZipedFileName));
11
12 ZipEntry theEntry;
13 while ((theEntry = s.GetNextEntry()) != null)
14 {
15
16 Console.WriteLine(theEntry.Name);
17
18 string directoryName = Path.GetDirectoryName(theEntry.Name);
19 string fileName = Path.GetFileName(theEntry.Name);
20
21 // create directory
22 if (directoryName.Trim() != "" && !Directory.Exists(directoryName))
23 Directory.CreateDirectory(directoryName);
24
25 if (fileName != String.Empty)
26 {
27 FileStream streamWriter = File.Create(theEntry.Name);
28
29 int size = 2048;
30 byte[] data = new byte[2048];
31 while (true)
32 {
33 size = s.Read(data, 0, data.Length);
34 if (size > 0)
35 {
36 streamWriter.Write(data, 0, size);
37 }
38 else
39 {
40 break;
41 }
42 }
43
44 streamWriter.Close();
45 }
46 }
47 s.Close();
48
49 Directory.SetCurrentDirectory(strOriCurrentDir);
50 }
這裡使用到組件ICSharpCode.SharpZipLib.2 {
3 if (!Directory.Exists(pCurDir))
4 {
5 Directory.CreateDirectory(pCurDir);
6 }
7
8 string strOriCurrentDir = Directory.GetCurrentDirectory();
9 Directory.SetCurrentDirectory(pCurDir);
10 ZipInputStream s = new ZipInputStream(File.OpenRead(pZipedFileName));
11
12 ZipEntry theEntry;
13 while ((theEntry = s.GetNextEntry()) != null)
14 {
15
16 Console.WriteLine(theEntry.Name);
17
18 string directoryName = Path.GetDirectoryName(theEntry.Name);
19 string fileName = Path.GetFileName(theEntry.Name);
20
21 // create directory
22 if (directoryName.Trim() != "" && !Directory.Exists(directoryName))
23 Directory.CreateDirectory(directoryName);
24
25 if (fileName != String.Empty)
26 {
27 FileStream streamWriter = File.Create(theEntry.Name);
28
29 int size = 2048;
30 byte[] data = new byte[2048];
31 while (true)
32 {
33 size = s.Read(data, 0, data.Length);
34 if (size > 0)
35 {
36 streamWriter.Write(data, 0, size);
37 }
38 else
39 {
40 break;
41 }
42 }
43
44 streamWriter.Close();
45 }
46 }
47 s.Close();
48
49 Directory.SetCurrentDirectory(strOriCurrentDir);
50 }
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
感謝ZCC在文件下載過程中對我的指導.