天天看點

C#中實作壓縮檔案和檔案夾 .

  1. 【【【【C#壓縮檔案】】】】  
  2. 方法1:  
  3.     //【filepath想要壓縮檔案的位址】   
  4.     //【zippath輸出壓縮檔案的位址】   
  5.     private void GetFileToZip(string filepath,string zippath)  
  6.     {  
  7.         FileStream fs = File.OpenRead(filepath);  
  8.             byte[] buffer = new byte[fs.Length];  
  9.             fs.Read(buffer, 0, buffer.Length);  
  10.             fs.Close();  
  11.             FileStream ZipFile = File.Create(zippath);  
  12.             ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);  
  13.             ZipEntry ZipEntry = new ZipEntry(輸出的檔案名稱);  
  14.             ZipStream.PutNextEntry(ZipEntry);  
  15.             ZipStream.SetLevel(6);  
  16.             ZipStream.Write(buffer, 0, buffer.Length);  
  17.             ZipStream.Finish();  
  18.             ZipStream.Close();  
  19.     }  
  20. 方法2:  
  21.      private void FileToZip(string path,string address)   
  22.         {  
  23.             string name = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\")+1);  
  24.             FileStream StreamToZip = new FileStream(path, FileMode.Open, FileAccess.Read);  
  25.             FileStream ZipFile = File.Create(address);  
  26.             ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);  
  27.             //壓縮檔案   
  28.             ZipEntry ZipEntry = new ZipEntry(name);  
  29.             ZipStream.PutNextEntry(ZipEntry);  
  30.             ZipStream.SetLevel(6);                                         
  31.             byte[] buffer = new byte[StreamToZip.Length];  
  32.             StreamToZip.Read(buffer, 0, Convert.ToInt32(StreamToZip.Length));  
  33.             ZipStream.Write(buffer, 0, Convert.ToInt32(StreamToZip.Length));  
  34.             ZipStream.Finish();  
  35.             ZipStream.Close();  
  36.             StreamToZip.Close();  
  37.         }  
  38. 【【【【【【C#解壓檔案】】】】】】  
  39.     private void ZipToFile(string path, string addres)  
  40.         {  
  41.             ZipInputStream s = new ZipInputStream(File.OpenRead(path));  
  42.             ZipEntry fileEntry;   
  43.             while ((fileEntry = s.GetNextEntry()) != null)  
  44.             {  
  45.                 string filename = Path.GetFileName(fileEntry.Name);  
  46.                 if (filename != "")  
  47.                 {  
  48.                     filename = addres + "\\" + filename;  
  49.                     FileStream streamWriter = File.Create(filename);  
  50.                     int size = 2048;  
  51.                     byte[] buffer = new byte[s.length];  
  52.                     size = s.Read(buffer, 0, size);  
  53.                     streamWriter.Write(buffer, 0, size);  
  54.                     streamWriter.Close();                     
  55.                 }  
  56.             }  
  57.             s.Close();  
  58.         }  
  59. 【【【【【【C#壓縮目錄】】】】】】  
  60. 方法1:  
  61.     //【arg[0]要壓縮的目錄】   
  62.     //【arg[1]輸出的壓縮檔案】   
  63.        private void DirectoryToZip(string path, string address)  
  64.            {  
  65.              //擷取目前檔案夾中所有的檔案   
  66.                 string[] filenames = Directory.GetFiles(path);    
  67.              Crc32 crc = new Crc32();  
  68.              //建立輸出檔案(ZIP格式的檔案)   
  69.              ZipOutputStream zos = new ZipOutputStream(File.Create(address));  
  70.               zos.SetLevel(6);  
  71.              //周遊所有的檔案   
  72.              foreach (string name in filenames)  
  73.              {  
  74.                     FileStream fs = File.OpenRead(name);  
  75.                    byte[] buffer = new byte[fs.Length];  
  76.                   //讀取檔案   
  77.                    fs.Read(buffer, 0, Convert.ToInt32(fs.Length));  
  78.                    //擷取檔案的檔案名稱和字尾名   
  79.                   string file = Path.GetFileName(name);  
  80.                   //輸出檔案的名稱   
  81.                   ZipEntry entry = new ZipEntry(file);                  
  82.                   crc.Reset();  
  83.                   crc.Update(buffer);  
  84.                  entry.Crc = crc.Value;  
  85.                  zos.PutNextEntry(entry);  
  86.                  zos.Write(buffer, 0, Convert.ToInt32(fs.Length));  
  87.                  fs.Close();  
  88.               }  
  89.               zos.Finish();  
  90.                zos.Close();  
  91.             }  
  92. 【【【【【【【【C#讀取壓縮檔案(将壓縮檔案轉換為二進制)】】】】】】】】  
  93.     private void GetZipToByte(){  
  94.         string path = @"C:\Documents and Settings\Administrator\桌面\檔案.rar";  
  95.             FileStream fs = new FileStream(path, FileMode.Open);  
  96.             bytes = new byte[fs.Length];  
  97.             int count = Convert.ToInt32(fs.Length);  
  98.             fs.Read(bytes, 0, count);  
  99.     }  
  100. 【【【【【【【【C#将二進制轉換為壓縮檔案】】】】】】】】  
  101.     private void GetByteToZip()  
  102.     {  
  103.         string path = @"F:\dom.rar";//壓縮檔案的位址   
  104.         File.WriteAllBytes(path, bytes);  
  105.     }