欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中关于zip压缩解压帮助类的封装 附源码下载

程序员文章站 2023-12-13 14:19:34
c#下压缩解压,主要是用第三方类库进行封装的。icsharpcode.sharpziplib.dll类库,链接地址为你官方下载链接。压缩主要是用流的方式进行压缩的。 压缩...
c#下压缩解压,主要是用第三方类库进行封装的。icsharpcode.sharpziplib.dll类库,链接地址为你官方下载链接。压缩主要是用流的方式进行压缩的。

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

代码如下

复制代码 代码如下:

/// <summary>
        /// 压缩文件或者文件夹
        /// </summary>
        /// <param name="_depositpath">压缩后文件的存放路径   如c:\\windows\abc.zip</param>
        /// <returns></returns>
        public bool compressionzip(string _depositpath)
        {
            bool result = true;
            filestream fs = null;
            try
            {
                zipoutputstream comstream = new zipoutputstream(file.create(_depositpath));
                comstream.setlevel(9);      //压缩等级
                foreach (string path in absolutepaths)
                {
                    //如果是目录
                    if (directory.exists(path))
                    {
                        zipfloder(path, comstream, path);
                    }
                    else if (file.exists(path))//如果是文件
                    {
                         fs = file.openread(path);
                        byte[] bts = new byte[fs.length];
                        fs.read(bts, 0, bts.length);
                        zipentry ze = new zipentry(new fileinfo(path).name);
                        comstream.putnextentry(ze);             //为压缩文件流提供一个容器
                        comstream.write(bts, 0, bts.length);  //写入字节
                    }
                }
                comstream.finish(); // 结束压缩
                comstream.close();
            }
            catch (exception ex)
            {
                if (fs != null)
                {
                    fs.close();
                }
                errormsg = ex.message;
                result = false;
            }
            return result;
        }
        //压缩文件夹
        private void zipfloder(string _ofloderpath, zipoutputstream zos, string _floderpath)
        {
            foreach (filesysteminfo item in new directoryinfo(_floderpath).getfilesysteminfos())
            {
                if (directory.exists(item.fullname))
                {
                    zipfloder(_ofloderpath, zos, item.fullname);
                }
                else if (file.exists(item.fullname))//如果是文件
                {
                    directoryinfo odir = new directoryinfo(_ofloderpath);
                    string fullname2 = new fileinfo(item.fullname).fullname;
                    string path = odir.name + fullname2.substring(odir.fullname.length, fullname2.length - odir.fullname.length);//获取相对目录
                    filestream fs = file.openread(fullname2);
                    byte[] bts = new byte[fs.length];
                    fs.read(bts, 0, bts.length);
                    zipentry ze = new zipentry(path);
                    zos.putnextentry(ze);             //为压缩文件流提供一个容器
                    zos.write(bts, 0, bts.length);  //写入字节
                }
            }
        }

关于解压  解压就简单多了。有文件解压文件,有文件夹 遍历,解压其中的文件。解压的文件中已经包含了其与文件夹的层次关系。

复制代码 代码如下:

/// <summary>
        /// 解压
        /// </summary>
        /// <param name="_depositpath">压缩文件路径</param>
        /// <param name="_floderpath">解压的路径</param>
        /// <returns></returns>
        public bool decompressionzip(string _depositpath, string _floderpath)
        {
            bool result = true;
            filestream fs=null;
            try
            {
                zipinputstream inpstream = new zipinputstream(file.openread(_depositpath));
                zipentry ze = inpstream.getnextentry();//获取压缩文件中的每一个文件
                directory.createdirectory(_floderpath);//创建解压文件夹
                while (ze != null)//如果解压完ze则是null
                {
                    if (ze.isfile)//压缩zipinputstream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名
                    {
                        string[] strs=ze.name.split('\\');//如果文件名中包含'\\‘则表明有文件夹
                        if (strs.length > 1)
                        {
                            //两层循环用于一层一层创建文件夹
                            for (int i = 0; i < strs.length-1; i++)
                            {
                                string floderpath=_floderpath;
                                for (int j = 0; j < i; j++)
                                {
                                    floderpath = floderpath + "\\" + strs[j];
                                }
                                floderpath=floderpath+"\\"+strs[i];
                                directory.createdirectory(floderpath);
                            }
                        }
                         fs = new filestream(_floderpath+"\\"+ze.name, filemode.openorcreate, fileaccess.write);//创建文件
                        //循环读取文件到文件流中
                        while (true)
                        {
                            byte[] bts = new byte[1024];
                           int i= inpstream.read(bts, 0, bts.length);
                           if (i > 0)
                           {
                               fs.write(bts, 0, i);
                           }
                           else
                           {
                               fs.flush();
                               fs.close();
                               break;
                           }
                        }
                    }
                    ze = inpstream.getnextentry();
                }
            }
            catch (exception ex)
            {
                if (fs != null)
                {
                    fs.close();
                }
                errormsg = ex.message;
                result = false;
            }
            return result;
        }

最后做个总结。c#作为高级语言,其强大的类库和第三方提供的类库。可以做很多事情。但也有弊端,用第三方类库性能不是很高。我压缩几百m的东西。cpu瞬间跑到50%多。比360压缩和zip压缩性能差远了。所以此类也就适用压缩比较小的东西。

完整例子下载地址

上一篇:

下一篇: