天天看點

C#使用ICSharpCode.SharpZipLib壓縮檔案[轉]

一直以來都是采用winzip進行壓縮的,調用起來友善,而且公司也有版權,是以就沒有考慮過其他的東東。不過唯一不足的地方就是需要安裝(包括win zip和其command line addon),而且需要讓程式知道調用的winzip路徑,配置起來不是很友善。

本次項目,考慮到程式的易配置性,決定采用另外别的方式進行壓縮,找了找就找到了icsharpcode的sharpziplib元件,開源的,并且功能很強大:壓縮、解壓縮、加密等等一系列功能都有,而且調用起來也蠻友善的,于是決定采用該元件了。

同樣,為了能夠更好的為項目服務,也對該元件作了封裝,調用更加簡單。

using system;

using system.io;

using system.collections;

using icsharpcode.sharpziplib.zip;

using icsharpcode.sharpziplib.checksums;

using icsharpcode.sharpziplib.gzip;

namespace benq.modias.utility

...{

    /**////<summary>

    ///******************************************************************

    ///**  creator     : venus feng 

    ///**  create date : 2006-9-19 16:21

    ///**  modifier    : 

    ///**  modify date : 

    ///**  description : use icsharpziplib to zip file

    ///** 

    ///**  version no  : 1.0.0

    ///</summary>

    public class zipper : idisposable

    ...{

        private string m_foldertozip;

        private arraylist m_filenamestozip;

        private string m_filenamezipped;

        private zipoutputstream m_zipstream = null;

        private crc32 m_crc;

        begin for public properties#region begin for public properties

        /**//// <summary>

        /// folder name to zip

        /// like "c:test"

        /// </summary>

        public  string foldertozip

        ...{

            get ...{ return m_foldertozip; }

            set ...{ m_foldertozip = value; }

        }

        /// file name to zip

        /// like "c:testtest.txt"

        public  arraylist filenamestozip

            get ...{ return m_filenamestozip; }

            set ...{ m_filenamestozip = value; }

        /// zipped file name 

        /// like "c:testmyzipfile.zip"

        public  string filenamezipped

            get ...{ return m_filenamezipped; }

            set ...{ m_filenamezipped = value; }

        #endregion

        /// the construct

        public zipper()

            this.m_foldertozip = "";

            this.m_filenamestozip = new arraylist();

            this.m_filenamezipped = "";

        zipfolder#region zipfolder

        /// zip one folder : single level

        /// before doing this event, you must set the folder and the zip file name you want

        public void zipfolder()

            if (this.m_foldertozip.trim().length == 0)

            ...{

                throw new exception("you must setup the folder name you want to zip!");

            }

            if(directory.exists(this.m_foldertozip) == false)

                throw new exception("the folder you input does not exist! please check it!");

            if (this.m_filenamezipped.trim().length == 0)

                throw new exception("you must setup the zipped file name!");

            string[] filenames = directory.getfiles(this.m_foldertozip.trim());

            if (filenames.length == 0)

                throw new exception("can not find any file in this folder(" + this.m_foldertozip + ")!");

            // create the zip file

            this.createzipfile(this.m_filenamezipped);

            // zip all files

            foreach(string file in filenames)

                this.zipsinglefile(file);

            // close the zip file

            this.closezipfile();

        zipfiles#region zipfiles

        /// zip files

        /// before doing this event, you must set the files name and the zip file name you want

        public void zipfiles()

            if (this.m_filenamestozip.count == 0)

                throw new exception("you must setup the files name you want to zip!");

            foreach(object file in this.m_filenamestozip)

                if(file.exists(((string)file).trim()) == false)

                ...{

                    throw new exception("the file(" + (string)file + ") you input does not exist! please check it!");

                }

                throw new exception("you must input the zipped file name!");

            // zip this file

                this.zipsinglefile((string)file);

        createzipfile#region createzipfile

        /// create zip file by filenamezipped

        /// <param name="filenamezipped">zipped file name like "c:testmyzipfile.zip"</param>

        private void createzipfile(string filenamezipped)

            this.m_crc = new crc32();

            this.m_zipstream = new zipoutputstream(file.create(filenamezipped));

            this.m_zipstream.setlevel(6); // 0 - store only to 9 - means best compression

        closezipfile#region closezipfile

        /// close the zip file

        private void closezipfile()

            this.m_zipstream.finish();

            this.m_zipstream.close();

            this.m_zipstream = null;

        zipsinglefile#region zipsinglefile

        /// zip single file 

        /// <param name="filename">file name like "c:testtest.txt"</param>

        private void zipsinglefile(string filenametozip)

            // open and read this file

            filestream fso = file.openread(filenametozip);

            // read this file to buffer

            byte[] buffer = new byte[fso.length];

            fso.read(buffer,0,buffer.length);

            // create a new zipentry

            zipentry zipentry = new zipentry(filenametozip.split('\')[filenametozip.split('\').length - 1]);

            zipentry.datetime = datetime.now;

            // set size and the crc, because the information

            // about the size and crc should be stored in the header

            // if it is not set it is automatically written in the footer.

            // (in this case size == crc == -1 in the header)

            // some zip programs have problems with zip files that don't store

            // the size and crc in the header.

            zipentry.size = fso.length;

            fso.close();

            fso = null;

            // using crc to format the buffer

            this.m_crc.reset();

            this.m_crc.update(buffer);

            zipentry.crc = this.m_crc.value;

            // add this zipentry to the zipstream

            this.m_zipstream.putnextentry(zipentry);

            this.m_zipstream.write(buffer,0,buffer.length);

        idisposable member#region idisposable member

        /// release all objects

        public void dispose()

            if(this.m_zipstream != null)

                this.m_zipstream.close();

                this.m_zipstream = null;

    }

}

歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/archive/2009/07/08/1519502.html