熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net用代碼壓縮服務器上文件夾或文件

2013-11-13 10:42:06  來源: .NET編程 

  實現過程

)先引用 ICSharpCodeSharpZipLibdll

cs文件引入

using SystemIO;
using SystemText;

using ICSharpCodeSharpZipLibChecksums;
using ICSharpCodeSharpZipLibZip;
using ICSharpCodeSharpZipLibGZip;  

)代碼 

 #region 下列代碼為壓縮並下載代碼
       ZipOutputStream zos = null;
       String strBaseDir = ""; 

       void dlZipDir(string strPath string strFileName)
       {
           MemoryStream ms = null;
           ResponseContentType = "application/octetstream";
           strFileName = HttpUtilityUrlEncode(strFileName)Replace(’+’ ’ ’);
           ResponseAddHeader("ContentDisposition" "attachment;   filename=" + strFileName + "zip");
           ms = new MemoryStream();
           zos = new ZipOutputStream(ms);
           strBaseDir = strPath + "";
           addZipEntry(strBaseDir);
           zosFinish();
           zosClose();
           ResponseClear();
           ResponseBinaryWrite(msToArray());
           ResponseEnd();
       }

       void addZipEntry(string PathStr)
       {
           DirectoryInfo di = new DirectoryInfo(PathStr);
           foreach (DirectoryInfo item in diGetDirectories())
           {
               addZipEntry(itemFullName);
           }
           foreach (FileInfo item in diGetFiles())
           {
               FileStream fs = FileOpenRead(itemFullName);
               byte[] buffer = new byte[fsLength];
               fsRead(buffer  bufferLength);
               string strEntryName = itemFullNameReplace(strBaseDir "");
               ZipEntry entry = new ZipEntry(strEntryName);
               zosPutNextEntry(entry);
               zosWrite(buffer  bufferLength);
               fsClose();
           }
       }    
        
       PRotected void Button_Click(object sender EventArgs e)
        {
            string userPath ="D:華海實訓(hohisoftcom)各級賬號和密碼";
         
               dlZipDir(userPath "華海實訓(hohisoftcom)服務賬號");  
        }

       #endregion


From:http://tw.wingwit.com/Article/program/net/201311/14328.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.