下文件的压缩和解压缩 NET
墨初 知识笔记 47阅读
经常遇到文件压缩和解压缩时,打开Windows应用程序。NET或ASP.NET,但在微软框架1.1,没有相关的类库。这里引用ICSharpCode,可以快速达到相关目的。这个类库是用C#开发的,是开源的。感兴趣的朋友可以从这里下载。1.文件的压缩将下载的ICSharpCode.SharpZipLib.dll添加到引用中,使用ZipClass系统(http://www . Sina.com/)创建了一个新类;使用系统。木卫一;使用ICSharpCode。夏普齐普里布。ZipNamespacejohnsolution.com mon {/* *//summary//zip类文件压缩类///summary public ' color : rgba(0,0,255,1)' class zipclass { publicviltstartzip(){//要压缩的文件目录string[];=目录。get files(@ ' e : \ test ');//生成的文件路径zipoutputstreams=newzipoutputstream((
n style="color: rgba(0, 0, 0, 1)">@"e:\test.zip"));s.SetLevel(5);
foreach (string ;in )
{
;= (file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
}
}
1.文件的解压
将下载的 ICSharpCode.SharpZipLib.dll 添加到引用,新建一个 UnZipClass 的类(UnZipClass.cs)
using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
namespace JohnSolution.Common
{
/// <summary>
/// UnZipClass 解压文件类
/// </summary>
public class UnZipClass
{
public void StartUnZip()
{
ZipInputStream s = new ZipInputStream((@"e:\test.zip"));
ZipEntry entry;
while ((entry = s.GetNextEntry()) != null)
{
int size = 2048;
byte[] buffer = new byte[2048];
;= (@"e:\" + entry.Name);
while (true)
{
size = s.Read(buffer, 0, buffer.Length);
if (size > 0)
{
unZip(buffer,0,size);
}
else
{
break;
}
}
unZip();
unZip();
}
s.Close();
}
}
}
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
namespace JohnSolution.Common
{
/// <summary>
/// UnZipClass 解压文件类
/// </summary>
public class UnZipClass
{
public void StartUnZip()
{
ZipInputStream s = new ZipInputStream((@"e:\test.zip"));
ZipEntry entry;
while ((entry = s.GetNextEntry()) != null)
{
int size = 2048;
byte[] buffer = new byte[2048];
;= (@"e:\" + entry.Name);
while (true)
{
size = s.Read(buffer, 0, buffer.Length);
if (size > 0)
{
unZip(buffer,0,size);
}
else
{
break;
}
}
unZip();
unZip();
}
s.Close();
}
}
}

标签: