压缩的Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace SharpZipLibUse
{
class Program
{
static void Main(string[] args)
{
FileStream zipFile = new FileStream("Demo.zip", FileMode.Create);
ZipOutputStream zos = new ZipOutputStream(zipFile);
DirectoryInfo di = new DirectoryInfo(".");
FileInfo[] files = di.GetFiles("*.txt");
byte[] buffer = new byte[10 * 1024];
foreach (FileInfo fi in files)
{
ZipEntry entry = new ZipEntry(fi.Name);
entry.Size = fi.Length;
zos.PutNextEntry(entry);
int length=0;
Stream input=fi.Open(FileMode.Open);
while ((length=input.Read(buffer,0,10*1024))>0)
{
zos.Write(buffer,0,length);
}
input.Close();
}
zos.Finish();
zos.Close();
Console.WriteLine("Ok!");
Console.Read();
}
}
}
运行结果如图:



解压的Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace ZipDemo
{
class Program
{
static void Main(string[] args)
{
ZipFile zf = new ZipFile("Program.zip");
foreach (ZipEntry entry in zf)
{
Console.WriteLine("压缩包内文件名:" + entry.Name);
Console.WriteLine("压缩包大小:" + entry.Size);
FileStream fs = new FileStream(entry.Name, FileMode.Create);
Stream input = zf.GetInputStream(entry);
byte[] buffer = new byte[10 * 1024];
int length = 0;
while ((length = input.Read(buffer, 0, 10 * 1024)) > 0)
{
fs.Write(buffer, 0, length);
}
fs.Close();
input.Close();
}
Console.Read();
}
}
}
运行结果如图:


