日期:2014-05-17  浏览次数:21064 次

C#处理压缩文件

添加引用,添加SharpZipLib.dll

SharpZipLib.dll 的下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/

//***************CodeTest*************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

class Test
{
??? //This is the method of zip:
??? //SrcFile 源文件路径,DstFile 压缩后目标文件的路径,BufferSize 缓冲期大小
??? public static void Zip(string SrcFile, string DstFile, int BufferSize)
??? {
??????? FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
??????? FileStream fileStreamOut = new FileStream(DstFile, FileMode.Create, FileAccess.Write);
??????? ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
??????? byte[] buffer = new byte[BufferSize];
??????? ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
??????? zipOutStream.PutNextEntry(entry);
??????? int size;
??????? do
??????? {
??????????? size = fileStreamIn.Read(buffer, 0, buffer.Length);
??????????? zipOutStream.Write(buffer, 0, size);
??????? } while (size > 0);
??????? zipOutStream.Close();
??????? fileStreamOut.Close();
??????? fileStreamIn.Close();
??? }
??? //This is the method of unzip:
??? //SrcFile 源文件路径,DstFile 解压缩后目标文件的路径,BufferSize 缓冲期大小
??? public static void UnZip(string SrcFile, string DstFile, int BufferSize)
??? {
??????? FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
??????? ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
??????? ZipEntry entry = zipInStream.GetNextEntry();
??????? FileStream fileStreamOut = new FileStream(DstFile, FileMode.Create, FileAccess.Write);
??????? int size;
??????? byte[] buffer = new byte[BufferSize];
??????? do
??????? {
??????????? size = zipInStream.Read(buffer, 0, buffer.Length);
??????????? fileStreamOut.Write(buffer, 0, size);
??????? } while (size > 0);
??????? zipInStream.Close();
??????? fileStreamOut.Close();
??????? fileStreamIn.Close();
??? }

??? public static void Main(string[] args)
??? {
??????? try
??????? {
??????????? //定义文件路径
??????????? string SrcFile = @"C:\temp\MyTest.txt";
??????????? string DstFile = @"C:\temp\MyTest.zip";
??????????? //默认缓冲器字节大小
??????????? int BufferSize = 4096;
??????????? //压缩文件,将 SrcFile 文件压缩为 DstFile 文件
??????????? Zip(SrcFile, DstFile, BufferSize);
??????????? //解压缩文件,将压缩文件 DstFile 解压为 SrcFile 文件
??????????? //UnZip(DstFile, SrcFile, BufferSize);
??????? }
??????? catch (IOException ex)
??????? {
??????????? Console.WriteLine("An IO exception has been thrown!");
??????????? Console.WriteLine(ex.ToString());
??????????? Console.ReadKey();
??????? }
??? }
}

//***************EndCode*************************************

GZipStream 类:提供用于压缩和解压缩流的方法和属性。

//***************CodeTest************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;//使用压缩类

class Test
{
??? static void SaveCompressedFile(string filename, string data)
??? {
??????? //创建一个 FileStream 对象
??????? FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);