項(xiàng)目中用到壓縮和解壓的功能,在網(wǎng)上查查看,有如下幾種,整理一下,供參考
一 : 引用第三方的插件實(shí)現(xiàn)
在引用中添加ICSharpCode.SharpZipLib.dll文件
可從www.icsharpcode.net/Opensource/SharpZipLib/Download.aspx獲得
需要添加以下引用
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
對(duì)文件壓縮的方法如下:
// 對(duì)目標(biāo)文件夾進(jìn)行壓縮,將壓縮結(jié)果保存為指定文件
//FilePath存放的是目標(biāo)文件夾下要壓縮的文件BakPath 壓縮后文件存放的路徑
public class ZipClass
{
public ArrayList Zip(string FilePath, string BakPath)
{
// 新建一個(gè)list數(shù)組,用于存儲(chǔ)壓縮后的文件的名字
ArrayList list = new ArrayList();
try
{string[] filenames = Directory.GetFiles(FilePath);
list.Add(filenames);
Crc32 crc = new Crc32();//新建Checksums的Crc32類(lèi)對(duì)象ZipOutputStream ZipStream = new ZipOutputStream(File.Create(BakPath));
foreach (string file in filenames)
{
//打開(kāi)要壓縮的文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);//獲取壓縮文件的相對(duì)路徑
string files = file.Substring(file.LastIndexOf("\\"));
ZipEntry entry = new ZipEntry(files);entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
ZipStream.PutNextEntry(entry);
ZipStream.Write(buffer, 0, buffer.Length);
}
ZipStream.Finish();
ZipStream.Close();
return list;
}
catch (Exception ee)
{
throw ee;
}
}
在解壓縮類(lèi)中添加如下引用
using ICSharpCode.SharpZipLib.Zip;
對(duì)文件的解壓縮的方法如下:
public void UnZip(string[] args)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name);
//生成解壓目錄
Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
{
//解壓文件到指定的目錄
FileStream streamWriter = File.Create(args[1] + theEntry.Name);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
Else { break; }
}
streamWriter.Close();
}
}
s.Close();
}
二. 用 Net 2.0中的自帶的GZipStream類(lèi)
在壓縮和解壓縮的時(shí)候只要添加如下引用
using System.IO.Compression;
對(duì)文件壓縮方法如下:
public void CompressFile(string sourceFile, string destinationFile)
{
if (File.Exists(sourceFile) == false) //判斷文件是否存在
throw new FileNotFoundException();
//創(chuàng)建文件流和字節(jié)數(shù)組
byte[] buffer = null;
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream compressedStream = null;
try
{
sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[sourceStream.Length];
//把文件流存放到字節(jié)數(shù)組中
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length)
{
throw new ApplicationException();
}
destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);
//創(chuàng)建GzipStream實(shí)例,寫(xiě)入壓縮的文件流
compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);
compressedStream.Write(buffer, 0, buffer.Length);
}
catch (ApplicationException ex)
{
MessageBox.Show(ex.Message, "壓縮文件時(shí)發(fā)生錯(cuò)誤:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Make sure we allways close all streams
if (sourceStream != null)
{ sourceStream.Close();}
if (compressedStream != null)
{ compressedStream.Close();}
if (destinationStream != null)
{ destinationStream.Close();}
}
}
解壓縮方法如下:
public void DecompressFile(string sourceFile, string destinationFile)
{
if (File.Exists(sourceFile) == false)
throw new FileNotFoundException();
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream decompressedStream = null;
byte[] quartetBuffer = null;
try
{
sourceStream = new FileStream(sourceFile, FileMode.Open);
decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
quartetBuffer = new byte[4];
int position = (int)sourceStream.Length - 4;
sourceStream.Position = position;
sourceStream.Read(quartetBuffer, 0, 4);
sourceStream.Position = 0;
int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
byte[] buffer = new byte[checkLength + 100];
int offset = 0;
int total = 0;
// 從字節(jié)數(shù)組中讀取壓縮數(shù)據(jù)
while (true)
{
int bytesRead = decompressedStream.Read(buffer, offset, 100);
if (bytesRead == 0) break;
offset += bytesRead;
total += bytesRead;
}
destinationStream = new FileStream(destinationFile, FileMode.Create);
destinationStream.Write(buffer, 0, total);
destinationStream.Flush();
}
catch (ApplicationException ex)
{
MessageBox.Show(ex.Message, "解壓文件時(shí)發(fā)生錯(cuò)誤:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (sourceStream != null)
{ sourceStream.Close();}
if (decompressedStream != null)
{ decompressedStream.Close();}
if (destinationStream != null)
{ destinationStream.Close();}
}
}
比較:
引用 SharpZipLib的方法通用性比較強(qiáng),能壓縮和對(duì)別的解壓文件進(jìn)行解壓;
用GzipStream類(lèi)對(duì)文件進(jìn)行壓縮和解壓比較方便,但是其只能對(duì)自己的壓縮文件進(jìn)行解壓,對(duì)常見(jiàn)的壓縮格式不支持
粗略的總結(jié)了一下,有錯(cuò)誤的地方還望多多指出. ^_^
聯(lián)系客服