C # decompress the zip file

Source: Internet
Author: User

/// <Summary> /// function: Decompress a zip file. /// </Summary> /// <param name = "zipFilePath"> compressed file path </param> /// <param name = "unZipDir"> decompressed 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> /// <param name = "err"> error message </param> /// <returns> whether the decompression is successful </returns> public bool UnZipFile (string zipFilePath, string unZipDir) //, out string err {// err = ""; if (zipFilePath = string. empty) {// err = "the compressed file cannot be blank! "; Return false;} if (! File. Exists (zipFilePath) {// err = "the compressed File does not exist! "; Return false;} // if (unZipDir = string. empty) unZipDir = zipFilePath. replace (Path. getFileName (zipFilePath), Path. getFileNameWithoutExtension (zipFilePath); if (! UnZipDir. EndsWith ("\") unZipDir + = "\"; if (! Directory. exists (unZipDir) Directory. createDirectory (unZipDir); try {using (ZipInputStream s = new ZipInputStream (File. openRead (zipFilePath. toLower () {ZipEntry theEntry; while (theEntry = s. getNextEntry ())! = Null) {string directoryName = Path. getDirectoryName (theEntry. name); string fileName = Path. getFileName (theEntry. name); if (directoryName. length> 0) {Directory. createDirectory (unZipDir + directoryName);} if (! DirectoryName. EndsWith ("\") directoryName + = "\"; if (fileName! = String. empty) {using (FileStream streamWriter = File. create (unZipDir + theEntry. name) {int size = 2048; byte [] data = new byte [2048]; while (true) {size = s. read (data, 0, data. length); if (size> 0) {streamWriter. write (data, 0, size) ;}else {break ;}}}// while} catch (Exception ex) {// err = ex. message; return false;} return true;} // decompress

 

Related Article

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.