#region Unpack the file zip format rar format
/// <summary>
/// Unzip the files
/// </summary>
/// <param name="fileFromUnZip"> File path before unpacking (absolute path) </param>
/// <param name="fileToUnZip"> Unzipped file directory (absolute path) </param>
public static void UnpackFile(string fileFromUnZip, string fileToUnZip)
{
// Get the compressed type
string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + 1, 3).ToLower();
switch (unType)
{
case "rar":
UnRar(fileFromUnZip, fileToUnZip);
break;
case "zip":
UnZip(fileFromUnZip, fileToUnZip);
break;
}
}
// Unpack the rar File format
private static void UnRar(string fileFromUnZip, string fileToUnZip)
{
using (Process Process1 = new Process())// open 1 A process Perform decompression work
{
string ServerDir = ConfigurationManager.AppSettings["UnpackFile"].ToString();//rar The installation path of the tool You have to install WinRAR // 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();// Unzip the start
Process1.WaitForExit();
Process1.Close();
}
}
// Unpack 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 unzip directory
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + fileName);// Unzip 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