Extract RAR and use unrar. dll
The solution we often see is to use process to call WinRAR for decompression, but the server does not necessarily have to buy WinRAR. To call process, you must have sufficient permissions for the Internet account set by IIS? Another solution is to use unrar. dll.
After unrar is downloaded and unrar is unlocked, it contains examples of C # language. After reading license.txt, it should be applicable to commercial activities.
「 May be used in any software to handle RAR archives without limitations free of charge 」
I tried to use it in the Web site project. The document name can also be properly unlocked. But it should be noted that unrar. DLL is not native. net DLL, but written in C ++, which provides unrar. CS uses C # To package a layer and uses dllimport to call its functions. Therefore, you do not need to add a reference in the project. You can directly copy the content to the bin directory, in addition, it does not work on the web server in the development environment (the DLL cannot be read) and can be placed on IIS.
It's really easy to use. The code is like this.
Using System;
Using System. IO;
Using Schematrix;
Public Partial Class _ Default: system. Web. UI. Page {
Protected Void Page_load ( Object Sender, eventargs e ){
String File = Server. mappath ( " ~ /App_data/image .rar " );
String Targetpath = Server. mappath ( " ~ /App_data/ " );
Decompressrar (file, targetpath, False );
}
Public Void Decompressrar ( String Rararchive, String Destinationpath, Bool Createdir ){
If (File. exists (rararchive )){
Unrar = New Unrar (rararchive );
Unrar. Open (unrar. openmode. Extract );
Unrar. destinationpath = Destinationpath;
While (Unrar. readheader ()){
If (Unrar. currentfile. isdirectory ){
Unrar. Skip ();
} Else {
If (Createdir ){
Unrar. Extract ();
} Else {
Unrar. Extract (destinationpath + Path. getfilename (unrar. currentfile. filename ));
}
}
}
Unrar. Close ();
}
}
}
Download the entire example (it can only be executed on IIS)