1.前言:
生產的資料日益龐大,舊的歷史資料需要進行壓縮以節省磁碟空間,當有需要查詢時,再透過程式解壓縮後讀取檔案。SharpZipLib是一個GPL License的開放原始碼元件,完全以C#程式開發,提供Zip, GZip, Tar及BZip2等壓縮方式。

2.說明:
SharpZipLib的最新軟體可由下列網址下載:
http://sharpziplib.com/
http://www.icsharpcode.net/opensource/sharpziplib/

本範例使用的版本為0.86.0
解壓縮軟體後,將\SharpZipLib_0860_Bin\net-20\ICSharpCode.SharpZipLib.dll的DLL檔複製到自己專案下的bin目錄
加入參考: ICSharpCode.SharpZipLib.dll
加入命名空間:

using ICSharpCode.SharpZipLib.Zip;

程式碼:

private void btZip_Click(object sender, EventArgs e)
{
	string path = @"d:\tmp\test";//要壓縮檔案的目錄路徑
	ZipFiles(path, string.Empty, string.Empty);
}

private void btUnzip_Click(object sender, EventArgs e)
{
	string path = @"d:\tmp\test1\test.zip";//壓縮檔案路徑
	UnZipFiles(path, string.Empty);
}

//讀取目錄下所有檔案
private static ArrayList GetFiles(string path)
{
	ArrayList files = new ArrayList();

	if (Directory.Exists(path))
	{
		files.AddRange(Directory.GetFiles(path));
	}

	return files;
}

//建立目錄
private static void CreateDirectory(string path)
{
	if (!Directory.Exists(path))
	{
		Directory.CreateDirectory(path);
	}
}


///
/// 壓縮檔案
///
///壓縮檔案路徑
///密碼
///註解
private void ZipFiles(string path, string password, string comment)
{
	ZipOutputStream zos = null;

	try
	{
		string zipPath = path + @"\" + Path.GetFileName(path) + ".zip";
		ArrayList files = GetFiles(path);
		zos = new ZipOutputStream(File.Create(zipPath));
		if (password != null && password != string.Empty) zos.Password = password;
		if (comment != null && comment != "") zos.SetComment(comment);
		zos.SetLevel(9);//Compression level 0-9 (9 is highest)
		byte[] buffer = new byte[4096];

		foreach (string f in files)
		{
			ZipEntry entry = new ZipEntry(Path.GetFileName(f));
			entry.DateTime = DateTime.Now;
			zos.PutNextEntry(entry);
			FileStream fs = File.OpenRead(f);
			int sourceBytes;

			do
			{
				sourceBytes = fs.Read(buffer, 0, buffer.Length);
				zos.Write(buffer, 0, sourceBytes);
			} while (sourceBytes > 0);

			fs.Close();
			fs.Dispose();
		}
	}

	catch (Exception ex)
	{
		Console.WriteLine(ex.Message);
	}

	finally
	{
		zos.Finish();
		zos.Close();
		zos.Dispose();
	}
}

///
/// 解壓縮檔案
///
///解壓縮檔案目錄路徑
///密碼
private void UnZipFiles(string path, string password)
{
	ZipInputStream zis = null;

	try
	{
		string unZipPath = path.Replace(".zip", "");
		CreateDirectory(unZipPath);
		zis = new ZipInputStream(File.OpenRead(path));
		if (password != null && password != string.Empty) zis.Password = password;
		ZipEntry entry;

		while ((entry = zis.GetNextEntry()) != null)
		{
			string filePath = unZipPath + @"\" + entry.Name;

			if (entry.Name != "")
			{
				FileStream fs = File.Create(filePath);
				int size = 2048;
				byte[] buffer = new byte[2048];
				while (true)
				{
					size = zis.Read(buffer, 0, buffer.Length);
					if (size > 0) { fs.Write(buffer, 0, size); }
					else { break; }
				}

				fs.Close();
				fs.Dispose();
			}
		}
	}

	catch (Exception ex)
	{
		Console.WriteLine(ex.Message);
	}

	finally
	{
		zis.Close();
		zis.Dispose();
	}
}
arrow
arrow

    西夏普 發表在 痞客邦 留言(2) 人氣()