多個文件和文件夾生成壓縮包

#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;

        }

發佈了38 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章