您的位置:首页 > 编程语言 > C#

C#中用SharpZipLib生成gzip/解压文件

2015-10-26 10:40 543 查看
Code tells all:
using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Core;

namespace CNKIDataExport
{
class Program
{
public static void gZipFile(string filePath, string zipFilePath)
{
Stream s = new GZipOutputStream(File.Create(zipFilePath));
FileStream fs = File.OpenRead(filePath);
int size;
byte[] buf = new byte[4096];
do
{
size = fs.Read(buf, 0, buf.Length);
s.Write(buf, 0, size);
} while (size > 0);
s.Close();
fs.Close();
}

public static void gunZipFile(string zipFilePath, string filePath)
{
using (Stream inStream = new GZipInputStream(File.OpenRead(zipFilePath)))
using (FileStream outStream = File.Create(filePath))
{
byte[] buf = new byte[4096];
StreamUtils.Copy(inStream, outStream, buf);
}
}

static void Main(string[] args)
{
string src = @"D:\test\in.txt"
string dest = @"D:\test\out.gz"
string ori = @"D:\test\ori.txt"

gZipFile(src, dest);
Console.WriteLine("gzip over!");
gunZipFile(dest, ori);
Console.WriteLine("gunzip over!");
Console.ReadKey();
}
}
}


相关链接:
1、SharpZipLib下载
2、Using SharpZipLib to gzip a file
3、ICSharpCode.SharpZipLib.GZip.GZipInputStream Class Reference
4、C#利用SharpZipLib解压或压缩文件夹实例操作(ZIP格式)

*** walker ***
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 压缩 gzip