C # package the folder in ZIP format (including all files in folders and subfolders)

Source: Internet
Author: User
Tags crc32 zip extension

I didn't like to play with a blog. I also registered this blog in the blog Park this time.
But there is no content, so I want to add anything.

Most of the gadgets recently developed by Microsoft are related to file operations/I/O. Therefore, I am going to sort out these two days and paste some things that may be shared with you, I hope it will be helpful for beginners of C!
(More examples)Code, Can access Microsoft learners help website code smaples from Microsoft: http://1code.codeplex.com, download microsoft's All-in-onecode framework, this is a tool that I recently developed in Microsoft to help developers learn. It can be found that there are many sample codes for the learner's reference .)

 

C # You can call a third-party DLL to package a zip file to get twice the result with half the effort, and the DLL is free of charge: sharpziplib

After downloading and decompressing the file, run the icsharpcode command. sharpziplib. copy the DLL to the directory of the current project (if you are lazy, you can directly copy it to the bin \ DEBUG directory of the current project), right-click the project reference opened by Vs, and add the reference icsharpcode. sharpziplib. DLL

Then, right-click the project opened by Vs to create a class named ziphelper. CS, clear all codes in the class, copy the following code, and paste it:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. io; using system. diagnostics; using icsharpcode. sharpziplib; using icsharpcode. sharpziplib. zip; using icsharpcode. sharpziplib. checksums; using icsharpcode. sharpziplib. core; namespace ziponecode. zipprovider {public class ziphelper {/// <summary> /// compressed file /// </Summary> /// <Param name = "sourcefilepath"> </param> /// <Param name = "destinationzipfilepath"> </param> Public static void createzip (string sourcefilepath, string destinationzipfilepath) {If (sourcefilepath [sourcefilepath. Length-1]! = System. io. path. directoryseparatorchar) sourcefilepath + = system. io. path. directoryseparatorchar; zipoutputstream zipstream = new zipoutputstream (file. create (destinationzipfilepath); zipstream. setlevel (6); // compression level 0-9 createzipfiles (sourcefilepath, zipstream, sourcefilepath); zipstream. finish (); zipstream. close ();} /// <summary> /// recursive compression file /// </Summary> /// <Param name = "sourcefilepath"> path of the file or folder to be compressed </Param >/// <Param name = "zipstream"> ZIP file path of the packaging result (similar to D: \ workspace \ a.zipfiles, full package name and. Zip extension </param> // <Param name = "staticfile"> </param> Private Static void createzipfiles (string sourcefilepath, zipoutputstream zipstream, string staticfile) {CRC32 CRC = new CRC32 (); string [] filesarray = directory. getfilesystementries (sourcefilepath); foreach (string file in filesarray) {If (directory. exists (File) // if it is a folder, recursive {createzipfiles (file, zipstream, staticfile);} else // if it is a file, start compressing {filestream = file. openread (File); byte [] buffer = new byte [filestream. length]; filestream. read (buffer, 0, buffer. length); string tempfile = file. substring (staticfile. lastindexof ("\") + 1); zipentry entry = new zipentry (tempfile); entry. datetime = datetime. now; entry. size = filestream. length; filestream. close (); CRC. reset (); CRC. update (buffer); entry. CRC = CRC. value; zipstream. putnextentry (entry); zipstream. write (buffer, 0, buffer. length );}}}}}

 

 

After using ziponecode. zipprovider is referenced externally, you can call ziphelper. createzip (@ "D: \ temp \ forzip", @ "D: \ temp2 \ forzip.zip.

Note that before calling, consider checking for exceptions, such as whether the source file path exists.

 

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.