多个文件和文件夹生成压缩包

#region GQY 生成多个文件夹压缩包

                   string zipFilePath =string.Empty;

                   if (dirPath.EndsWith("\\"))

                   {

                        zipFilePath =dirPath.Substring(0, dirPath.Length - 1);

                   }

                   zipFilePath = zipFilePath + ".zip";

                   using (ZipOutputStreams =newZipOutputStream(File.Create(zipFilePath)))

                   {

                        for(inti = 0; i < secondFile.Length; i++)

                        {

                           ZipFileDictory(dirPath + secondFile[i], s,"");

                        }

                   }

                   #endregion

 

///<summary>

        ///递归压缩文件夹方法

        ///</summary>

        ///<paramname="FolderToZip">最内层文件夹,直接可以读取文件</param>

        ///<paramname="s">ZipOutputStream</param>

        ///<paramname="ParentFolderName">FolderToZip上层如果是根目录,为空。否则就是这个文件夹</param>

        privateboolZipFileDictory(string FolderToZip,ZipOutputStream s,stringParentFolderName)

        {

            boolres = true;

            string[]folders, filenames;

            ZipEntry entry =null;

            FileStream fs =null;

            Crc32 crc =newCrc32();

            try

            {

               //创建当前文件夹

               entry = new ZipEntry(Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip)+"\\")); //加上 “/”才会当成是文件夹创建

               s.PutNextEntry(entry);

               s.Flush();

               //先压缩文件,再递归压缩文件夹

               filenames = Directory.GetFiles(FolderToZip);

               foreach (stringfilein filenames)

               {

                   //打开压缩文件

                   fs = File.OpenRead(file);

                   byte[] buffer =newbyte[fs.Length];

                   fs.Read(buffer, 0, buffer.Length);

                   entry = new ZipEntry(Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip)+"\\" + Path.GetFileName(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

            {

               res = false;

            }

            finally

            {

               if (fs != null)

               {

                   fs.Close();

                   fs = null;

               }

               if (entry != null)

               {

                   entry = null;

               }

               GC.Collect();

               GC.Collect(1);

            }

           folders = Directory.GetDirectories(FolderToZip);

 

            foreach(stringfolderin folders)

            {

               if (!ZipFileDictory(folder, s,Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip))))

               {

                   return false;

               }

            }

            returnres;

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值