using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip; //開源工具,可免費下載::
//http://files.cnblogs.com/xiaowei0705/SharpZipLib_0860_Bin.zip
using System.IO;
namespace Package
{
class Class1
{
#region 加壓解壓方法
/// <summary>
/// 功能:壓縮檔(暫時只壓縮檔夾下一級目錄中的檔案,檔案夾及其子級被忽略)
/// </summary>
/// <param name="dirPath">被壓縮的檔案夾夾路徑</param>
/// <param name="zipFilePath">產生壓縮檔的路徑,為空白則預設與被壓縮檔夾同一級目錄,名稱為:檔案夾名+.zip</param>
/// <param name="err">出錯資訊</param>
/// <returns>是否壓縮成功</returns>
public bool ZipFile(string dirPath, string zipFilePath, out string err)
{
err = "";
if (dirPath == string.Empty)
{
err = "要壓縮的檔案夾不可為空!";
return false;
}
if (!Directory.Exists(dirPath))
{
err = "要壓縮的檔案夾不存在!";
return false;
}
//壓縮檔名為空白時使用檔案夾名+.zip
if (zipFilePath == string.Empty)
{
if (dirPath.EndsWith("\\"))
{
dirPath = dirPath.Substring(0, dirPath.Length - 1);
}
zipFilePath = dirPath + ".zip";
}
try
{
string[] filenames = Directory.GetFiles(dirPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}
/// <summary>
/// 功能:解壓zip格式的檔案。
/// </summary>
/// <param name="zipFilePath">壓縮檔路徑</param>
/// <param name="unZipDir">解壓檔案存放路徑,為空白時預設與壓縮檔同一級目錄下,跟壓縮檔同名的檔案夾</param>
/// <param name="err">出錯資訊</param>
/// <returns>解壓是否成功</returns>
public bool UnZipFile(string zipFilePath, string unZipDir, out string err)
{
err = "";
if (zipFilePath == string.Empty)
{
err = "壓縮檔不可為空!";
return false;
}
if (!File.Exists(zipFilePath))
{
err = "壓縮檔不存在!";
return false;
}
//解壓檔案夾為空白時預設與壓縮檔同一級目錄下,跟壓縮檔同名的檔案夾
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("\\"))
directoryName += "\\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + 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;
}
}
}
}
}//while
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}//解壓結束
#endregion
}
}