仓酷云

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

[学习教程] ASP.NET教程之C#中使用SharpZipLib举行紧缩息争压

[复制链接]
萌萌妈妈 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:37:50 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

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

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

x
我感觉可以顶到50楼,出乎意料的是大家居然纷纷写出自己的博文,还被编辑做成了专题,置于首页头条。紧缩  我在做项目标时分必要将文件举行紧缩息争紧缩,因而就从http://www.icsharpcode.net下载了关于紧缩息争紧缩的源码,可是下载上去后,面临这么多的代码,一时不知怎样动手。只好耐下心来,渐渐的研讨,总算找到了路径。针对本人的必要改写了文件紧缩息争紧缩的两个类,分离为ZipClass和UnZipClass。个中碰着了很多坚苦,就决意写出来紧缩息争压的程序后,必定把源码贴出来共享,让初次打仗紧缩息争紧缩的伴侣能够少走些弯路。上面就来注释怎样在C#里用http://www.icsharpcode.net下载的SharpZipLib举行文件的紧缩息争紧缩。
  起首必要在项目里援用sharpziplib.dll。然后修正个中的关于紧缩息争紧缩的类。完成源码以下:
///<summary>
///紧缩文件
///</summary>
usingSystem;
usingSystem.IO;
usingICSharpCode.SharpZipLib.Checksums;
usingICSharpCode.SharpZipLib.Zip;
usingICSharpCode.SharpZipLib.GZip;
namespaceCompression
{
publicclassZipClass
{
publicvoidZipFile(stringFileToZip,stringZipedFile,intCompressionLevel,intBlockSize)
{
//假如文件没有找到,则报错
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]);
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();
}
}
}
  如今再来看看解压文件类的源码
///<summary>
///解压文件
///</summary>
usingSystem;
usingSystem.Text;
usingSystem.Collections;
usingSystem.IO;
usingSystem.Diagnostics;
usingSystem.Runtime.Serialization.Formatters.Binary;
usingSystem.Data;
usingICSharpCode.SharpZipLib.BZip2;
usingICSharpCode.SharpZipLib.Zip;
usingICSharpCode.SharpZipLib.Zip.Compression;
usingICSharpCode.SharpZipLib.Zip.Compression.Streams;
usingICSharpCode.SharpZipLib.GZip;
namespaceDeCompression
{
publicclassUnZipClass
{
publicvoidUnZip(string[]args)
{
ZipInputStreams=newZipInputStream(File.OpenRead(args[0]));
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]+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();
}
}
}
  有了紧缩息争紧缩的类今后,就要在窗体里挪用了。怎样?是老手,不会挪用?ok,接着往下看怎样在窗体里挪用。
  起首在窗体里安排两个命令按钮(不要告知我你不会放啊~),然后编写以下源码
///<summary>
///挪用源码
///</summary>
privatevoidbutton2_Click_1(objectsender,System.EventArgse)
{
string[]FileProperties=newstring[2];
FileProperties[0]="C:unzipped";//待紧缩文件目次
FileProperties[1]="C:zipa.zip";//紧缩后的方针文件
ZipClassZc=newZipClass();
Zc.ZipFileMain(FileProperties);
}
privatevoidbutton2_Click(objectsender,System.EventArgse)
{
string[]FileProperties=newstring[2];
FileProperties[0]="C:zip        est.zip";//待解压的文件
FileProperties[1]="C:unzipped";//解压后安排的方针目次
UnZipClassUnZc=newUnZipClass();
UnZc.UnZip(FileProperties);
}
  好了,到此为止,怎样紧缩息争紧缩的类都已完成了,必要的伴侣间接拿走调吧。
我觉得很重要,一般所说的不重要应该指的是:你学好一种以后再学另一种就很容易了。(因为这样大家可能有一个错觉就是语言不是很重要,只要随便学一种就可以了,其实不是这样的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-16 17:40

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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