|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
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),我一同学说,你应该早就有作品啦。我惶惶然…… |
|