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

asp.net實現文件夾及文件壓縮,並實現下載

2022-06-13   來源: .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:華海實訓(qqviewcom)各級賬號和密碼";
        
               dlZipDir(userPath "華海實訓(qqviewcom)服務賬號");

  }

  #endregion

  


From:http://tw.wingwit.com/Article/program/net/201311/14232.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.