asp.net c# ZIP檔案壓縮與解壓

來源:互聯網
上載者:User

asp教程.net c# zip檔案壓縮與解壓

using system;
using system.collections.generic;
using system.text;
using system.io;
using icsharpcode.sharpziplib.zip;
using system.diagnostics;
using icsharpcode.sharpziplib.core;

namespace testconsole
{
    class program
    {
        static void main()
        {
            //createzipfile(@"d:", @"d:a.zip");
            unzipfile(@"d:a.zip");

            console.read();
        }

        private static void createzipfile(string filespath, string zipfilepath)
        {

            if (!directory.exists(filespath))
            {
                console.writeline("cannot find directory '{0}'", filespath);
                return;
            }

            try
            {
                string[] filenames = directory.getfiles(filespath);
                using (zipoutputstream s = new zipoutputstream(file.create(zipfilepath)))
                {

                    s.setlevel(9); // 壓縮層級 0-9
                    //s.password = "123"; //zip壓縮檔密碼
                    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)
            {
                console.writeline("exception during processing {0}", ex);
            }
        }

        private static void unzipfile( string zipfilepath)
        {
            if (!file.exists(zipfilepath))
            {
                console.writeline("cannot find file '{0}'", zipfilepath);
                return;
            }

            using (zipinputstream s = new zipinputstream(file.openread(zipfilepath)))
            {

                zipentry theentry;
                while ((theentry = s.getnextentry()) != null)
                {

                    console.writeline(theentry.name);

                    string directoryname = path.getdirectoryname(theentry.name);
                    string filename = path.getfilename(theentry.name);

                    // create directory
                    if (directoryname.length > 0)
                    {
                        directory.createdirectory(directoryname);
                    }

                    if (filename != string.empty)
                    {
                        using (filestream streamwriter = file.create(theentry.name))
                        {

                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.read(data, 0, data.length);
                                if (size > 0)
                                {
                                    streamwriter.write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}

雖然網上很多流行的壓縮檔格式都是rar的,但是由於rar不是一個開放的標準,因此zip成了更多人的選擇。如果你不想自己開發的話可以選擇開源的項目,比如sharpziplib就是一個不錯的選擇

聯繫我們

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