C # using Icsharpcode for file compression implementation

Source: Internet
Author: User

As described in this article for a C # using Icsharpcode compression of the use of the class, the test results are good. Share it for everyone's reference. Here's how:


1. Parameter classes

Using system;using system.collections.generic;using system.linq;using system.text;namespace ZipCompress{public  Class Zipparameter  {    private string zip_name = "";    private string zip_directoryname = "";    Private list<string> zip_filelist = new list<string> ();    <summary>///    Compressed file name///    </summary> public    string zipname    {      get {return zip_ Name; }      set {zip_name = value;}    }    <summary>///    Compressed file path///    </summary> public    string zipdirectoryname    {      get { return zip_directoryname; }      set {zip_directoryname = value;}    }    <summary>///    Compressed file list///    </summary> public    list<string> zipfilelist    {      get {return zip_filelist;}      set {zip_filelist = value;}}}}  


2. Working class

Function: Implement file compression// How to: Set Parameters for compression//*****************************************************************************************using System;using icsharpcode.sharpziplib.zip;using system.io;using system.text;namespace ZipCompress{public class Compressfile {//<summary>///Compressed file parameters///</summary> public zipparameter zipparameter {get; SE T }///<summary>///Compressed file returns compressed information///</summary>//<returns>string Returns the message after compression &LT;/RETURNS&G    T      public string compressreturnmsg () {FileStream zip_file;      Zipoutputstream ZipStream;      ZipEntry ZipEntry; String rtnmessage = "";//Returns the information of the try {//looping file, if the file does not exist on the compression not added inside for (int i = 0; i < ZipParameter.ZIPFileList.Count; i++) {if (!            File.exists (Zipparameter.zipfilelist[i])) {ZipParameter.ZIPFileList.RemoveAt (i);          i--; }        }//There is no file below the compression does not execute if (ZipParameter.ZIPFileList.Count = = 0) {return "File not find"        ; }//There is no directory to create if (! Directory.Exists (Zipparameter.zipdirectoryname)) {Directory.CreateDirectory (zipparameter.zipdirectoryname        );        }//solve the document name garbled problem, garbled is because codepage not Encoding GBK = encoding.getencoding ("GBK"); ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = GBK.        CodePage;        File path, document path and file name string strpath = Zipparameter.zipdirectoryname + zipparameter.zipname;        Zip_file = File.create (strpath);        ZipStream = new Zipoutputstream (zip_file);          foreach (String filetozip in zipparameter.zipfilelist) {zip_file = File.openread (Filetozip);          byte[] buffer = new Byte[zip_file.length]; Zip_file.read (buffer, 0, buffer.)          Length);          Zip_file.close ();          ZipEntry = new ZipEntry (Path.getfilename (filetozip)); Zipstream.putnextentry(ZipEntry); Zipstream.write (buffer, 0, buffer.)        Length);        } zipstream.finish ();        Zipstream.close ();        Zip_file.close ();      Rtnmessage = "Success"; } catch (Exception ex) {rtnmessage = "fail:" + ex.      Message; } finally {GC.        Collect (); Gc.      Collect (1);    } return rtnmessage; }  }}


3. Using the class

Zipparameter ZP = new Zipparameter (); ZP. Zipdirectoryname = @ "C:\Users\Public\Pictures\Sample pictures\"; ZP. Zipname = "Test.zip"; ZP. Zipfilelist.add (@ "C:\Users\Public\Pictures\Sample pictures\chrysanthemum.jpg"); ZP. Zipfilelist.add (@ "C:\Users\Public\Pictures\Sample pictures\desert.jpg"); ZP. Zipfilelist.add (@ "C:\Users\Public\Pictures\Sample pictures\ error file. jpg"); Compressfile cprfile = new Compressfile (); cprfile.zipparameter = zp;string strmessage = cprfile.compressreturnmsg ();

4. File source Point this

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # using Icsharpcode for file compression implementation

This address: http://www.paobuke.com/develop/c-develop/pbk23576.html






Related content C # method of removing numbers or non-numbers from a string based on regular expressions ASP. How to get the current time of the system in detail c#.net programming methods for creating Access files and Excel files DataBindings Usage Example analysis in C #
A typical flaw in C # for beginners in C # simple way to generate barcode images from embedded x86 assembler C # by Inline-asm C # split screen control usage instance

C # using Icsharpcode for file compression implementation

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.