c#使用SharpZipLib压缩和解压缩文件

本文是在其他人基础上修改而来,测试过程:原文地址

正常zip方式压缩文件,然后用wrar工具可以解压出来(开始用wrar3.4版本的怎么也解不出来,以为是不支持wrar解压,后来改用3.9及4.11版本可以实现正常解压,搞了半天时间,原来是版本问题,版本不对害死人啊),另外wrar压缩时选择“zip”选项压缩时,再用此类进行解压,也可以正常解压。

在此以做标记,给自己个别人做铺路,以后少走弯路,

如下列出代码,至于dll自己可以去下载,属于开源类库。

调用方式,如下:

[csharp] view plaincopyprint?

    private void btnNewZip_Click(object sender, EventArgs e)  
           {  
               try  
               {  
                   Common.SharpZipLibHelper zip = new Common.SharpZipLibHelper();  
                   //zip.Compress(@"c:\789A", @"c:\789A.zip", true);  
                   zip.Compress(@"c:\db.mdb", @"c:\db.zip", true);  
               }  
               catch { }  
           }  
      
           private void btnNewUnZip_Click(object sender, EventArgs e)  
           {  
               try  
               {  
                   Common.SharpZipLibHelper zip = new Common.SharpZipLibHelper();  
                   //zip.UnZipDirectory(@"c:\789A.zip", @"c:\789test", "");  
                   zip.UnZipDirectory(@"c:\db.zip", @"c:\dbaa", "");  
               }  
               catch { }  
           }  



以下是helper类库,当中修改了一个地方:


[csharp] view plaincopyprint?

    <p>using System;</p><pre class="csharp" name="code">using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.IO;  
    using ICSharpCode.SharpZipLib.Zip;  
    using ICSharpCode.SharpZipLib.Checksums;  
      
    namespace Common  
    {  
        public class SharpZipLibHelper  
        {  
            private byte[] buffer = new byte[2048];  
     
            #region 压缩文件夹,支持递归  
      
            /// <summary>  
            /// 压缩文件夹  
            /// </summary>  
            /// <param name="dir">待压缩的文件夹</param>  
            /// <param name="targetFileName">压缩后文件路径(包括文件名)</param>  
            /// <param name="recursive">是否递归压缩</param>  
            /// <returns></returns>  
            public bool Compress(string dir, string targetFileName, bool recursive)  
            {  
                //如果已经存在目标文件,询问用户是否覆盖  
                if (File.Exists(targetFileName))  
                {  
                    // if (!_ProcessOverwrite(targetFileName))  
                    return false;  
                }  
                string[] ars = new string[2];  
                if (recursive == false)  
                {  
                    //return Compress(dir, targetFileName);  
                    ars[0] = dir;  
                    ars[1] = targetFileName;  
                    return ZipFileDictory(ars);  
                }  
                FileStream ZipFile;  
                ZipOutputStream ZipStream;  
      
                //open  
                ZipFile = File.Create(targetFileName);  
                ZipStream = new ZipOutputStream(ZipFile);  
      
                if (dir != String.Empty)  
                {  
                    _CompressFolder(dir, ZipStream, dir.Substring(3));  
                }  
      
                //close  
                ZipStream.Finish();  
                ZipStream.Close();  
      
                if (File.Exists(targetFileName))  
                    return true;  
                else  
                    return false;  
            }  
      
      
      
      
      
            /// <summary>  
            /// 压缩目录  
            /// </summary>  
            /// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>  
            public static bool ZipFileDictory(string[] args)  
            {  
                ZipOutputStream s = null;  
                try  
                {  
                    string[] filenames = Directory.GetFiles(args[0]);  
      
                    Crc32 crc = new Crc32();  
                    s = new ZipOutputStream(File.Create(args[1]));  
                    s.SetLevel(6);  
      
                    foreach (string file in filenames)  
                    {  
                        //打开压缩文件  
                        FileStream fs = File.OpenRead(file);  
      
                        byte[] buffer = new byte[fs.Length];  
                        fs.Read(buffer, 0, buffer.Length);  
                        ZipEntry entry = new ZipEntry(file);  
      
                        entry.DateTime = DateTime.Now;  
      
                        entry.Size = fs.Length;  
                        fs.Close();  
      
                        crc.Reset();  
                        crc.Update(buffer);  
      
                        entry.Crc = crc.Value;  
      
                        s.PutNextEntry(entry);  
      
                        s.Write(buffer, 0, buffer.Length);  
      
                    }  
      
                }  
                catch (Exception e)  
                {  
                    return false;  
                }  
      
                finally  
                {  
                    s.Finish();  
                    s.Close();  
                }  
                return true;  
            }  
      
      
      
      
      
            /// <summary>  
            /// 压缩某个子文件夹  
            /// </summary>  
            /// <param name="basePath"></param>  
            /// <param name="zips"></param>  
            /// <param name="zipfolername"></param>       
            private static void _CompressFolder(string basePath, ZipOutputStream zips, string zipfolername)  
            {  
                if (File.Exists(basePath))  
                {  
                    _AddFile(basePath, zips, zipfolername);  
                    return;  
                }  
                string[] names = Directory.GetFiles(basePath);  
                foreach (string fileName in names)  
                {  
                    _AddFile(fileName, zips, zipfolername);  
                }  
      
                names = Directory.GetDirectories(basePath);  
                foreach (string folderName in names)  
                {  
                    _CompressFolder(folderName, zips, zipfolername);  
                }  
      
            }  
      
            /// <summary>  
            /// 压缩某个子文件  
            /// </summary>  
            /// <param name="fileName"></param>  
            /// <param name="zips"></param>  
            /// <param name="zipfolername"></param>  
            private static void _AddFile(string fileName, ZipOutputStream zips, string zipfolername)  
            {  
                if (File.Exists(fileName))  
                {  
                    _CreateZipFile(fileName, zips, zipfolername);  
                }  
            }  
      
            /// <summary>  
            /// 压缩单独文件  
            /// </summary>  
            /// <param name="FileToZip"></param>  
            /// <param name="zips"></param>  
            /// <param name="zipfolername"></param>  
            private static void _CreateZipFile(string FileToZip, ZipOutputStream zips, string zipfolername)  
            {  
                try  
                {  
                    FileStream StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);  
                    string temp = FileToZip;  
                    string temp1 = zipfolername;  
                    if (temp1.Length > 0)  
                    {  
                        int i = temp1.LastIndexOf("\\") + 1;//这个地方原来是个bug用的是"//",导致压缩路径过长路径2012-7-2  
                        int j = temp.Length - i;  
                        temp = temp.Substring(i, j);  
                    }  
                    ZipEntry ZipEn = new ZipEntry(temp.Substring(3));  
      
                    zips.PutNextEntry(ZipEn);  
                    byte[] buffer = new byte[16384];  
                    System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);  
                    zips.Write(buffer, 0, size);  
                    try  
                    {  
                        while (size < StreamToZip.Length)  
                        {  
                            int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);  
                            zips.Write(buffer, 0, sizeRead);  
                            size += sizeRead;  
                        }  
                    }  
                    catch (System.Exception ex)  
                    {  
                        throw ex;  
                    }  
      
                    StreamToZip.Close();  
                }  
                catch (Exception e)  
                {  
                    throw e;  
                }  
            }  
            #endregion  
     
     
     
            #region  
      
            /// <summary>  
            /// 解压缩目录  
            /// </summary>  
            /// <param name="zipDirectoryPath">压缩目录路径</param>  
            /// <param name="unZipDirecotyPath">解压缩目录路径</param>  
            public void UnZipDirectory(string zipDirectoryPath, string unZipDirecotyPath, string Password)  
            {  
                while (unZipDirecotyPath.LastIndexOf("\\") + 1 == unZipDirecotyPath.Length)//检查路径是否以"\"结尾  
                {  
                    unZipDirecotyPath = unZipDirecotyPath.Substring(0, unZipDirecotyPath.Length - 1);//如果是则去掉末尾的"\"  
                }  
      
                using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath)))  
                {  
      
                    //判断Password  
                    if (Password != null && Password.Length > 0)  
                    {  
                        zipStream.Password = Password;  
                    }  
      
                    ZipEntry zipEntry = null;  
                    while ((zipEntry = zipStream.GetNextEntry()) != null)  
                    {  
                        string directoryName = Path.GetDirectoryName(zipEntry.Name);  
                        string fileName = Path.GetFileName(zipEntry.Name);  
      
                        if (!string.IsNullOrEmpty(directoryName))  
                        {  
                            Directory.CreateDirectory(unZipDirecotyPath + @"\" + directoryName);  
                        }  
      
                        if (!string.IsNullOrEmpty(fileName))  
                        {  
                            if (zipEntry.CompressedSize == 0)  
                                break;  
                            if (zipEntry.IsDirectory)//如果压缩格式为文件夹方式压缩  
                            {  
                                directoryName = Path.GetDirectoryName(unZipDirecotyPath + @"\" + zipEntry.Name);  
                                Directory.CreateDirectory(directoryName);  
                            }  
                           else//2012-5-28修改,支持单个文件压缩时自己创建目标文件夹  
                            {  
                                if (!Directory.Exists(unZipDirecotyPath))  
                                {  
                                    Directory.CreateDirectory(unZipDirecotyPath);  
                                }  
                            }  
      
                            using (FileStream stream = File.Create(unZipDirecotyPath + @"\" + zipEntry.Name))  
                            {  
                                while (true)  
                                {  
                                    int size = zipStream.Read(buffer, 0, buffer.Length);  
                                    if (size > 0)  
                                    {  
                                        stream.Write(buffer, 0, size);  
                                    }  
                                    else  
                                    {  
                                        break;  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
     
     
            #endregion  
      
      
        }  
    }  
    </pre><br>  
    <br>  
    <p></p>  
    <p></p>  
    <p><br>  
    </p>  
    <pre></pre>  
    <pre></pre>  
    <pre></pre>  
    <pre></pre>  
    <pre></pre>  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值