解压文件的方法

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

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

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

43ExpandedSubBlockEnd.gif                    }

44InBlock.gif    
45InBlock.gif                    streamWriter.Close();
46ExpandedSubBlockEnd.gif                }

47ExpandedSubBlockEnd.gif            }

48InBlock.gif            s.Close();
49ExpandedBlockEnd.gif        }


2 第二个是RAR的,需要调用WINAPI
None.gif     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 
None.gif            
private   struct  SHELLEXECUTEINFO 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
public int cbSize; 
InBlock.gif            
public uint fMask; 
InBlock.gif            
public IntPtr hwnd; 
InBlock.gif            
public string lpVerb; 
InBlock.gif            
public string lpFile; 
InBlock.gif            
public string lpParameters; 
InBlock.gif            
public string lpDirectory; 
InBlock.gif            
public int nShow; 
InBlock.gif            
public IntPtr hInstApp; 
InBlock.gif            
public int lpIDList; 
InBlock.gif            
public string lpClass; 
InBlock.gif            
public IntPtr hkeyClass; 
InBlock.gif            
public int dwHotKey; 
InBlock.gif            
public IntPtr hIcon; 
InBlock.gif            
public IntPtr hProcess; 
ExpandedBlockEnd.gif        }
 
None.gif
None.gif        [DllImport(
" shell32 " , CharSet = CharSet.Auto)] 
None.gif        
extern   static   int  ShellExecuteEx( ref  SHELLEXECUTEINFO lpExecInfo); 
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 解压压缩包(RAR格式)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="directoryName">目录名</param>
ExpandedBlockEnd.gif        
/// <param name="fileName">文件名</param>

None.gif          public   static   void   UnRar( string  directoryName, string  filePath)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SHELLEXECUTEINFO seInfo 
= new SHELLEXECUTEINFO(); 
InBlock.gif
InBlock.gif 
InBlock.gif                seInfo.cbSize 
= Marshal.SizeOf(seInfo); 
InBlock.gif

InBlock.gif

InBlock.gif                seInfo.lpVerb 
="open";InBlock.gif
InBlock.gif                

InBlock.gif
                seInfo.lpFile = System.Configuration.ConfigurationSettings.AppSettings["WINRAR"];
InBlock.gif
InBlock.gif                seInfo.nShow
=5;
InBlock.gif
InBlock.gif                
if(!Directory.Exists(directoryName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Directory.CreateDirectory(directoryName);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
string param = string.Format(@"E  -ep -inul  -y  -o+  {0}  {1}",filePath,directoryName);
InBlock.gif
InBlock.gif                seInfo.lpParameters
=param;
InBlock.gif
InBlock.gif                ShellExecuteEx(
ref seInfo);
InBlock.gif
InBlock.gif                System.Threading.Thread.Sleep(
4000);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//删除上传的RAR文件
InBlock.gif
                if(File.Exists(filePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.Delete(filePath);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif        }


    小小心得,高手勿笑

转载于:https://www.cnblogs.com/yfx1982/archive/2006/01/05/311852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值