小記:這套代碼還是有點小bug的,只好用湊合用吧~~ bug1:當有重複檔案的時候,會在伺服器彈出是否覆蓋的視窗,所以,解壓的時候,切勿重名目錄。bug2:就是壓縮的時候,同理,也不要有重名的。還有就是,開始的時候,自己建立一個zip,然後copy一個出來,再去添加到裡面,整體思路就是這樣,反正能湊合用。呵呵~~
代碼如下。。也是從網上copy的,然後自己改改~~
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
public string doSay = "err";
protected void Page_Load(object sender, EventArgs e)
{
doSay = Server.MapPath("zzcn.net.zip");
}
protected void Button1_Click(object sender, EventArgs e)
{
string err;
if (UnZipFile(Server.MapPath("zzcn.net.zip"), Server.MapPath("zzcn.net"), out err))
{
doSay = "解壓成功!";
}
}
#region 解壓zip格式的檔案
/// <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.Length == 0)
{
err = "壓縮檔不可為空!";
return false;
}
else if (!zipFilePath.EndsWith(".zip"))
{
err = "檔案格式不正確!";
return false;
}
else if (!System.IO.File.Exists(zipFilePath))
{
err = "壓縮檔不存在!";
return false;
}
//解壓檔案夾為空白時預設與壓縮檔同一級目錄下,跟壓縮檔同名的檔案夾
if (unZipDir.Length == 0)
unZipDir = zipFilePath.Replace(System.IO.Path.GetFileName(zipFilePath), System.IO.Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!System.IO.Directory.Exists(unZipDir))
System.IO.Directory.CreateDirectory(unZipDir);
try
{
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFolder = sc.NameSpace(zipFilePath);
Shell32.Folder DestFolder = sc.NameSpace(unZipDir);
Shell32.FolderItems items = SrcFolder.Items();
DestFolder.CopyHere(items, 20);
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}//解壓結束
#endregion
public bool BuildFrame(string srcFile, string destFolder)
{
try
{
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFolder = sc.NameSpace(srcFile);
Shell32.Folder DestFolder = sc.NameSpace(destFolder);
Shell32.FolderItems items = SrcFolder.Items();
DestFolder.CopyHere(items, 20);
return true;
}
catch
{
return false;
}
}
public bool YaSuo(string DeskFolder, string objFile)
{
try
{
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFolder = sc.NameSpace(DeskFolder);
Shell32.Folder DestFolder = sc.NameSpace(objFile);
Shell32.FolderItems items = SrcFolder.Items();
DestFolder.CopyHere(items, 20);
return true;
}
catch
{
return false;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (YaSuo(Server.MapPath("zz"), Server.MapPath("zz1.zip")))
{
doSay = "壓縮完畢!歡迎訪問 t.sina.com.cn/pengchenggang";
}
}
}