博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SharpZipLib 解压zip压缩文件 (2011/03/28)

Posted on 2011-03-28 10:00  李存  阅读(345)  评论(0)    收藏  举报

废话我就不说了,直接粘源码,希望对用的着得朋友有所帮助。

using System;
using System.Data;
using System.Configuration;
using ICSharpCode.SharpZipLib.Zip;

namespace IO
{
    public class ZipHelper
    {
        /// <summary>
        /// 解压zip文件到指定的目录
        /// </summary>
        /// <param name="stream">传入的要解压的zip文件的流对象</param>
        /// <param name="destPath">要解压到的目录</param>
        public static void DeCompress(System.IO.Stream stream, string destPath)
        {

      ZipInputStream zis = new ZipInputStream(stream);
            ZipEntry ze;
            while ((ze = zis.GetNextEntry()) != null)
            {
                if (ze.IsDirectory)
                {
                    if (!System.IO.Directory.Exists(System.IO.Path.Combine(destPath,System.IO.Path.GetDirectoryName(ze.Name))))
                    {
                        System.IO.Directory.CreateDirectory(System.IO.Path.Combine(destPath, System.IO.Path.GetDirectoryName(ze.Name)));
                        continue;
                    }
                }
                else if (ze.IsFile)
                {
                    System.IO.FileStream fs = null;
                    if (!System.IO.File.Exists(System.IO.Path.Combine(destPath, System.IO.Path.GetDirectoryName(ze.Name))))
                    {
                        fs = System.IO.File.Create(System.IO.Path.Combine(System.IO.Path.Combine(destPath, System.IO.Path.GetDirectoryName(ze.Name)),System.IO.Path.GetFileName(ze.Name)));
                    }

                    if (fs != null)
                    {
                        byte[] byt = new byte[2048];
                        while (true)
                        {
                            int i = zis.Read(byt, 0, byt.Length);
                            fs.Write(byt, 0, byt.Length);
                            if (i < 1)
                            {
                                break;
                            }
                        }
                        fs.Close();
                    }
                }               
            }
            zis.Close();

        }
    }
}