仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 709|回复: 8
打印 上一主题 下一主题

[学习教程] ASP.NET网页设计ASP.NET完成文件的在线紧缩息争紧缩

[复制链接]
山那边是海 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:42:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
归根到底,Java跨平台可以,但是要重新编写代码,否则还分什么J2EE/J2SE/J2ME呢!asp.net|紧缩|在线  我们常常会碰到批量上传的成绩,也会碰到将某个目次下一切文件都上传到服务器上的成绩。那末,怎样办理此类成绩呢?之前的手艺一样平常接纳ActiveX等体例,这里笔者接纳SharpZlib来完成,传闻VS2005已有紧缩息争紧缩的办理计划,笔者还没偶然间用VS2005,以是就只好利用VS2003+SharpZlib来办理成绩了。

  1、起首从这里下载0.84版本的SharpZlib源码及示例码。

  2、下载上去以后你发明它没有VS2003的办理计划文件,没有干系。你能够本人创建,起首新建一个ZipUnzip的办理计划,然后,将下面经由解紧缩以后的一切文件及目次COPY到你的办理计划地点的目次下。

  3、在VS2003办理计划资本办理器(通常为在右上方中部点的地位)中点击显现一切文件按钮,然后能够见到良多“虚”的图标、文件及文件夹等,能够一次选择它们,然后包括进项目中。

  4、编译,最好利用Release选项,编译完成以后你能够在inRelease看到ZipUnzip.dll的类了。假如你编译时报错,说甚么AssemblyKeyFile之类的,你可使用强定名工具新建一个,也能够将AssemblyInfo.cs中[assembly:AssemblyKeyFile("。。。。。")]改成:[assembly:AssemblyKeyFile("")](不保举如许做)。

  5、新建一个WEBFORM项目,增加ZipUnzip.dll类的援用,然后增加以下文件及内容:

//------------------------------------------
//1.AttachmentUnZip.cs
//------------------------------------------
usingSystem;
usingSystem.IO;
usingICSharpCode.SharpZipLib.Zip;
usingICSharpCode.SharpZipLib.GZip;
usingICSharpCode.SharpZipLib.BZip2;
usingICSharpCode.SharpZipLib.Checksums;
usingICSharpCode.SharpZipLib.Zip.Compression;
usingICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespaceWebZipUnzip
{
 publicclassAttachmentUnZip
 {
  publicAttachmentUnZip()
  {}
  publicstaticvoidUpZip(stringzipFile)
  {
   string[]FileProperties=newstring[2];
   FileProperties[0]=zipFile;//待解压的文件
   FileProperties[1]=zipFile.Substring(0,zipFile.LastIndexOf("")+1);//解压后安排的方针目次
   UnZipClassUnZc=newUnZipClass();
   UnZc.UnZip(FileProperties);
  }
 }
}

//---------------------------------------------
//2.UnZipClass.cs
//---------------------------------------------

usingSystem;
usingSystem.IO;
usingICSharpCode.SharpZipLib.Zip;
usingICSharpCode.SharpZipLib.GZip;
usingICSharpCode.SharpZipLib.BZip2;
usingICSharpCode.SharpZipLib.Checksums;
usingICSharpCode.SharpZipLib.Zip.Compression;
usingICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespaceWebZipUnzip
{
 publicclassUnZipClass
 {
  ///
  ///解压文件
  ///
  ///包括要解压的文件名和要解压到的目次名数组
  publicvoidUnZip(string[]args)
  {
   ZipInputStreams=newZipInputStream(File.OpenRead(args[0]));
   try
   {
    ZipEntrytheEntry;
    while((theEntry=s.GetNextEntry())!=null)
    {
     stringdirectoryName=Path.GetDirectoryName(args[1]);
     stringfileName=Path.GetFileName(theEntry.Name);

     //天生解压目次
     Directory.CreateDirectory(directoryName);

     if(fileName!=String.Empty)
     {
      //解压文件到指定的目次
      FileStreamstreamWriter=File.Create(args[1]+fileName);
      intsize=2048;
      byte[]data=newbyte[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(Exceptioneu)
   {
    throweu;
   }
   finally
   {
    s.Close();
   }
  }//endUnZip

  publicstaticboolUnZipFile(stringfile,stringdir)
  {
   try
   {
    if(!Directory.Exists(dir))
     Directory.CreateDirectory(dir);
     stringfileFullName=Path.Combine(dir,file);
     ZipInputStreams=newZipInputStream(File.OpenRead(fileFullName));
 
     ZipEntrytheEntry;
     while((theEntry=s.GetNextEntry())!=null)
     {
      stringdirectoryName=Path.GetDirectoryName(theEntry.Name);
      stringfileName=Path.GetFileName(theEntry.Name);
 
      if(directoryName!=String.Empty)
       Directory.CreateDirectory(Path.Combine(dir,directoryName));
       if(fileName!=String.Empty)
       {
        FileStreamstreamWriter=File.Create(Path.Combine(dir,theEntry.Name));
        intsize=2048;
        byte[]data=newbyte[2048];
        while(true)
        {
         size=s.Read(data,0,data.Length);
         if(size>0)
         {
          streamWriter.Write(data,0,size);
         }
         else
         {
          break;
         }
        }
        streamWriter.Close();
       }
      }
      s.Close();
      returntrue;
     }
     catch(Exception)
     {
      throw;
     }
    }
   }//endUnZipClass
  }

//----------------------------------------------
//3.ZipClass.cs
//----------------------------------------------
usingSystem;
usingSystem.IO;
usingICSharpCode.SharpZipLib.Zip;
usingICSharpCode.SharpZipLib.GZip;
usingICSharpCode.SharpZipLib.BZip2;
usingICSharpCode.SharpZipLib.Checksums;
usingICSharpCode.SharpZipLib.Zip.Compression;
usingICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespaceWebZipUnzip
{
 ///
 ///紧缩文件
 ///
 publicclassZipClass
 {
  publicvoidZipFile(stringFileToZip,stringZipedFile,intCompressionLevel,intBlockSize,stringpassword)
  {
   //假如文件没有找到,则报错
   if(!System.IO.File.Exists(FileToZip))
   {
    thrownewSystem.IO.FileNotFoundException("Thespecifiedfile"+FileToZip+"couldnotbefound.Zippingaborderd");
   }

   System.IO.FileStreamStreamToZip=newSystem.IO.FileStream(FileToZip,System.IO.FileMode.Open,System.IO.FileAccess.Read);
   System.IO.FileStreamZipFile=System.IO.File.Create(ZipedFile);
   ZipOutputStreamZipStream=newZipOutputStream(ZipFile);
   ZipEntryZipEntry=newZipEntry("ZippedFile");
   ZipStream.PutNextEntry(ZipEntry);
   ZipStream.SetLevel(CompressionLevel);
   byte[]buffer=newbyte[BlockSize];
   System.Int32size=StreamToZip.Read(buffer,0,buffer.Length);
   ZipStream.Write(buffer,0,size);
   try
   {
    while(size<StreamToZip.Length)
    {
     intsizeRead=StreamToZip.Read(buffer,0,buffer.Length);
     ZipStream.Write(buffer,0,sizeRead);
     size+=sizeRead;
    }
   }
   catch(System.Exceptionex)
   {
    throwex;
   }
   ZipStream.Finish();
   ZipStream.Close();
   StreamToZip.Close();
  }

  publicvoidZipFileMain(string[]args)
  {
   //string[]filenames=Directory.GetFiles(args[0]);
   string[]filenames=newstring[]{args[0]};
 
   Crc32crc=newCrc32();
   ZipOutputStreams=newZipOutputStream(File.Create(args[1]));

   s.SetLevel(6);//0-storeonlyto9-meansbestcompression

   foreach(stringfileinfilenames)
   {
    //翻开紧缩文件
    FileStreamfs=File.OpenRead(file);
    byte[]buffer=newbyte[fs.Length];
    fs.Read(buffer,0,buffer.Length);
    ZipEntryentry=newZipEntry(file);

    entry.DateTime=DateTime.Now;

    //setSizeandthecrc,becausetheinformation
    //aboutthesizeandcrcshouldbestoredintheheader
    //ifitisnotsetitisautomaticallywritteninthefooter.
    //(inthiscasesize==crc==-1intheheader)
    //SomeZIPprogramshaveproblemswithzipfilesthatdontstore
    //thesizeandcrcintheheader.
    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
//-------------------------------------------

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.IO;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;

namespaceWebZipUnzip
{
 ///
 ///SummarydescriptionforWebForm1.
 ///
 publicclassWebForm1:System.Web.UI.Page
 {
  protectedSystem.Web.UI.WebControls.ButtonButton1;
  protectedSystem.Web.UI.HtmlControls.HtmlInputFileFile1;
  protectedSystem.Web.UI.WebControls.ButtonButton2;

  privatevoidPage_Load(objectsender,System.EventArgse)
  {
   //Putusercodetoinitializethepagehere
  }

  #regionWebFormDesignergeneratedcode
  overrideprotectedvoidOnInit(EventArgse)
  {
   //
   //CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
   //
   InitializeComponent();
   base.OnInit(e);
  }

  ///
  ///RequiredmethodforDesignersupport-donotmodify
  ///thecontentsofthismethodwiththecodeeditor.
  ///
  privatevoidInitializeComponent()
  {
   this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
   this.Button2.Click+=newSystem.EventHandler(this.Button2_Click);
   this.Load+=newSystem.EventHandler(this.Page_Load);

  }
  #endregion

  #region紧缩
  privatevoidButton1_Click(objectsender,System.EventArgse)
  {
   string[]FileProperties=newstring[2];
   stringfullName=this.File1.PostedFile.FileName;//C:        esta.txt
   stringdestPath=System.IO.Path.GetDirectoryName(fullName);//C:        est
   //待紧缩文件
   FileProperties[0]=fullName;

   //紧缩后的方针文件
   FileProperties[1]=destPath+""+System.IO.Path.GetFileNameWithoutExtension(fullName)+".zip";
   ZipClassZc=newZipClass();
   Zc.ZipFileMain(FileProperties);

   //删除紧缩前的文件
   System.IO.File.Delete(fullName);
  }

  #endregion

  #region解压
  privatevoidButton2_Click(objectsender,System.EventArgse)
  {
   stringfullName=this.File1.PostedFile.FileName;//C:        esta.zip
   //解压文件
   //AttachmentUnZip.UpZip(fullName);

   //string[]FileProperties=newstring[2];
   //FileProperties[0]=fullName;//待解压的文件
   //FileProperties[1]=System.IO.Path.GetDirectoryName(fullName);//解压后安排的方针目次
   //UnZipClassUnZc=newUnZipClass();
   //UnZc.UnZip(FileProperties);
   stringdir=System.IO.Path.GetDirectoryName(fullName);
   stringfileName=System.IO.Path.GetFileName(fullName);
   UnZipClass.UnZipFile(fileName,dir);
  }
  #endregion
 }
}
  此计划办理了文件名中笔墨的成绩,目次解紧缩成绩。

  至于全部文件夹批量上传并紧缩成一个WINZIP紧缩包的成绩,没偶然间办理了,列位若有办理计划,无妨共享一下。不过你如果学.net的话,你就不要选os了,这课比较底层的。你可以旁听数据库加上软件构件和中间件。(webservices和面向服务的课也应该听一听)
冷月葬花魂 该用户已被删除
沙发
发表于 2015-1-19 06:01:05 | 只看该作者
ASP是把代码交给VBScript解释器或Jscript解释器来解释,当然速度没有编译过的程序快了。
简单生活 该用户已被删除
板凳
发表于 2015-1-24 07:56:06 | 只看该作者
ASP.NET可以无缝地与WYSIWYGHTML编辑器和其他编程工具(包括MicrosoftVisualStudio.NET)一起工作。这不仅使得Web开发更加方便,而且还能提供这些工具必须提供的所有优点,包括开发人员可以用来将服务器控件拖放到Web页的GUI和完全集成的调试支持。微软为ASP.net设计了这样一些策略:易于写出结构清晰的代码、代码易于重用和共享、可用编译类语言编写等等,目的是让程序员更容易开发出Web应用,满足计算向Web转移的战略需要。
admin 该用户已被删除
地板
发表于 2015-1-31 22:16:27 | 只看该作者
如今主流的Web服务器软件主要由IIS或Apache组成。IIS支持ASP且只能运行在Windows平台下,Apache支持PHP,CGI,JSP且可运行于多种平台,虽然Apache是世界使用排名第一的Web服务器平台。
兰色精灵 该用户已被删除
5#
发表于 2015-2-7 00:15:54 | 只看该作者
目前在微软的.net战略中新推出的ASP.net借鉴了Java技术的优点,使用CSharp(C#)语言作为ASP.net的推荐语言,同时改进了以前ASP的安全性差等缺点。但是,使用ASP/ASP.net仍有一定的局限性,因为从某种角度来说它们只能在微软的WindowsNT/2000/XP+IIS的服务器平台上良好运行(虽然像ChilliSoft提供了在UNIX/Linux上运行ASP的解决方案.
活着的死人 该用户已被删除
6#
发表于 2015-2-19 04:35:13 | 只看该作者
Asp.net:首先来说,Asp.net和Asp没什么关系,看着像是升级版本什么的,其实没什么联系。Asp是脚本编程,用的是ASP语言,而ASP.net用的是C#语言,完全不同的东西。
老尸 该用户已被删除
7#
发表于 2015-3-6 13:05:00 | 只看该作者
对于中小项目来说.net技术是完全可以胜任,但为什么现在大型公司或网站都选择php或java呢?就是因为微软不够开放,没有提供从硬件到应用服务器再到业务应用的整套解决方案。
金色的骷髅 该用户已被删除
8#
发表于 2015-3-13 01:08:16 | 只看该作者
HTML:当然这是网页最基本的语言,每一个服务器语言都需要它的支持,要学习,这个肯定是开始,不说了.
精灵巫婆 该用户已被删除
9#
发表于 2015-3-20 08:17:23 | 只看该作者
平台无关性是PHP的最大优点,但是在优点的背后,还是有一些小小的缺点的。如果在PHP中不使用ODBC,而用其自带的数据库函数(这样的效率要比使用ODBC高)来连接数据库的话,使用不同的数据库,PHP的函数名不能统一。这样,使得程序的移植变得有些麻烦。不过,作为目前应用最为广泛的一种后台语言,PHP的优点还是异常明显的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-22 01:56

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表