C # How to decompress the file

Source: Internet
Author: User

Copy codeThe Code is as follows: # region decompress the file in the zip format. rar format
/// <Summary>
/// Decompress the file
/// </Summary>
/// <Param name = "fileFromUnZip"> file path before decompression (absolute path) </param>
/// <Param name = "fileToUnZip"> decompressed file directory (absolute path) </param>
Public static void UnpackFile (string fileFromUnZip, string fileToUnZip)
{
// Obtain the compression type
String unType = fileFromUnZip. Substring (fileFromUnZip. LastIndexOf (".") + 1, 3). ToLower ();
Switch (unType)
{
Case "rar ":
UnRar (fileFromUnZip, fileToUnZip );
Break;
Case "zip ":
UnZip (fileFromUnZip, fileToUnZip );
Break;
}
}
// Extract files in rar format
Private static void UnRar (string fileFromUnZip, string fileToUnZip)
{
Using (Process Process1 = new Process () // start a Process to perform Decompression
{
String ServerDir = ConfigurationManager. deleetask[ "UnpackFile"]. toString (); // The installation path of the rar tool must be WinRAR // For example, C: \ Program Files (x86) \ WinRAR \ RAR.exe
Process1.StartInfo. UseShellExecute = false;
Process1.StartInfo. RedirectStandardInput = true;
Process1.StartInfo. RedirectStandardOutput = true;
Process1.StartInfo. RedirectStandardError = true;
Process1.StartInfo. CreateNoWindow = true;
Process1.StartInfo. FileName = ServerDir;
Process1.StartInfo. Arguments = "x-inul-y" + fileFromUnZip + "" + fileToUnZip;
Process1.Start (); // Extract
Process1.WaitForExit ();
Process1.Close ();
}
}
// Decompress the zip file
Public static void UnZip (string fileFromUnZip, string fileToUnZip)
{
ZipInputStream inputStream = new ZipInputStream (File. OpenRead (fileFromUnZip ));
ZipEntry theEntry;
While (theEntry = inputStream. GetNextEntry ())! = Null)
{
FileToUnZip + = "/";
String fileName = Path. GetFileName (theEntry. Name );
String path = Path. GetDirectoryName (fileToUnZip) + "/";
// Directory. CreateDirectory (path); // generate the extract Directory
If (fileName! = String. Empty)
{
FileStream streamWriter = File. Create (path + fileName); // extract the File to the specified directory.
Int size = 2048;
Byte [] data = new byte [2048];
While (true)
{
Size = inputStream. Read (data, 0, data. Length );
If (size> 0)
{
StreamWriter. Write (data, 0, size );
}
Else
{
Break;
}
}
StreamWriter. Close ();
}
}
InputStream. Close ();
}
# Endregion
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.