在C#中运用SharpZipLib和unrar进行解压缩

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.IO;
None.gif
using  ICSharpCode.SharpZipLib.Zip;
None.gif
None.gif
namespace  Campton.Tools
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class ZipManager
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public ZipManager() dot.gif{ }
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有静态变量#region 私有静态变量
InBlock.gif        
private static ZipManager _instance;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
静态方法#region 静态方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建实例方法
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>返回ZipManager对象实例</returns>

InBlock.gif        public static ZipManager Instance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_instance == null)
InBlock.gif                _instance 
= new ZipManager();
InBlock.gif
InBlock.gif            
return _instance;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公有方法#region 公有方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 解压文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="zipFile">zip文件(完整的绝对路径)</param>
InBlock.gif        
/// <param name="destFolder">目标目录(绝对路径)</param>
ExpandedSubBlockEnd.gif        
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>

InBlock.gif        public bool UnCompress(string zipFile, string destFolder)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!File.Exists(zipFile))
InBlock.gif                
return false;
InBlock.gif            
string exts = Path.GetExtension(zipFile);
InBlock.gif            
if (string.Compare(exts, ".zip"true== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return UnZip(zipFile, destFolder);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (string.Compare(exts, ".rar"true== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return UnRar(zipFile, destFolder);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有方法#region 私有方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对Zip文件进行解压
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="zipFile">zip文件(完整的绝对路径)</param>
InBlock.gif        
/// <param name="destFolder">目标目录(绝对路径)</param>
ExpandedSubBlockEnd.gif        
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>

InBlock.gif        private bool UnZip(string zipFile, string destFolder)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!File.Exists(zipFile))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
if (!Directory.Exists(destFolder))
InBlock.gif                Directory.CreateDirectory(destFolder);
InBlock.gif            FileInfo fi 
= new FileInfo(zipFile);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (ZipInputStream zis = new ZipInputStream(fi.OpenRead()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ZipEntry ze;
InBlock.gif                    
while ((ze = zis.GetNextEntry()) != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string path = Path.Combine(destFolder, ze.Name);
InBlock.gif                        
if (ze.IsDirectory)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            Directory.CreateDirectory(path);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
using (Stream outStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
int size = 2048;
InBlock.gif                                
byte[] data = new byte[size];
InBlock.gif
InBlock.gif                                
while (size > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    size 
= zis.Read(data, 0, data.Length);
InBlock.gif                                    outStream.Write(data, 
0, size);
ExpandedSubBlockEnd.gif                                }

InBlock.gif
InBlock.gif                                outStream.Flush();
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对RAR所支持的压缩文件进行解压
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="zipFile">压缩文件(完整的绝对路径)</param>
InBlock.gif        
/// <param name="destFolder">目标目录(绝对路径)</param>
ExpandedSubBlockEnd.gif        
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>

InBlock.gif        private bool UnRar(string zipFile, string destFolder)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!File.Exists(zipFile))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
if (!Directory.Exists(destFolder))
InBlock.gif                Directory.CreateDirectory(destFolder);
InBlock.gif            
InBlock.gif            Unrar unRar 
= new Unrar();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                unRar.DestinationPath 
= destFolder;
InBlock.gif                unRar.Open(zipFile, Unrar.OpenMode.Extract);
InBlock.gif                
while (unRar.ReadHeader())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    unRar.Extract();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (unRar != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    unRar.Close();
InBlock.gif                    unRar.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
posted on 2007-05-12 21:33 黄尚 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/afxcn/archive/2007/05/12/744234.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值