亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > .NET > 正文

用ASP.Net實現文件的在線壓縮和解壓縮

2024-07-10 13:13:00
字體:
來源:轉載
供稿:網友

  我們經常會遇到批量上傳的問題,也會遇到將某個目錄下所有文件都上傳到服務器上的問題。那么,如何解決此類問題呢?以前的技術一般采用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
 {  
  /// <summary>
  /// 解壓文件
  /// </summary>
  /// <param name="args">包含要解壓的文件名和要解壓到的目錄名數組</param>
  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
{
 /// <summary>
 /// 壓縮文件
 /// </summary>
 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
// ---------------------------------------------
  <%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="webzipunzip.webform1" %>&nbsp;
<meta content="microsoft visual studio .net 7.1" name=generator>
<meta content=c# name=code_language>
<meta content=javascript name=vs_defaultclientscript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetschema>
<form id=form1 method=post runat="server"><?xml:namespace prefix = asp /><asp:button id=button1 runat="server" text="壓縮"></asp:button><asp:button id=button2 runat="server" text="解壓"></asp:button><input id=file1 type=file name=file1 runat="server"> </form></body></html>

//-------------------------------------------
// 5.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>
 /// summary description for webform1.
 /// </summary>
 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);
  }
 
  /// <summary>
  /// required method for designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  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
 }
}

  ok! 試試看。

  此方案解決了文件名中文字的問題,目錄解壓縮問題。
  至于整個文件夾批量上傳并壓縮成一個winzip壓縮包的問題,沒有時間解決了,各位如有解決方案,不妨共享一下。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品黄页免费高清在线观看| 97人人做人人爱| 亚洲一区二区久久久| 亚洲精品之草原avav久久| 高清欧美性猛交xxxx| 国产在线视频2019最新视频| 中文字幕亚洲专区| 91夜夜未满十八勿入爽爽影院| 久久躁日日躁aaaaxxxx| 菠萝蜜影院一区二区免费| 欧美激情免费在线| 精品女同一区二区三区在线播放| 精品亚洲永久免费精品| 亚洲精品自拍视频| 久久综合九色九九| 亚洲va国产va天堂va久久| 91亚洲国产成人久久精品网站| 欧美在线免费看| 精品一区二区电影| 日韩国产精品亚洲а∨天堂免| 欧美一级免费看| 亚洲人在线视频| 伦伦影院午夜日韩欧美限制| 福利精品视频在线| 国产欧美精品久久久| 亚洲天堂第一页| 日韩免费精品视频| 国产精品日韩av| 国语自产在线不卡| 国产aaa精品| 国产日韩精品在线播放| 亚洲性生活视频在线观看| 日韩久久精品成人| 中文在线资源观看视频网站免费不卡| 久久免费精品日本久久中文字幕| 91免费欧美精品| 欧美人在线视频| 亚洲国产精品久久久| 久久中文字幕在线视频| 亚洲国产精品久久久久秋霞蜜臀| 久久久久久久999| 国产一区视频在线播放| 国产精品永久免费观看| 亚洲免费人成在线视频观看| 国产成人精品视频在线观看| 久久91精品国产91久久久| 国产精品一二区| 国产成人在线一区| 亚洲第一网站免费视频| 国产99久久精品一区二区永久免费| 日韩高清av一区二区三区| 久久久久久有精品国产| 亚洲欧美精品一区二区| 日韩欧美亚洲一二三区| 亚洲综合中文字幕在线观看| 国产精品盗摄久久久| 亚洲视频一区二区| 欧美一级视频免费在线观看| 国产精品成人品| 欧美网站在线观看| 亚洲电影第1页| www.日韩欧美| 91网站免费看| 国产丝袜高跟一区| 黄网动漫久久久| 精品亚洲一区二区| 亚洲激情视频在线播放| 45www国产精品网站| 欧美大片免费看| 97国产suv精品一区二区62| 91精品视频网站| 欧美中文字幕在线观看| 欧美重口另类videos人妖| 亚洲精品一区二区久| www国产亚洲精品久久网站| 国产精品日韩欧美综合| 国产精品免费一区二区三区都可以| 国产精品激情av电影在线观看| 久久人体大胆视频| 国产精品av电影| 97欧美精品一区二区三区| 亚洲国产精品女人久久久| 国产精品电影一区| 在线亚洲午夜片av大片| 国产福利视频一区| 九九热99久久久国产盗摄| 91久久久亚洲精品| 亚洲国产又黄又爽女人高潮的| 欧美性猛交xxxx久久久| 精品视频一区在线视频| 国产精品久久婷婷六月丁香| 中文字幕久久久av一区| 国产精品久久不能| 亚洲第一精品久久忘忧草社区| 国产一区二区日韩| 日韩hd视频在线观看| 亚洲国产精品中文| 国产精品一二三视频| 不卡在线观看电视剧完整版| 午夜精品蜜臀一区二区三区免费| 欧美一级淫片播放口| 亚洲第一免费网站| 亚洲精品成人久久久| 国产精品专区一| 久久久久久91香蕉国产| 国内伊人久久久久久网站视频| 欧美激情网友自拍| 欧美日韩一区二区免费视频| 精品国产乱码久久久久久虫虫漫画| 永久免费毛片在线播放不卡| 美女扒开尿口让男人操亚洲视频网站| 国自在线精品视频| 欧美视频一区二区三区…| 精品国产91久久久| 国产精品久久国产精品99gif| 亚洲欧美另类在线观看| 日韩欧美成人免费视频| 欧美激情按摩在线| 日韩欧美在线中文字幕| 国产精品精品久久久久久| 最近2019中文字幕mv免费看| 91精品国产91久久久久福利| 欧美电影《睫毛膏》| 成人免费淫片视频软件| 日韩av在线一区二区| 成人黄色激情网| 精品国产一区二区三区久久| 久久香蕉国产线看观看网| 富二代精品短视频| 亚洲国产欧美自拍| 欧美精品videos| 亚洲国产精品va在线| 欧美黑人xxxⅹ高潮交| 欧美日韩国产专区| 国产精品丝袜久久久久久高清| 久久国产精品久久久久久久久久| 成人网在线免费看| 精品中文字幕在线2019| 欧美老少配视频| 成年无码av片在线| 欧美黑人性猛交| 亚洲国产另类久久精品| 青青精品视频播放| 日本不卡免费高清视频| 久久久久久久国产| 久久人人爽亚洲精品天堂| 日韩av中文字幕在线播放| xvideos亚洲人网站| 美女少妇精品视频| 欧美日韩激情小视频| 欧美色图在线视频| 国产精品亚洲视频在线观看| 97av在线影院| 精品香蕉在线观看视频一| 国产成人+综合亚洲+天堂| 欧美激情精品在线| 欧美精品在线看| 久99九色视频在线观看| 亚洲自拍小视频| 欧美在线观看一区二区三区| 亚洲国产另类 国产精品国产免费| 国产不卡一区二区在线播放| 精品久久中文字幕久久av| 亚洲香蕉在线观看|