国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用ASP.Net實(shí)現(xiàn)文件的在線壓縮和解壓縮

2024-07-10 13:13:00
字體:
供稿:網(wǎng)友

  我們經(jīng)常會(huì)遇到批量上傳的問題,也會(huì)遇到將某個(gè)目錄下所有文件都上傳到服務(wù)器上的問題。那么,如何解決此類問題呢?以前的技術(shù)一般采用activex等方式,這里筆者采用sharpzlib來實(shí)現(xiàn),聽說vs2005已有壓縮和解壓縮的解決方案,筆者還沒有時(shí)間用vs2005,所以就只好使用vs2003 + sharpzlib來解決問題了。

  1、首先從這里下載0.84版本的sharpzlib源碼及示例碼。
  2、下載下來之后你發(fā)現(xiàn)它沒有vs2003的解決方案文件,沒有關(guān)系。你可以自己建立,首先新建一個(gè)zipunzip的解決方案,然后,將上面經(jīng)過解壓縮之后的所有文件及目錄copy到你的解決方案所在的目錄下。
  3、在vs2003解決方案資源管理器(一般是在右上方中部點(diǎn)的位置)中點(diǎn)擊顯示所有文件按鈕,然后可以見到很多“虛”的圖標(biāo)、文件及文件夾等,可以一次選擇它們,然后包含進(jìn)項(xiàng)目中。
  4、編譯,最好使用release選項(xiàng),編譯完成之后你可以在/bin/release/看到zipunzip.dll的類了。如果你編譯時(shí)報(bào)錯(cuò),說什么assemblykeyfile之類的,你可以使用強(qiáng)命名工具新建一個(gè),也可以將assemblyinfo.cs中[assembly: assemblykeyfile("。。。。。")]改成:[assembly: assemblykeyfile("")]  (不推薦這樣做)。
  5、新建一個(gè)webform項(xiàng)目,添加zipunzip.dll類的引用,然后添加如下文件及內(nèi)容:

// ------------------------------------------
// 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);//解壓后放置的目標(biāo)目錄
   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">包含要解壓的文件名和要解壓到的目錄名數(shù)組</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)
  {
   //如果文件沒有找到,則報(bào)錯(cuò)
   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;

   //壓縮后的目標(biāo)文件
   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);//解壓后放置的目標(biāo)目錄
//   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! 試試看。

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 淮阳县| 白玉县| 深圳市| 石城县| 微山县| 虞城县| 原平市| 公主岭市| 龙岩市| 神农架林区| 龙岩市| 长白| 水城县| 华安县| 凌海市| 托克逊县| 遂宁市| 南京市| 克什克腾旗| 晋州市| 边坝县| 北流市| 舒兰市| 林口县| 太康县| 商河县| 将乐县| 湖南省| 泗洪县| 临泉县| 兰西县| 基隆市| 徐水县| 阳高县| 鄂托克前旗| 泰来县| 南通市| 武川县| 东源县| 怀化市| 阳新县|