老代碼 C# 解壓壓縮Zip檔案 留作珍藏~~

來源:互聯網
上載者:User

小記:這套代碼還是有點小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";
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.