解压文件的方法

    最近工作中需要用到解压文件的方法,研究了一阵子,得到了两个方法,分开对应 ZIP和RAR文件的,现在拿出来给大家分享
     1.先是解压ZIP的,需要第三方控件ICSharpCode.SharpZipLib.dll,搜索一下应该有得下(我不知道该怎么 上传上来),下面是解压的代码 :
   
 1/// <summary>
 2        /// 解压压缩包
 3        /// </summary>
 4        /// <param name="directoryName">目录名</param>
 5        /// <param name="fileName">文件名</param>

 6        public static void UnZip(string directoryName,string filePath)
 7        {
 8            
 9            ZipInputStream s = new ZipInputStream(File.OpenRead(filePath));
10  
11            ZipEntry theEntry;
12            while ((theEntry = s.GetNextEntry()) != null
13            {
14   
15                
16                string fileName      = Path.GetFileName(theEntry.Name);
17   
18                //生成解压目录
19                if(Directory.Exists(directoryName))
20                {
21                    Directory.Delete(directoryName,true);
22                }

23                Directory.CreateDirectory(directoryName);
24   
25                if (fileName != String.Empty) 
26                {   
27                    //解压文件到指定的目录
28                    FileStream streamWriter = File.Create(directoryName + "\\" + theEntry.Name);
29    
30                    int size = 2048;
31                    byte[] data = new byte[2048];
32                    while (true
33                    {
34                        size = s.Read(data, 0, data.Length);
35                        if (size > 0
36                        {
37                            streamWriter.Write(data, 0, size);
38                        }
 
39                        else 
40                        {
41                            break;
42                        }

43                    }

44    
45                    streamWriter.Close();
46                }

47            }

48            s.Close();
49        }


2 第二个是RAR的,需要调用WINAPI
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] 
            
private struct SHELLEXECUTEINFO 
        

            
public int cbSize; 
            
public uint fMask; 
            
public IntPtr hwnd; 
            
public string lpVerb; 
            
public string lpFile; 
            
public string lpParameters; 
            
public string lpDirectory; 
            
public int nShow; 
            
public IntPtr hInstApp; 
            
public int lpIDList; 
            
public string lpClass; 
            
public IntPtr hkeyClass; 
            
public int dwHotKey; 
            
public IntPtr hIcon; 
            
public IntPtr hProcess; 
        }
 

        [DllImport(
"shell32", CharSet=CharSet.Auto)] 
        
extern static int ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo); 

        
/// <summary>
        
/// 解压压缩包(RAR格式)
        
/// </summary>
        
/// <param name="directoryName">目录名</param>
        
/// <param name="fileName">文件名</param>

        public static void  UnRar(string directoryName,string filePath)
        
{
            
try

            
{
                SHELLEXECUTEINFO seInfo 
= new SHELLEXECUTEINFO(); 

 
                seInfo.cbSize 
= Marshal.SizeOf(seInfo); 


                seInfo.lpVerb 
="open";
                

                seInfo.lpFile = System.Configuration.ConfigurationSettings.AppSettings["WINRAR"];

                seInfo.nShow
=5;

                
if(!Directory.Exists(directoryName))
                
{
                    Directory.CreateDirectory(directoryName);
                }


                
string param = string.Format(@"E  -ep -inul  -y  -o+  {0}  {1}",filePath,directoryName);

                seInfo.lpParameters
=param;

                ShellExecuteEx(
ref seInfo);

                System.Threading.Thread.Sleep(
4000);

            }

            
catch
            
{

            }

            
finally
            
{
                
//删除上传的RAR文件
                if(File.Exists(filePath))
                
{
                    File.Delete(filePath);
                }

            }



        }


    小小心得,高手勿笑
posted on 2006-01-05 20:56  仁面寿星  阅读(1195)  评论(0编辑  收藏  举报