仓酷云

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

[学习教程] ASP.NET教程之用ASP.Net完成文件的在线紧缩息争紧缩

[复制链接]
愤怒的大鸟 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:28:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
因为二次编译器太复杂,那么建议只是在安装程序的时候编译一次,而不类似java那样运行就编译。并且我觉得,一次痛苦,总比多次低效率要舒服多了。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
{
///<summary>
///解压文件
///</summary>
///<paramname="args">包括要解压的文件名和要解压到的目次名数组</param>
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
{
///<summary>
///紧缩文件
///</summary>
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
//---------------------------------------------
<%@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="WebZipUnzip.WebForm1"%>
<METAcontent="MicrosoftVisualStudio.NET7.1"name=GENERATOR>
<METAcontent=C#name=CODE_LANGUAGE>
<METAcontent=JavaScriptname=vs_defaultClientScript>
<METAcontent=http://schemas.microsoft.com/intellisense/ie5name=vs_targetSchema>
<FORMid=Form1method=postrunat="server"><?xml:namespaceprefix=asp/><asp:Buttonid=Button1style="Z-INDEX:101;LEFT:56px;POSITION:absolute;TOP:64px"runat="server"Text="紧缩"></asp:Button><asp:Buttonid=Button2style="Z-INDEX:102;LEFT:112px;POSITION:absolute;TOP:64px"runat="server"Text="解压"></asp:Button><INPUTid=File1style="Z-INDEX:103;LEFT:32px;POSITION:absolute;TOP:24px"type=filename=File1runat="server"></FORM></BODY></HTML>
//-------------------------------------------
//5.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
{
///<summary>
///SummarydescriptionforWebForm1.
///</summary>
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);
}

///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
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
}
}
  OK!碰运气。
  此计划办理了文件名中笔墨的成绩,目次解紧缩成绩。
  至于全部文件夹批量上传并紧缩成一个WINZIP紧缩包的成绩,没偶然间办理了,列位若有办理计划,无妨共享一下。
不可能天天有学习.net),我一同学说,你应该早就有作品啦。我惶惶然……
沙发
发表于 2015-1-17 21:39:28 | 只看该作者
市场决定一切,我个人从经历上觉得两者至少在很长时间内还是要共存下去,包括C和C++,至少从找工作就看得出来,总不可能大家都像所谓的时尚一样,追捧一门语言并应用它。
爱飞 该用户已被删除
板凳
发表于 2015-1-21 08:52:19 | 只看该作者
由于JSP/Servlet都是基于Java的,所以它们也有Java语言的最大优点——平台无关性,也就是所谓的“一次编写,随处运行(WORA–WriteOnce,RunAnywhere)”。除了这个优点,JSP/Servlet的效率以及安全性也是相当惊人的。
飘灵儿 该用户已被删除
地板
发表于 2015-1-30 12:55:42 | 只看该作者
这也就是最近几年来随着各种新的后台技术的诞生,CGI应用在Internet上越来越少的原因。CGI方式不适合大访问量的应用。
若相依 该用户已被删除
5#
发表于 2015-2-6 11:55:38 | 只看该作者
asp.net空间的支持有:ASP.NET1.1/虚拟目录/MicrosoftFrontPage2000扩展/CDONTS,同时他的网站上也提供了Asp.net的使用详解和程序源代码,相信对使用ASP.NET编程的程序员来说会非常有用哦!
冷月葬花魂 该用户已被删除
6#
发表于 2015-2-16 02:53:24 | 只看该作者
是指转换后的Servlet程序代码的行数。这给调试代码带来一定困难。所以,在排除错误时,可以采取分段排除的方法(在可能出错的代码前后输出一些字符串,用字符串是否被输出来确定代码段从哪里开始出错)。
admin 该用户已被删除
7#
发表于 2015-3-4 23:14:14 | 只看该作者
由于JSP/Servlet都是基于Java的,所以它们也有Java语言的最大优点——平台无关性,也就是所谓的“一次编写,随处运行(WORA–WriteOnce,RunAnywhere)”。除了这个优点,JSP/Servlet的效率以及安全性也是相当惊人的。
不帅 该用户已被删除
8#
发表于 2015-3-11 21:20:06 | 只看该作者
但是目前在CGI中使用的最为广泛的是Perl语言。所以,狭义上所指的CGI程序一般都是指Perl程序,一般CGI程序的后缀都是.pl或者.cgi。
小女巫 该用户已被删除
9#
发表于 2015-3-28 08:17:32 | 只看该作者
虽然在形式上JSP和ASP或PHP看上去很相似——都可以被内嵌在HTML代码中。但是,它的执行方式和ASP或PHP完全不同。在JSP被执行的时候,JSP文件被JSP解释器(JSPParser)转换成Servlet代码,然后Servlet代码被Java编译器编译成.class字节文件,这样就由生成的Servlet来对客户端应答。所以,JSP可以看做是Servlet的脚本语言(ScriptLanguage)版。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-27 19:43

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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