小记:这套代码还是有点小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"; } } }
|
请发表评论