Method 1: Call WinRAR
Using Microsoft. Win32;
Using system. diagnostics;
Protected void button#click (Object sender, eventargs E)
{
RAR (@ "E:/95413594531/GIS", "tmptest", @ "E:/95413594531 /");
}
/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "dfilepath"> the folder or file to be compressed </param>
/// <Param name = "drarname"> Generate the compressed file name </param>
/// <Param name = "drarpath"> Generate the path for saving the compressed file </param>
/// <Returns> </returns>
Protected bool RAR (string dfilepath, string drarname, string drarpath)
{
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
{
The_reg = registry. classesroot. opensubkey (@ "applications/winrar.exe/Shell/Open/command ");
The_obj = the_reg.getvalue ("");
The_rar = the_obj.tostring ();
The_reg.close ();
The_rar = the_rar.substring (1, the_rar.length-7 );
The_info = "A" + "" + drarname + "" + dfilepath + "-EP1"; // command + compressed file name + compressed file or path
The_startinfo = new processstartinfo ();
The_startinfo.filename = the_rar;
The_startinfo.arguments = the_info;
The_startinfo.windowstyle = processwindowstyle. hidden;
The_startinfo.workingdirectory = drarpath; // directory where the rarfile is stored.
The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
The_process.waitforexit ();
The_process.close ();
Return true;
}
Catch (exception ex)
{
Return false;
}
}
/// <Summary>
/// Decompress the package to the specified folder
/// </Summary>
/// <Param name = "rarfilepath"> directory of the compressed file </param>
/// <Param name = "rarfilename"> compressed file name </param>
/// <Param name = "unrarfilepath"> decompress the package to a folder </param>
/// <Returns> </returns>
Protected bool unrar (string rarfilepath, string rarfilename, string unrarfilepath)
{
// Extract
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
{
The_reg = registry. classesroot. opensubkey (@ "applications/winrar.exe/Shell/Open/command ");
The_obj = the_reg.getvalue ("");
The_rar = the_obj.tostring ();
The_reg.close ();
The_rar = the_rar.substring (1, the_rar.length-7 );
The_info = @ "X" + "" + rarfilepath + rarfilename + "" + unrarfilepath;
The_startinfo = new processstartinfo ();
The_startinfo.filename = the_rar;
The_startinfo.arguments = the_info;
The_startinfo.windowstyle = processwindowstyle. hidden;
The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
Return true;
}
Catch (exception ex)
{
Return false;
}
}
Method 2: Call the class library icsharpcode. sharpziplib. dll
Using system. IO;
Using icsharpcode. sharpziplib;
Using icsharpcode. sharpziplib. checksums;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. gzip;
Using icsharpcode. sharpziplib. Bzip2;
Using icsharpcode. sharpziplib. Zip. compression;
Using icsharpcode. sharpziplib. Zip. Compression. streams;
Protected void button#click (Object sender, eventargs E)
{
String [] STR = new string [1];
STR [0] = @ "E:/9541241560/local school /";
Zip (STR );
}
# Region Compression
/// <Summary>
/// Compressed file. The default directory is the current directory, the file name is the current directory name, and the compression level is 6.
/// </Summary>
/// <Param name = "fileordirectory"> file or folder to be compressed </param>
Public void zip (Params string [] fileordirectory)
{
Zip (6, fileordirectory );
}
/// <Summary>
/// Compressed file. The default directory is the current directory, and the file name is the current directory name.
/// </Summary>
/// <Param name = "ziplevel"> compression level </param>
/// <Param name = "fileordirectory"> file or folder to be compressed </param>
Public void zip (INT ziplevel, Params string [] fileordirectory)
{
If (fileordirectory = NULL)
Return;
Else if (fileordirectory. Length <1)
Return;
Else
{
String STR = fileordirectory [0];
If (Str. endswith ("//"))
STR = Str. substring (0, str. Length-1 );
STR + = ". Zip ";
Zip (STR, ziplevel, fileordirectory );
}
}
/// <Summary>
/// Compressed file. The default directory is the current directory.
/// </Summary>
/// <Param name = "zipedfilename"> compressed file </param>
/// <Param name = "ziplevel"> compression level </param>
/// <Param name = "fileordirectory"> file or folder to be compressed </param>
Public void zip (string zipedfilename, int ziplevel, Params string [] fileordirectory)
{
If (fileordirectory = NULL)
Return;
Else if (fileordirectory. Length <1)
Return;
Else
{
String STR = fileordirectory [0];
If (Str. endswith ("//"))
STR = Str. substring (0, str. Length-1 );
STR = Str. substring (0, str. lastindexof ("//"));
Zip (zipedfilename, STR, ziplevel, fileordirectory );
}
}
/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "zipedfilename"> compressed file </param>
/// <Param name = "ziplevel"> compression level </param>
/// <Param name = "currentdirectory"> current directory </param>
/// <Param name = "fileordirectory"> file or folder to be compressed </param>
Public void zip (string zipedfilename, string currentdirectory, int ziplevel, Params string [] fileordirectory)
{
Arraylist allfiles = new arraylist ();
// Obtain all objects
If (fileordirectory! = NULL)
{
For (INT I = 0; I <fileordirectory. length; I ++)
{
If (file. exists (fileordirectory [I])
Allfiles. Add (fileordirectory [I]);
Else if (directory. exists (fileordirectory [I])
Getdirectoryfile (fileordirectory [I], allfiles );
}
}
If (allfiles. Count <1)
Return;
Zipoutputstream zipedstream = new zipoutputstream (file. Create (zipedfilename ));
Zipedstream. setlevel (ziplevel );
For (INT I = 0; I <allfiles. Count; I ++)
{
String file = allfiles [I]. tostring ();
Filestream FS;
// Open the file to be compressed
Try
{
FS = file. openread (File );
}
Catch
{
Continue;
}
Try
{
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
// Create an entry
String filename = file. Replace (currentdirectory ,"");
If (filename. startswith ("//"))
Filename = filename. substring (1 );
Zipentry entry = new zipentry (filename );
Entry. datetime = datetime. now;
// Save to the zip stream
FS. Close ();
Zipedstream. putnextentry (entry );
Zipedstream. Write (buffer, 0, buffer. Length );
}
Catch
{
}
Finally
{
FS. Close ();
FS. Dispose ();
}
}
Zipedstream. Finish ();
Zipedstream. Close ();
}
/// <Summary>
/// Compressed folder
/// </Summary>
/// <Param name = "curretndirectory"> current folder </param>
Public void zipdirectory (string curretndirectory)
{
If (curretndirectory = NULL)
Return;
String dir = curretndirectory;
If (dir. endswith ("//"))
Dir = dir. substring (0, dir. Length-1 );
String file = dir. substring (dir. lastindexof ("//") + 1) + ". Zip ";
Dir + = "//" + file;
Zip (Dir, 6, curretndirectory );
}
/// <Summary>
/// Compressed folder
/// </Summary>
/// <Param name = "curretndirectory"> current folder </param>
Public void zipdirectory (string curretndirectory, int ziplevel)
{
If (curretndirectory = NULL)
Return;
String dir = curretndirectory;
If (dir. endswith ("//"))
Dir = dir. substring (0, dir. Length-1 );
Dir + = ". Zip ";
Zip (Dir, ziplevel, curretndirectory );
}
// Recursively retrieve all objects in a directory
Private void getdirectoryfile (string parentdirectory, arraylist tostore)
{
String [] files = directory. getfiles (parentdirectory );
For (INT I = 0; I <files. length; I ++)
Tostore. Add (files [I]);
String [] directorys = directory. getdirectories (parentdirectory );
For (INT I = 0; I <directorys. length; I ++)
Getdirectoryfile (directorys [I], tostore );
}
# Endregion