C # Use sharpziplib for zip Compression

Source: Internet
Author: User

Use the icsharpcode. sharpziplib, an open-source external pressurization Library, to implement pressurization. The official website of this library is
Http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

User Experience: simple pressurization and decompression can be performed according to the example. All files in a folder can be pressurized, but the instructions for pressurization subfolders are not provided.
At present, some codes on the Internet cannot pressurize empty folders, and some cannot be unzipped by rar, which requires some improvements.
However, if you only need to pressurize the "file" (excluding folders and subdirectories) in the first-level subdirectory of the folder, It is very convenient to use this library. It is in normal ZIP format.
Compared with the gzipstream class provided by. net, gzipstream can pressurize multiple files according to the standard ZIP format, while gzipstream does not provide the method to pressurize multiple files, which needs to be defined by itself,
In this way, only your own program can be decompressed. sharpziplib is not as general as sharpziplib.

Using icsharpcode. sharpziplib. zip; <br/> # region pressurization and decompression method <br/> // <summary> <br/> // function: file compression (only files in the directory at the lower level under the folder are temporarily compressed, and the folder and its sub-level are ignored) <br/> // </Summary> <br/> // <Param name = "dirpath"> compressed folder path </param> <br/> /// <Param name = "zipfilepath"> path of the compressed file, if it is null, the first-level directory is the same as the compressed folder by default. The name is: folder name. Zip </param> <br/> // <Param name = "Err"> error message </param> <br/> // <returns> whether the file is compressed successfully </returns> <br/> Public bool zipfile (string dirpath, String zipfilepath, out string ERR) <br/>{< br/> err = ""; <br/> If (dirpath = string. empty) <br/>{< br/> err = "the folder to be compressed cannot be blank! "; <Br/> return false; <br/>}< br/> If (! Directory. exists (dirpath) <br/>{< br/> err = "the folder to be compressed does not exist! "; <Br/> return false; <br/>}< br/> // when the compressed file name is empty, use the folder name. Zip <br/> If (zipfilepath = string. empty) <br/>{< br/> If (dirpath. endswith ("//") <br/>{< br/> dirpath = dirpath. substring (0, dirpath. length-1); <br/>}< br/> zipfilepath = dirpath + ". zip "; <br/>}</P> <p> try <br/> {<br/> string [] filenames = directory. getfiles (dirpath); <br/> using (zipoutputstream S = new zipoutputstream (file. create (Z Ipfilepath) <br/>{< br/> S. setlevel (9); <br/> byte [] buffer = new byte [4096]; <br/> foreach (string file in filenames) <br/> {<br/> zipentry entry = new zipentry (path. getfilename (File); <br/> entry. datetime = datetime. now; <br/> S. putnextentry (entry); <br/> using (filestream FS = file. openread (File) <br/>{< br/> int sourcebytes; <br/> DO <br/>{< br/> sourcebytes = FS. read (buffer, 0, Buffer. Length); <br/> S. write (buffer, 0, sourcebytes); <br/>}while (sourcebytes> 0); <br/>}< br/> S. finish (); <br/> S. close (); <br/>}< br/> catch (exception ex) <br/>{< br/> err = ex. message; <br/> return false; <br/>}< br/> return true; <br/>}</P> <p> // <summary> <br/> // function: Decompress a zip file. <Br/> /// </Summary> <br/> // <Param name = "zipfilepath"> compressed file path </param> <br/> /// <Param name = "unzipdir"> decompress the file storage path, if it is null, it is in the same directory as the compressed file by default, folder with the same name as the compressed file </param> <br/> // <Param name = "Err"> error message </param> <br/> // <returns> whether the decompression is successful </returns> <br/> Public bool unzipfile (string zipfilepath, string unzipdir, out string ERR) <br/>{< br/> err = ""; <br/> If (zipfilepath = string. empty) <br/>{< br/> err = "the compressed file cannot be Null! "; <Br/> return false; <br/>}< br/> If (! File. exists (zipfilepath) <br/>{< br/> err = "the compressed file does not exist! "; <Br/> return false; <br/>}< br/> // when the decompression folder is empty, it is in the same directory as the compressed file by default, folder with the same name as the compressed file <br/> If (unzipdir = string. empty) <br/> unzipdir = zipfilepath. replace (path. getfilename (zipfilepath), path. getfilenamewithoutextension (zipfilepath); <br/> If (! Unzipdir. endswith ("//") <br/> unzipdir + = "//"; <br/> If (! Directory. exists (unzipdir) <br/> directory. createdirectory (unzipdir); </P> <p> try <br/> {<br/> using (zipinputstream S = new zipinputstream (file. openread (zipfilepath) <br/>{</P> <p> zipentry theentry; <br/> while (theentry = S. getnextentry ())! = NULL) <br/>{< br/> string directoryname = path. getdirectoryname (theentry. name); <br/> string filename = path. getfilename (theentry. name); <br/> If (directoryname. length> 0) <br/>{< br/> directory. createdirectory (unzipdir + directoryname); <br/>}< br/> If (! Directoryname. endswith ("//") <br/> directoryname + = "//"; <br/> If (filename! = String. empty) <br/>{< br/> using (filestream streamwriter = file. create (unzipdir + theentry. name) <br/>{</P> <p> int size = 2048; <br/> byte [] DATA = new byte [2048]; <br/> while (true) <br/> {<br/> size = S. read (data, 0, Data. length); <br/> If (size> 0) <br/>{< br/> streamwriter. write (data, 0, size); <br/>}< br/> else <br/>{< br/> break; <br/>}< br/>}// while <br/>}< br /> catch (exception ex) <br/> {<br/> err = ex. message; <br/> return false; <br/>}< br/> return true; <br/>}// decompress <br/> # endregion

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.