我們經常會遇到批量上傳的問題,也會遇到將某個目錄下所有文件都上傳到服務器上的問題。那么,如何解決此類問題呢?以前的技術一般采用activex等方式,這里筆者采用sharpzlib來實現,聽說vs2005已有壓縮和解壓縮的解決方案,筆者還沒有時間用vs2005,所以就只好使用vs2003 + sharpzlib來解決問題了。 
  1、首先從這里下載0.84版本的sharpzlib源碼及示例碼。
  2、下載下來之后你發現它沒有vs2003的解決方案文件,沒有關系。你可以自己建立,首先新建一個zipunzip的解決方案,然后,將上面經過解壓縮之后的所有文件及目錄copy到你的解決方案所在的目錄下。 
  3、在vs2003解決方案資源管理器(一般是在右上方中部點的位置)中點擊顯示所有文件按鈕,然后可以見到很多“虛”的圖標、文件及文件夾等,可以一次選擇它們,然后包含進項目中。
  4、編譯,最好使用release選項,編譯完成之后你可以在/bin/release/看到zipunzip.dll的類了。如果你編譯時報錯,說什么assemblykeyfile之類的,你可以使用強命名工具新建一個,也可以將assemblyinfo.cs中[assembly: assemblykeyfile("。。。。。")]改成:[assembly: assemblykeyfile("")] (不推薦這樣做)。
  5、新建一個webform項目,添加zipunzip.dll類的引用,然后添加如下文件及內容:
// ------------------------------------------
// 1. attachmentunzip.cs
// ------------------------------------------
using system;
using system.io;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.gzip;
using icsharpcode.sharpziplib.bzip2;
using icsharpcode.sharpziplib.checksums;
using icsharpcode.sharpziplib.zip.compression;
using icsharpcode.sharpziplib.zip.compression.streams;
namespace webzipunzip
{
 public class attachmentunzip
 {
  public attachmentunzip()
  {}
  public static void upzip(string zipfile)
  {
   string []fileproperties=new string[2];
   fileproperties[0]=zipfile;//待解壓的文件
   fileproperties[1]=zipfile.substring(0,zipfile.lastindexof("http://")+1);//解壓后放置的目標目錄
   unzipclass unzc=new unzipclass();
   unzc.unzip(fileproperties);
  }
 }
}
// ---------------------------------------------
// 2. unzipclass.cs
// ---------------------------------------------
using system;
using system.io;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.gzip;
using icsharpcode.sharpziplib.bzip2;
using icsharpcode.sharpziplib.checksums;
using icsharpcode.sharpziplib.zip.compression;
using icsharpcode.sharpziplib.zip.compression.streams;
namespace webzipunzip
{
 public class unzipclass
 { 
  /// 
  /// 解壓文件
  /// 
  /// 包含要解壓的文件名和要解壓到的目錄名數組
  public void unzip(string[] args)
  {
   zipinputstream s = new zipinputstream(file.openread(args[0]));
   try
   { 
    zipentry theentry;
    while ((theentry = s.getnextentry()) != null) 
    { 
     string directoryname = path.getdirectoryname(args[1]);
     string filename = path.getfilename(theentry.name);
     //生成解壓目錄
     directory.createdirectory(directoryname);
     if (filename != string.empty) 
     { 
      //解壓文件到指定的目錄
      filestream streamwriter = file.create(args[1]+filename);
      int size = 2048;
      byte[] data = new byte[2048];
      while (true) 
      {
       size = s.read(data, 0, data.length);
       if (size > 0) 
       {
        streamwriter.write(data, 0, size);
       } 
       else 
       {
        break;
       }
      }
      streamwriter.close();
     }
    }
    s.close();
   }
   catch(exception eu)
   {
    throw eu;
   }
   finally
   {
    s.close();
   }
  }//end unzip
  public static bool unzipfile(string file, string dir)
  {
   try
   {
    if (!directory.exists(dir))
     directory.createdirectory(dir);
     string filefullname = path.combine(dir,file);
     zipinputstream s = new zipinputstream(file.openread( filefullname ));
 
     zipentry theentry;
     while ((theentry = s.getnextentry()) != null)
     {
      string directoryname = path.getdirectoryname(theentry.name);
      string filename = path.getfilename(theentry.name);
 
      if (directoryname != string.empty)
       directory.createdirectory( path.combine(dir, directoryname));
       if (filename != string.empty)
       {
        filestream streamwriter = file.create( path.combine(dir,theentry.name) );
        int size = 2048;
        byte[] data = new byte[2048];
        while (true)
        {
         size = s.read(data, 0, data.length);
         if (size > 0)
         {
          streamwriter.write(data, 0, size);
         }
         else
         {
          break;
         }
        }
        streamwriter.close();
       }
      }
      s.close();
      return true;
     }
     catch (exception)
     {
      throw;
     }
    }
   }//end unzipclass
  }
// ----------------------------------------------
// 3. zipclass.cs
// ----------------------------------------------
using system;
using system.io;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.gzip;
using icsharpcode.sharpziplib.bzip2;
using icsharpcode.sharpziplib.checksums;
using icsharpcode.sharpziplib.zip.compression;
using icsharpcode.sharpziplib.zip.compression.streams;
namespace webzipunzip
{
 /// 
 /// 壓縮文件
 /// 
 public class zipclass
 {
  public void zipfile(string filetozip, string zipedfile ,int compressionlevel, int blocksize,string password)
  {
   //如果文件沒有找到,則報錯
   if (! system.io.file.exists(filetozip)) 
   {
    throw new system.io.filenotfoundexception("the specified file " + filetozip + " could not be found. zipping aborderd");
   }
   system.io.filestream streamtozip = new system.io.filestream(filetozip,system.io.filemode.open , system.io.fileaccess.read);
   system.io.filestream zipfile = system.io.file.create(zipedfile);
   zipoutputstream zipstream = new zipoutputstream(zipfile);
   zipentry zipentry = new zipentry("zippedfile");
   zipstream.putnextentry(zipentry);
   zipstream.setlevel(compressionlevel);
   byte[] buffer = new byte[blocksize];
   system.int32 size =streamtozip.read(buffer,0,buffer.length);
   zipstream.write(buffer,0,size);
   try 
   {
    while (size < streamtozip.length) 
    {
     int sizeread =streamtozip.read(buffer,0,buffer.length);
     zipstream.write(buffer,0,sizeread);
     size += sizeread;
    }
   } 
   catch(system.exception ex)
   {
    throw ex;
   }
   zipstream.finish();
   zipstream.close();
   streamtozip.close();
  }
  public void zipfilemain(string[] args)
  {
   //string[] filenames = directory.getfiles(args[0]);
   string[] filenames = new string[]{args[0]};
 
   crc32 crc = new crc32();
   zipoutputstream s = new zipoutputstream(file.create(args[1]));
   s.setlevel(6); // 0 - store only to 9 - means best compression
   foreach (string file in filenames) 
   {
    //打開壓縮文件
    filestream fs = file.openread(file); 
    byte[] buffer = new byte[fs.length];
    fs.read(buffer, 0, buffer.length);
    zipentry entry = new zipentry(file);
    entry.datetime = datetime.now;
    // set size and the crc, because the information
    // about the size and crc should be stored in the header
    // if it is not set it is automatically written in the footer.
    // (in this case size == crc == -1 in the header)
    // some zip programs have problems with zip files that don't store
    // the size and crc in the header.
    entry.size = fs.length;
    fs.close();
    crc.reset();
    crc.update(buffer);
    entry.crc = crc.value;
    s.putnextentry(entry);
    s.write(buffer, 0, buffer.length);
   } 
   s.finish();
   s.close();
  }
 }
}
// ---------------------------------------------
// 4.webform1.aspx.cs
//-------------------------------------------
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.io;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace webzipunzip
{
 /// 
 /// summary description for webform1.
 /// 
 public class webform1 : system.web.ui.page
 {
  protected system.web.ui.webcontrols.button button1;
  protected system.web.ui.htmlcontrols.htmlinputfile file1;
  protected system.web.ui.webcontrols.button button2;
  private void page_load(object sender, system.eventargs e)
  {
   // put user code to initialize the page here
  }
  #region web form designer generated code
  override protected void oninit(eventargs e)
  {
   //
   // codegen: this call is required by the asp.net web form designer.
   //
   initializecomponent();
   base.oninit(e);
  }
  /// 
  /// required method for designer support - do not modify
  /// the contents of this method with the code editor.
  /// 
  private void initializecomponent()
  { 
   this.button1.click += new system.eventhandler(this.button1_click);
   this.button2.click += new system.eventhandler(this.button2_click);
   this.load += new system.eventhandler(this.page_load);
  }
  #endregion
  #region 壓縮
  private void button1_click(object sender, system.eventargs e)
  {
   string []fileproperties=new string[2]; 
   string fullname=this.file1.postedfile.filename;//c:/test/a.txt
   string destpath=system.io.path.getdirectoryname(fullname);//c:/test
   //待壓縮文件
   fileproperties[0]=fullname;
   //壓縮后的目標文件
   fileproperties[1]= destpath +"http://"+ system.io.path.getfilenamewithoutextension(fullname) + ".zip";
   zipclass zc=new zipclass();
   zc.zipfilemain(fileproperties);
   //刪除壓縮前的文件
   system.io.file.delete(fullname);
  }
  #endregion
  #region 解壓
  private void button2_click(object sender, system.eventargs e)
  {
   string fullname=this.file1.postedfile.filename;//c:/test/a.zip
   //解壓文件
   //attachmentunzip.upzip(fullname);
   // string[] fileproperties = new string[2];
   // fileproperties[0] = fullname;//待解壓的文件
   // fileproperties[1] = system.io.path.getdirectoryname(fullname);//解壓后放置的目標目錄
   // unzipclass unzc=new unzipclass();
   // unzc.unzip(fileproperties);
   string dir = system.io.path.getdirectoryname(fullname);
   string filename = system.io.path.getfilename(fullname);
   unzipclass.unzipfile(filename, dir);
  }
  #endregion
 }
}
  此方案解決了文件名中文字的問題,目錄解壓縮問題。
  至于整個文件夾批量上傳并壓縮成一個winzip壓縮包的問題,沒有時間解決了,各位如有解決方案,不妨共享一下。