网络传输压缩DLL: ICSharpCode.SharpZipLib
2017-04-24 18:43
369 查看
UnityGZip压缩数据DLL,这个文章实在包含了在U3D中的应用以及源代码和Dll
BZip2和Tar格式
好了,深入的大家还要多多研究,今天我们简单介绍一下简单的单文件、文件夹的压缩和解压
先给大家看一下效果:
一、引入ICSharpCode.SharpZipLib
我们新建个帮助类 ZipHelper.cs 然后添加dll引用二、添加完dll引用之后我们需要添加这几个Using引用
1usingICSharpCode.SharpZipLib.Checksums; 2usingICSharpCode.SharpZipLib.Zip; 3usingSystem;4usingSystem.IO;
三、压缩单个文件
这里我添加了几个参数大家可以根据自己的需要修改一下///ZIP:压缩文件夹
///addyuangangby2016-06-13
///</summary>
///<paramname="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
///<paramname="ZipedPath">压缩后的文件路径(绝对路径)</param>
///<paramname="ZipedFileName">压缩后的文件名称(文件名,默认同源文件夹同名)</param>
///<paramname="IsEncrypt">是否加密(默认加密)</param>
publicstaticvoidZipDirectory(stringDirectoryToZip,stringZipedPath,stringZipedFileName="",boolIsEncrypt=true)
{
//如果目录不存在,则报错
if(!System.IO.Directory.Exists(DirectoryToZip))
{
thrownewSystem.IO.FileNotFoundException("指定的目录:"+DirectoryToZip+"不存在!");
}
//文件名称(默认同源文件名称相同)
stringZipFileName=string.IsNullOrEmpty(ZipedFileName)?ZipedPath+"\\"+newDirectoryInfo(DirectoryToZip).Name+".zip":ZipedPath+"\\"+ZipedFileName+".zip";
using(System.IO.FileStreamZipFile=System.IO.File.Create(ZipFileName))
{
using(ZipOutputStreams=newZipOutputStream(ZipFile))
{
if(IsEncrypt)
{
//压缩文件加密
s.Password="123";
}
ZipSetp(DirectoryToZip,s,"");
}
}
}
///<summary>
///递归遍历目录
///addyuangangby2016-06-13
///</summary>
privatestaticvoidZipSetp(stringstrDirectory,ZipOutputStreams,stringparentPath)
{
if(strDirectory[strDirectory.Length-1]!=Path.DirectorySeparatorChar)
{
strDirectory+=Path.DirectorySeparatorChar;
}
Crc32crc=newCrc32();
string[]filenames=Directory.GetFileSystemEntries(strDirectory);
foreach(stringfileinfilenames)//遍历所有的文件和目录
{
if(Directory.Exists(file))//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
stringpPath=parentPath;
pPath+=file.Substring(file.LastIndexOf("\\")+1);
pPath+="\\";
ZipSetp(file,s,pPath);
}
else//否则直接压缩文件
{
//打开压缩文件
using(FileStreamfs=File.OpenRead(file))
{
byte[]buffer=newbyte[fs.Length];
fs.Read(buffer,0,buffer.Length);
stringfileName=parentPath+file.Substring(file.LastInd
e4f2
exOf("\\")+1);
ZipEntryentry=newZipEntry(fileName);
entry.DateTime=DateTime.Now;
entry.Size=fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc=crc.Value;
s.PutNextEntry(entry);
s.Write(buffer,0,buffer.Length);
}
}
}
}
四、压缩文件夹
1///<summary>
2///ZIP:压缩文件夹
3///addyuangangby2016-06-13
4///</summary>
5///<paramname="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
6///<paramname="ZipedPath">压缩后的文件路径(绝对路径)</param>
7///<paramname="ZipedFileName">压缩后的文件名称(文件名,默认同源文件夹同名)</param>
8///<paramname="IsEncrypt">是否加密(默认加密)</param>
9publicstaticvoidZipDirectory(stringDirectoryToZip,stringZipedPath,stringZipedFileName="",boolIsEncrypt=true)
10{
11//如果目录不存在,则报错
12if(!System.IO.Directory.Exists(DirectoryToZip))
13{
14thrownewSystem.IO.FileNotFoundException("指定的目录:"+DirectoryToZip+"不存在!");
15}
16
17//文件名称(默认同源文件名称相同)
18stringZipFileName=string.IsNullOrEmpty(ZipedFileName)?ZipedPath+"\\"+newDirectoryInfo(DirectoryToZip).Name+".zip":ZipedPath+"\\"+ZipedFileName+".zip";
19
20using(System.IO.FileStreamZipFile=System.IO.File.Create(ZipFileName))
21{
22using(ZipOutputStreams=newZipOutputStream(ZipFile))
23{
24if(IsEncrypt)
25{
26//压缩文件加密
27s.Password=“123”;
28}
29ZipSetp(DirectoryToZip,s,"");
30}
31}
32}
33///<summary>
34///递归遍历目录
35///addyuangangby2016-06-13
36///</summary>
37privatestaticvoidZipSetp(stringstrDirectory,ZipOutputStreams,stringparentPath)
38{
39if(strDirectory[strDirectory.Length-1]!=Path.DirectorySeparatorChar)
40{
41strDirectory+=Path.DirectorySeparatorChar;
42}
43Crc32crc=newCrc32();
44
45string[]filenames=Directory.GetFileSystemEntries(strDirectory);
46
47foreach(stringfileinfilenames)//遍历所有的文件和目录
48{
49
50if(Directory.Exists(file))//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
51{
52stringpPath=parentPath;
53pPath+=file.Substring(file.LastIndexOf("\\")+1);
54pPath+="\\";
55ZipSetp(file,s,pPath);
56}
57
58else//否则直接压缩文件
59{
60//打开压缩文件
61using(FileStreamfs=File.OpenRead(file))
62{
63
64byte[]buffer=newbyte[fs.Length];
65fs.Read(buffer,0,buffer.Length);
66
67stringfileName=parentPath+file.Substring(file.LastIndexOf("\\")+1);
68ZipEntryentry=newZipEntry(fileName);
69
70entry.DateTime=DateTime.Now;
71entry.Size=fs.Length;
72
73fs.Close();
74
75crc.Reset();
76crc.Update(buffer);
77
78entry.Crc=crc.Value;
79s.PutNextEntry(entry);
80
81s.Write(buffer,0,buffer.Length);
82}
83}
84}
85}
五、解压一个ZIP文件
1///<summary>
2///ZIP:解压一个zip文件
3///addyuangangby2016-06-13
4///</summary>
5///<paramname="ZipFile">需要解压的Zip文件(绝对路径)</param>
6///<paramname="TargetDirectory">解压到的目录</param>
7///<paramname="Password">解压密码</param>
8///<paramname="OverWrite">是否覆盖已存在的文件</param>
9publicstaticvoidUnZip(stringZipFile,stringTargetDirectory,stringPassword,boolOverWrite=true)
10{
11//如果解压到的目录不存在,则报错
12if(!System.IO.Directory.Exists(TargetDirectory))
13{
14thrownewSystem.IO.FileNotFoundException("指定的目录:"+TargetDirectory+"不存在!");
15}
16//目录结尾
17if(!TargetDirectory.EndsWith("\\")){TargetDirectory=TargetDirectory+"\\";}
18
19using(ZipInputStreamzipfiles=newZipInputStream(File.OpenRead(ZipFile)))
20{
21zipfiles.Password=Password;
22ZipEntrytheEntry;
23
24while((theEntry=zipfiles.GetNextEntry())!=null)
25{
26stringdirectoryName="";
27stringpathToZip="";
28pathToZip=theEntry.Name;
29
30if(pathToZip!="")
31directoryName=Path.GetDirectoryName(pathToZip)+"\\";
32
33stringfileName=Path.GetFileName(pathToZip);
34
35Directory.CreateDirectory(TargetDirectory+directoryName);
36
37if(fileName!="")
38{
39if((File.Exists(TargetDirectory+directoryName+fileName)&&OverWrite)||(!File.Exists(TargetDirectory+directoryName+fileName)))
40{
41using(FileStreamstreamWriter=File.Create(TargetDirectory+directoryName+fileName))
42{
43intsize=2048;
44byte[]data=newbyte[2048];
45while(true)
46{
47size=zipfiles.Read(data,0,data.Length);
48
49if(size>0)
50streamWriter.Write(data,0,size);
51else
52break;
53}
54streamWriter.Close();
55}
56}
57}
58}
59
60zipfiles.Close();
61}
62}
zhe屎
相关文章推荐
- ICSharpCode.SharpZipLib.dll 压缩多文件
- C#/ASP.NET/WinForm 通过ICSharpCode.SharpZipLib.dll实现文件夹、文件的压缩与解压
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
- C#+ICSharpCode.SharpZipLib.dll实现压缩解压缩功能
- C# 压缩文件 ICSharpCode.SharpZipLib.dll
- ASP.NET生成压缩文件(rar打包)ICSharpCode.SharpZipLib.dll
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- 基于(ICSharpCode.SharpZipLib.dll)的文件压缩方法,类文件
- ICSharpCode.SharpZipLib.dll压缩的zip包,7zip解压时出错
- 使用 ICSharpCode.SharpZipLib.dll 压缩文件 方法实例
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- 压缩解压目录和文件--ICSharpCode.SharpZipLib.dll 算法 (转)
- ICSharpCode.SharpZipLib 实现文件压缩功能,可以压缩空文件夹
- ICSharpCode.SharpZipLib.dll 使用方法
- ICSharpCode.SharpZipLib 压缩
- ICSharpCode.SharpZipLib压缩解压
- C#文件压缩:ICSharpCode.SharpZipLib生成zip、tar、tar.gz
- ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用
- ICSharpCode.SharpZipLib实现压缩解压缩