ASP.NET產生壓縮檔(rar打包)

來源:互聯網
上載者:User

首先引用ICSharpCode.SharpZipLib.dll,沒有在這裡下載:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar

壓縮打包代碼

/// <summary>
    /// 產生壓縮檔
    /// </summary>
    /// <param name="strZipPath">產生的zip檔案的路徑</param>
    /// <param name="strZipTopDirectoryPath">源檔案的上級目錄</param>
    /// <param name="intZipLevel">T壓縮等級</param>
    /// <param name="strPassword">壓縮包解壓密碼</param>
    /// <param name="filesOrDirectoriesPaths">源檔案路徑</param>
    /// <returns></returns>
    private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
    {
        try
        {
            List<string> AllFilesPath = new List<string>();
            if (filesOrDirectoriesPaths.Length > 0) // get all files path
            {
                for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
                {
                    if (File.Exists(filesOrDirectoriesPaths[i]))
                    {
                        AllFilesPath.Add(filesOrDirectoriesPaths[i]);
                    }
                    else if (Directory.Exists(filesOrDirectoriesPaths[i]))
                    {
                        GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
                    }
                }
            }

            if (AllFilesPath.Count > 0)
            {

                ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
                zipOutputStream.SetLevel(intZipLevel);
                zipOutputStream.Password = strPassword;

                for (int i = 0; i < AllFilesPath.Count; i++)
                {
                    string strFile = AllFilesPath[i].ToString();
                    try
                    {
                        if (strFile.Substring(strFile.Length - 1) == "") //folder
                        {
                            string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                            if (strFileName.StartsWith(""))
                            {
                                strFileName = strFileName.Substring(1);
                            }
                            ZipEntry entry = new ZipEntry(strFileName);
                            entry.DateTime = DateTime.Now;
                            zipOutputStream.PutNextEntry(entry);
                        }
                        else //file
                        {
                            FileStream fs = File.OpenRead(strFile);

                            byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);

                            string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                            if (strFileName.StartsWith(""))
                            {
                                strFileName = strFileName.Substring(0);
                            }
                            ZipEntry entry = new ZipEntry(strFileName);
                            entry.DateTime = DateTime.Now;
                            zipOutputStream.PutNextEntry(entry);
                            zipOutputStream.Write(buffer, 0, buffer.Length);

                            fs.Close();
                            fs.Dispose();
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }

                zipOutputStream.Finish();
                zipOutputStream.Close();

                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// Gets the directory files.
    /// </summary>
    /// <param name="strParentDirectoryPath">源檔案路徑</param>
    /// <param name="AllFilesPath">所有檔案路徑</param>
    private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
    {
        string[] files = Directory.GetFiles(strParentDirectoryPath);
        for (int i = 0; i < files.Length; i++)
        {
            AllFilesPath.Add(files[i]);
        }
        string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
        for (int i = 0; i < directorys.Length; i++)
        {
            GetDirectoryFiles(directorys[i], AllFilesPath);
        }
        if (files.Length == 0 && directorys.Length == 0) //empty folder
        {
            AllFilesPath.Add(strParentDirectoryPath);
        }
    }

 

 

調用

string strZipPath = @"D:\ConsultSystem\mgr\user.zip";
        string strZipTopDirectoryPath = @"D:\ConsultSystem\mgr\";
        int intZipLevel = 6;
        string strPassword = "";
        string[] filesOrDirectoriesPaths = new string[] { @"D:\ConsultSystem\mgr\user.txt" };
        Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.