Csharp/C#教程:C#中关于zip压缩解压帮助类的封装 附源码下载分享

c#下压缩解压,主要是用第三方类库进行封装的。ICSharpCode.SharpZipLib.dll类库,链接地址为你官方下载链接。压缩主要是用流的方式进行压缩的。

压缩文件及文件夹。文件压缩很简单,把待压缩的文件用流的方式读到内存中,然后放到压缩流中。就可以了。文件夹就稍微麻烦下了。因为要把待压缩的文件夹解压后保留文件夹文件的层次结构。所以我的实现方式就是递归遍历文件夹中的文件。计算其相对位置放到压缩流中。

代码如下
代码如下:
///<summary>
       ///压缩文件或者文件夹
       ///</summary>
       ///<paramname=”_depositPath”>压缩后文件的存放路径  如C:\windowsabc.zip</param>
       ///<returns></returns>
       publicboolCompressionZip(string_depositPath)
       {
           boolresult=true;
           FileStreamfs=null;
           try
           {
               ZipOutputStreamComStream=newZipOutputStream(File.Create(_depositPath));
               ComStream.SetLevel(9);     //压缩等级
               foreach(stringpathinAbsolutePaths)
               {
                   //如果是目录
                   if(Directory.Exists(path))
                   {
                       ZipFloder(path,ComStream,path);
                   }
                   elseif(File.Exists(path))//如果是文件
                   {
                        fs=File.OpenRead(path);
                       byte[]bts=newbyte[fs.Length];
                       fs.Read(bts,0,bts.Length);
                       ZipEntryze=newZipEntry(newFileInfo(path).Name);
                       ComStream.PutNextEntry(ze);            //为压缩文件流提供一个容器
                       ComStream.Write(bts,0,bts.Length); //写入字节
                   }
               }
               ComStream.Finish();//结束压缩
               ComStream.Close();
   &nb sp;       }
           catch(Exceptionex)
           {
               if(fs!=null)
               {
                   fs.Close();
               }
               errorMsg=ex.Message;
               result=false;
           }
           returnresult;
       }
       //压缩文件夹
       privatevoidZipFloder(string_OfloderPath,ZipOutputStreamzos,string_floderPath)
       {
           foreach(FileSystemInfoiteminnewDirectoryInfo(_floderPath).GetFileSystemInfos())
           {
               if(Directory.Exists(item.FullName))
               {
                   ZipFloder(_OfloderPath,zos,item.FullName);
               }
               elseif(File.Exists(item.FullName))//如果是文件
               {
                   DirectoryInfoODir=newDirectoryInfo(_OfloderPath);
                   stringfullName2=newFileInfo(item.FullName).FullName;
                   stringpath=ODir.Name+fullName2.Substring(ODir.FullName.Length,fullName2.Length-ODir.FullName.Length);//获取相对目录
                   FileStreamfs=File.OpenRead(fullName2);
                   byte[]bts=newbyte[fs.Length];
                   fs.Read(bts,0,bts.Length);
                   ZipEntryze=newZipEntry(path);
                   zos.PutNextEntry(ze);            //为压缩文件流提供一个容器
                   zos.Write(bts,0,bts.Length); //写入字节
               }
           }
       }

关于解压 解压就简单多了。有文件解压文件,有文件夹遍历,解压其中的文件。解压的文件中已经包含了其与文件夹的层次关系。
代码如下:
///<summary>
       ///解压
       ///</summary>
       ///<paramname=”_depositPath”>压缩文件路径</param>
       ///<paramname=”_floderPath”>解压的路径</param>
       ///<returns></returns>
       publicboolDeCompressionZip(string_depositPath,string_floderPath)
       {
           boolresult=true;
           FileStreamfs=null;
           try
           {
               ZipInputStreamInpStream=newZipInputStream(File.OpenRead(_depositPath));
               ZipEntryze=InpStream.GetNextEntry();//获取压缩文件中的每一个文件
               Directory.CreateDirectory(_floderPath);//创建解压文件夹
               while(ze!=null)//如果解压完ze则是null
               {
                   if(ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件免费精选名字大全是文件夹\文件名
                   {
                       string[]strs=ze.Name.Split(‘\’);//如果文件名中包含’\‘则表明有文件夹
                       if(strs.Length>1)
                       {
                           //两层循环用于一层一层创建文件夹
                           for(inti=0;i<strs.Length-1;i++)
                           {
                               stringfloderPath=_floderPath;
                               for(intj=0;j<i;j++)
                               {
                           &nbsp ;       floderPath=floderPath+”\”+strs[j];
                               }
                               floderPath=floderPath+”\”+strs[i];
                               Directory.CreateDirectory(floderPath);
                           }
                       }
                        fs=newFileStream(_floderPath+”\”+ze.Name,FileMode.OpenOrCreate,FileAccess.Write);//创建文件
                       //循环读取文件到文件流中
                       while(true)
                       {
                           byte[]bts=newbyte[1024];
                          inti=InpStream.Read(bts,0,bts.Length);
                          if(i>0)
                          {
                              fs.Write(bts,0,i);
                          }
                          else
                          {
                              fs.Flush();
                              fs.Close();
                              break;
                          }
                       }
        &nbs p;          }
                   ze=InpStream.GetNextEntry();
               }
           }
           catch(Exceptionex)
           {
               if(fs!=null)
               {
                   fs.Close();
               }
               errorMsg=ex.Message;
               result=false;
           }
           returnresult;
       }

最后做个上述就是C#学习教程:C#中关于zip压缩解压帮助类的封装 附源码下载分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/904415.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐