<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      桃李不言,下自成蹊;軟件工程師,非某語言的程序員

      C#使用ICSharpCode.SharpZipLib.dll壓縮文件夾和文件

      大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下載SharpZiplib的最新版本,本文使用的版本為0.86.0.518,支持Zip, GZip, BZip2 和Tar格式,其實沒啥好說的直接上代碼

          /// <summary>
          /// Zip壓縮與解壓縮 
          /// </summary>
          public class ZipHelper
          {
              /// <summary>
              /// 壓縮單個文件
              /// </summary>
              /// <param name="fileToZip">要壓縮的文件</param>
              /// <param name="zipedFile">壓縮后的文件</param>
              /// <param name="compressionLevel">壓縮等級</param>
              /// <param name="blockSize">每次寫入大小</param>
              public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
              {
                  //如果文件沒有找到,則報錯
                  if (!System.IO.File.Exists(fileToZip))
                  {
                      throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + fileToZip + " 不存在!");
                  }
       
                  using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
                  {
                      using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
                      {
                          using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                          {
                              string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
       
                              ZipEntry ZipEntry = new ZipEntry(fileName);
       
                              ZipStream.PutNextEntry(ZipEntry);
       
                              ZipStream.SetLevel(compressionLevel);
       
                              byte[] buffer = new byte[blockSize];
       
                              int sizeRead = 0;
       
                              try
                              {
                                  do
                                  {
                                      sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                                      ZipStream.Write(buffer, 0, sizeRead);
                                  }
                                  while (sizeRead > 0);
                              }
                              catch (System.Exception ex)
                              {
                                  throw ex;
                              }
       
                              StreamToZip.Close();
                          }
       
                          ZipStream.Finish();
                          ZipStream.Close();
                      }
       
                      ZipFile.Close();
                  }
              }
       
              /// <summary>
              /// 壓縮單個文件
              /// </summary>
              /// <param name="fileToZip">要進行壓縮的文件名</param>
              /// <param name="zipedFile">壓縮后生成的壓縮文件名</param>
              public static void ZipFile(string fileToZip, string zipedFile)
              {
                  //如果文件沒有找到,則報錯
                  if (!File.Exists(fileToZip))
                  {
                      throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + fileToZip + " 不存在!");
                  }
       
                  using (FileStream fs = File.OpenRead(fileToZip))
                  {
                      byte[] buffer = new byte[fs.Length];
                      fs.Read(buffer, 0, buffer.Length);
                      fs.Close();
       
                      using (FileStream ZipFile = File.Create(zipedFile))
                      {
                          using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
                          {
                              string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
                              ZipEntry ZipEntry = new ZipEntry(fileName);
                              ZipStream.PutNextEntry(ZipEntry);
                              ZipStream.SetLevel(5);
       
                              ZipStream.Write(buffer, 0, buffer.Length);
                              ZipStream.Finish();
                              ZipStream.Close();
                          }
                      }
                  }
              }
       
              /// <summary>
              /// 壓縮多層目錄
              /// </summary>
              /// <param name="strDirectory">The directory.</param>
              /// <param name="zipedFile">The ziped file.</param>
              public static void ZipFileDirectory(string strDirectory, string zipedFile)
              {
                  using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
                  {
                      using (ZipOutputStream s = new ZipOutputStream(ZipFile))
                      {
                          ZipSetp(strDirectory, s, "");
                      }
                  }
              }
       
              /// <summary>
              /// 遞歸遍歷目錄
              /// </summary>
              /// <param name="strDirectory">The directory.</param>
              /// <param name="s">The ZipOutputStream Object.</param>
              /// <param name="parentPath">The parent path.</param>
              private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
              {
                  if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
                  {
                      strDirectory += Path.DirectorySeparatorChar;
                  }           
                  Crc32 crc = new Crc32();
       
                  string[] filenames = Directory.GetFileSystemEntries(strDirectory);
       
                  foreach (string file in filenames)// 遍歷所有的文件和目錄
                  {
       
                      if (Directory.Exists(file))// 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件
                      {
                          string pPath = parentPath;
                          pPath += file.Substring(file.LastIndexOf("\\") + 1);
                          pPath += "\\";
                          ZipSetp(file, s, pPath);
                      }
       
                      else // 否則直接壓縮文件
                      {
                          //打開壓縮文件
                          using (FileStream fs = File.OpenRead(file))
                          {
       
                              byte[] buffer = new byte[fs.Length];
                              fs.Read(buffer, 0, buffer.Length);
       
                              string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
                              ZipEntry entry = new ZipEntry(fileName);
       
                              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);
                          }
                      }
                  }
              }
       
              /// <summary>
              /// 解壓縮一個 zip 文件。
              /// </summary>
              /// <param name="zipedFile">The ziped file.</param>
              /// <param name="strDirectory">The STR directory.</param>
              /// <param name="password">zip 文件的密碼。</param>
              /// <param name="overWrite">是否覆蓋已存在的文件。</param>
              public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
              {
       
                  if (strDirectory == "")
                      strDirectory = Directory.GetCurrentDirectory();
                  if (!strDirectory.EndsWith("\\"))
                      strDirectory = strDirectory + "\\";
       
                  using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
                  {
                      s.Password = password;
                      ZipEntry theEntry;
       
                      while ((theEntry = s.GetNextEntry()) != null)
                      {
                          string directoryName = "";
                          string pathToZip = "";
                          pathToZip = theEntry.Name;
       
                          if (pathToZip != "")
                              directoryName = Path.GetDirectoryName(pathToZip) + "\\";
       
                          string fileName = Path.GetFileName(pathToZip);
       
                          Directory.CreateDirectory(strDirectory + directoryName);
       
                          if (fileName != "")
                          {
                              if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
                              {
                                  using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
                                  {
                                      int size = 2048;
                                      byte[] data = new byte[2048];
                                      while (true)
                                      {
                                          size = s.Read(data, 0, data.Length);
       
                                          if (size > 0)
                                              streamWriter.Write(data, 0, size);
                                          else
                                              break;
                                      }
                                      streamWriter.Close();
                                  }
                              }
                          }
                      }
       
                      s.Close();
                  }
              }
       
          }

      代碼來自網絡,略作修改,修改為靜態方法,修改文件夾遞歸壓縮時的bug..

      posted @ 2011-10-19 09:34  假正經哥哥  閱讀(32205)  評論(11)    收藏  舉報
      主站蜘蛛池模板: 又大又粗欧美黑人aaaaa片| 精品亚洲国产成人av| 日韩精品一区二区三区激情视频 | 亚洲不卡一区二区在线看| 亚洲中文字幕在线观看| 欧美一区二区三区性视频| 日韩av日韩av在线| 亚洲精品国产自在现线最新| 久久精品国产99国产精品严洲| 99精品人妻少妇一区| 亚洲日本VA午夜在线电影| 免费av深夜在线观看| 精品午夜福利短视频一区| 2020精品自拍视频曝光| 亚洲成a人片在线观看中| 亚洲成在人线在线播放无码| 中文字幕无码免费久久| 亚洲中文字幕在线二页| 久青草国产在视频在线观看| 色综合中文综合网| 四虎影视一区二区精品| 久久久久青草线综合超碰| 久久综合伊人77777| 国产免费高清69式视频在线观看| 亚洲码和欧洲码一二三四| 亚洲AV成人片不卡无码| 国产成人精品亚洲午夜麻豆| 天堂网在线.www天堂在线资源| av午夜福利亚洲精品福利| 少妇xxxxx性开放| 浪卡子县| 欧美乱码卡一卡二卡四卡免费| 好吊妞人成视频在线观看| 久久精品亚洲中文字幕无码网站| 久久青青草原精品国产app| 久操线在视频在线观看| 国产精品推荐手机在线| 国产老妇伦国产熟女老妇高清 | 蜜桃av亚洲精品一区二区| 神马久久亚洲一区 二区| 99久久免费精品国产色|