Packaging files with Icsharpcode compression

Source: Internet
Author: User
Tags count datetime parent directory zip

Because the project needs to package the file, at the colleague's suggestion to write a packing function with Icsharpcode. Icsharpcode's introduction will not say. Please go to the official website http://www.icsharpcode.net/.

First quote ICSharpCode.SharpZipLib.dll, not downloaded here: Http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar

Code implementation of multiple files, custom files, whole directory packaging and other functions. All right. The code:

Compress Packaging Code

<summary>///zips the specified zip path. </summary>///<param name= "Strzippath" >the zip path.</param>///<param name= "strziptopd Irectorypath ">the zip top directory path.</param>///<param name=" intziplevel ">the zip Level.</para m>///<param name= "strpassword" >the password.</param>,///<param name= "FilesOrDirectoriesPaths" >the files or directories paths.</param>///<returns></returns> private bool Zip (string Strz
        Ippath, 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 < file Sordirectoriespaths.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 (1);
                 }           ZipEntry entry = new ZipEntry (strFileName); Entry.
                            DateTime = DateTime.Now;
                            Zipoutputstream.putnextentry (entry); Zipoutputstream.write (buffer, 0, buffer.
                            Length); Fs.
                            Close (); Fs.
                        Dispose ();
                    The catch {continue;
                } zipoutputstream.finish ();
                Zipoutputstream.close ();
            return true;
            else {return false;
        catch {return false;
    }///<summary>///Gets the directory files. </summary>///<param name= "Strparentdirectorypath" >the parent directory path.</param>///& Lt;param name= "Allfilespath"; All Files path.</param> private void Getdirectoryfiles (String strparentdirectorypath, list<string> AllFile
        spath) {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); }
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.