[Introduction of using system.diagnostics;using Microsoft.Win32;]
#region using gzip compressionfile, returns a BOOL value
<param name= "Filesource" > source file name to compress </param>
<param name= "Filetarget" > file name after compression </param>
<returns> type of success after compression </returns>
public static bool Gzipfile (string Filesource, String filetarget)
{
byte[] MyByte = null;
FileStream mystream = null;
FileStream mydesstream = null;
GZipStream mycomstream = null;
Try
{
MyStream = new FileStream (Filesource, FileMode.Open, FileAccess.Read, FileShare.Read);
MyByte = new Byte[mystream.length];
Mystream.read (mybyte, 0, mybyte.length);
Mydesstream = new FileStream (Filetarget, FileMode.OpenOrCreate, FileAccess.Write);
Mycomstream = new GZipStream (Mydesstream, compressionmode.compress, true);
Mycomstream.write (mybyte, 0, mybyte.length);
return true;
}
catch (Exception ex)
{
Exceptionmessage = ex. Message;
return false;
}
Finally
{
Mystream.close ();
Mycomstream.close ();
Mydesstream.close ();
}
}
#endregion
#region use gzip to extract the file, return bool value
<summary>
Extract the file using gzip, return bool value
</summary>
<param name= "Filesource" > Source filename to unzip </param>
<param name= "Filetarget" > FileName to unzip </param>
<returns> successful types of decompression </returns>
public static bool Gzipsfile (string Filesource, String filetarget)
{
byte[] MyByte = null;
FileStream mystream = null;
FileStream mydesstream = null;
GZipStream mydecomstream = null;
Try
{
MyStream = new FileStream (Filesource, FileMode.Open);
Mydecomstream = new GZipStream (MyStream, compressionmode.decompress, true);
MyByte = new Byte[4];
int myposition = (int) mystream.length-4;
Mystream.position = myposition;
Mystream.read (MyByte, 0, 4);
mystream.position = 0;
int mylength = Bitconverter.toint32 (mybyte, 0);
byte[] MyData = new Byte[mylength + 100];
int myoffset = 0;
int mytotal = 0;
while (true)
{
int mybytesread = Mydecomstream.read (MyData, Myoffset, 100);
if (Mybytesread = = 0)
Break
Myoffset + = Mybytesread;
Mytotal + = Mybytesread;
}
Mydesstream = new FileStream (Filetarget, FileMode.Create);
Mydesstream.write (myData, 0, mytotal);
Mydesstream.flush ();
return true;
}
catch (Exception ex)
{
Exceptionmessage = ex. Message;
return false;
}
Finally
{
Mystream.close ();
Mydecomstream.close ();
Mydesstream.close ();
}
}
#endregion
[WinRAR compression software must be installed on the target machine]
#region compressing files using winrar
<summary>
compressing files with WinRAR
</summary>
<param name= "Filesource" > source file name to compress </param>
<param name= "Filetarget" > file name after compression </param>
<returns> success types of compression </returns>
public static bool Rarfile (string Filesource, String filetarget)
{
String Myrar;
RegistryKey Myreg;
Object MYOBJ;
String MyInfo;
ProcessStartInfo Mystartinfo;
Process myprocess;
Try
{
Myreg = Registry.ClassesRoot.OpenSubKey ("Applications\\winrar.exe\\shell\\open\\command");
MYOBJ = Myreg.getvalue ("");
Myrar = Myobj.tostring ();
Myreg.close ();
Myrar = myrar.substring (1, myrar.length-7);
MyInfo = "a" + Filetarget + "" + Filesource;
Mystartinfo = new ProcessStartInfo ();
Mystartinfo.filename = Myrar;
Mystartinfo.arguments = MyInfo;
Mystartinfo.windowstyle = Processwindowstyle.hidden;
myprocess = new Process ();
Myprocess.startinfo = Mystartinfo;
Myprocess.start ();
return true;
}
catch (Exception ex)
{
Exceptionmessage = ex. Message;
return false;
}
}
#endregion
#region Extracting files using winrar
<summary>
Extracting files using WinRAR
</summary>
<param name= "Filesource" > Source filename to unzip </param>
<param name= "Filetarget" > filename after decompression </param>
<returns> successful types of decompression </returns>
public static bool Rarsfile (string Filesource, String filetarget)
{
String Myrar;
RegistryKey Myreg;
Object MYOBJ;
String MyInfo;
ProcessStartInfo Mystartinfo;
Process myprocess;
Try
{
Myreg = Registry.ClassesRoot.OpenSubKey ("Applications\\winrar.exe\\shell\\open\\command");
MYOBJ = Myreg.getvalue ("");
Myrar = Myobj.tostring ();
Myreg.close ();
Myrar = myrar.substring (1, myrar.length-7);
MyInfo = "X" + filesource+ "" + filetarget + "\ \";
Mystartinfo = new ProcessStartInfo ();
Mystartinfo.filename = Myrar;
Mystartinfo.arguments = MyInfo;
Mystartinfo.windowstyle = Processwindowstyle.hidden;
myprocess = new Process ();
Myprocess.startinfo = Mystartinfo;
Myprocess.start ();
return true;
}
catch (Exception ex)
{
Exceptionmessage = ex. Message;
return false;
}
}
#endregion
Reproduced in original: http://www.zhixing123.cn/net/16278.html
Implementing program code to implement online compressed files