C#壓縮檔夾坑~

來源:互聯網
上載者:User

標籤:cep   ini   []   while   引用   exce   name   div   stream   

dotNet瘋狂之路No.29

 今天很殘酷,明天更殘酷,後天很美好,但是絕大部分人是死在明天晚上,只有那些真正的英雄才能見到後天的太陽。

 We‘re here to put a dent in the universe. Otherwise why else even be here?  

C#壓縮檔夾坑~    

開始從網上找了個壓縮的樣本  我去坑的不要不要的 沒辦法重新找 都是複製來複製去 沒啥意思

前提:ICSharpCode.SharpZipLib.dll引用

建立一個類

 

public class ZipClass{    /// <summary>    /// 功能:壓縮檔(暫時只壓縮檔夾下一級目錄中的檔案,檔案夾及其子級被忽略)    /// </summary>    /// <param name="dirPath">被壓縮的檔案夾夾路徑</param>    /// <param name="zipFilePath">產生壓縮檔的路徑</param>     /// <returns>是否壓縮成功</returns>    public static bool ZipFiles(string dirPath, string zipFilePath )    {                             try        {            string[] filenames = Directory.GetFiles(dirPath);            using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))            {                s.SetLevel(9);//0-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)        {                       return false;        }        return true;    }     }

 

  

 

C#壓縮檔夾坑~

聯繫我們

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