C#之SharpZipLib 解压缩工具类

本文介绍了SharpZipLib,一个用C#编写的.NET库,支持Zip、GZip、BZip2和Tar压缩。文章提供了使用NuGet安装和在.NET项目中集成的步骤,以及两个UnZip方法的代码示例。

SharpZipLib官方介绍

SharpZipLib(#ziplib,以前称为NZipLib)是一个完全用C#编写的用于Zip、GZip、BZip2和Tar的压缩库,适用于.NET。它被实现为一个程序集(可安装到GAC中),因此可以轻松地集成到其他项目中(任何.NET语言)。

使用NuGet 获取并安装

添加引用

using ICSharpCode.SharpZipLib.Zip;

 代码示例

    public class ZipHelper
    {

        public void UnZip(string zipFilePath, string unZipDir)
        {
            if (zipFilePath == string.Empty)
            {
                throw new Exception("压缩文件不能为空!");
            }
            if (!File.Exists(zipFilePath))
            {
                throw new FileNotFoundException("压缩文件不存在!");
            }
            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  
            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            if (!unZipDir.EndsWith("/"))
                unZipDir += "/";
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);
                    if (!string.IsNullOrEmpty(directoryName))
                    {
                        Directory.CreateDirectory(unZipDir + directoryName);
                    }
                    if (directoryName != null && !directoryName.EndsWith("/"))
                    {
                    }
                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                        {

                            int size;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        public void UnZip(byte[] zipFileData, string unZipDir)
        {
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            string filePath = string.Empty;
            try
            {
                using (var s = new ZipInputStream(new MemoryStream(zipFileData)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (string.IsNullOrEmpty(directoryName))
                        {
                            filePath = Path.Combine(unZipDir, fileName);
                        }
                        else
                        {
                            var dir = Path.Combine(unZipDir, directoryName);
                            Directory.CreateDirectory(dir);
                            if (!string.IsNullOrEmpty(fileName))
                            {
                                filePath = Path.Combine(dir, fileName);
                            }
                        }
                        if (filePath != String.Empty)
                        {
                            try
                            {
                                using (FileStream streamWriter = File.Create(filePath))
                                {
                                    int size;
                                    byte[] data = new byte[2048];
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size > 0)
                                        {
                                            streamWriter.Write(data, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(filePath);
                                throw;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(filePath);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }

 未完待续...

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值