添加引用,添加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);
//创建一个 GZipStream 对象,Compress 表示压缩基础流。
GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);
//实现一个 write,使其以一种特定的编码向流中写入字符。
StreamWriter writer = new StreamWriter(compressionStream);
writer.Write(data);
writer.Close();
}
static string LoadCompressedFile(string filename)
{
//创建一个 FileStream 对象
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
//创建一个 GZipStream 对象,Decompress 表示解压缩基础流。
GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress);
//实现从字符串进行读取
StreamReader reader = new StreamReader(compressionStream);
string data = reader.ReadToEnd();
reader.Close();
return data;
}
public static void Main(string[] args)
{
try
{
//定义文件路径
string filename = @"c:\compressedFile.txt";
Console.WriteLine("Enter a string to compress (will be repeated 100 times):");
//再控制台上输入文字
string sourceString = Console.ReadLine();
StringBuilder sourceStringMultiplier = new StringBuilder(sourceString.Length * 100);
for (int i = 0; i < 100; i++)
{
sourceStringMultiplier.Append(sourceString);
}
sourceString = sourceStringMultiplier.ToString();
Console.WriteLine("Source data is {0} bytes long.", sourceString.Length);
SaveCompressedFile(filename, sourceString);
Console.WriteLine("\nData saved to {0}.", filename);
FileInfo compressedFileData = new FileInfo(filename);
Console.WriteLine("Compressed file is {0} bytes long.", compressedFileData.Length);
string recoveredString = LoadCompressedFile(filename);
recoveredString = recoveredString.Substring(0, recoveredString.Length / 100);
Console.WriteLine("\nRecovered data: {0}", recoveredString);
Console.ReadKey();
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
}
//***************EndCode*************************************
//***************CodeTest*************************************
//***************EndCode*************************************