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