2010-09-17 83 views
1

可能重複:
.Net Zip Up files如何使用C#創建一個zip文件?

我想用C#來壓縮.csv文件。我如何做到這一點?

+0

不是一個真正的問題,請添加驗證 - 投票關閉... – t0mm13b 2010-09-17 13:10:22

+1

@ tommieb75:OP可能不是母語,但我們仍然可以幫助他作爲基本問題(如何在.NET中創建zip文件)非常清晰。儘管這個問題可能會被視爲一個愚蠢的行爲。 – 2010-09-17 13:15:46

+0

@ user430607,我確定您一定聽說過[本網站](http://www.google.com/)。你知道那個帶有文本框和底部的兩個按鈕。有時他們可能會有用。嘗試一下。你也注意到了這個頁面右上角的文本框(常見問題旁邊的那個)?這是有原因的。 – 2010-09-17 13:23:31

回答

0

荏苒你需要使用SharpZipLib利亞姆韋斯特利的文章。

閒逛的例子,你應該能夠從那裏找到答案......

2

斐伊川...

使用此代碼...你自己

using System; 
using System.Collections; 
using System.IO; 
using ICSharpCode.SharpZipLib.Zip; 

namespace FolderZipper 
{ 
    public static class ZipUtil 
    { 
     public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password) 
     { 
      ArrayList ar = GenerateFileList(inputFolderPath); // generate file list 
      int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length; 
      // find number of chars to remove  // from orginal file path 
      TrimLength += 1; //remove '\' 
      FileStream ostream; 
      byte[] obuffer; 
      string outPath = inputFolderPath + @"\" + outputPathAndFile; 
      ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream 
      if (password != null && password != String.Empty) 
       oZipStream.Password = password; 
      oZipStream.SetLevel(9); // maximum compression 
      ZipEntry oZipEntry; 
      foreach (string Fil in ar) // for each file, generate a zipentry 
      { 
       oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength)); 
       oZipStream.PutNextEntry(oZipEntry); 

       if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory 
       { 
        ostream = File.OpenRead(Fil); 
        obuffer = new byte[ostream.Length]; 
        ostream.Read(obuffer, 0, obuffer.Length); 
        oZipStream.Write(obuffer, 0, obuffer.Length); 
       } 
      } 
      oZipStream.Finish(); 
      oZipStream.Close(); 
     } 


     private static ArrayList GenerateFileList(string Dir) 
     { 
      ArrayList fils = new ArrayList(); 
      bool Empty = true; 
      foreach (string file in Directory.GetFiles(Dir)) // add each file in directory 
      { 
       fils.Add(file); 
       Empty = false; 
      } 

      if (Empty) 
      { 
       if (Directory.GetDirectories(Dir).Length == 0) 
        // if directory is completely empty, add it 
       { 
        fils.Add(Dir + @"/"); 
       } 
      } 

      foreach (string dirs in Directory.GetDirectories(Dir)) // recursive 
      { 
       foreach (object obj in GenerateFileList(dirs)) 
       { 
        fils.Add(obj); 
       } 
      } 
      return fils; // return file list 
     } 


     public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile) 
     { 
      ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile)); 
      if (password != null && password != String.Empty) 
       s.Password = password; 
      ZipEntry theEntry; 
      string tmpEntry = String.Empty; 
      while ((theEntry = s.GetNextEntry()) != null) 
      { 
       string directoryName = outputFolder; 
       string fileName = Path.GetFileName(theEntry.Name); 
       // create directory 
       if (directoryName != "") 
       { 
        Directory.CreateDirectory(directoryName); 
       } 
       if (fileName != String.Empty) 
       { 
        if (theEntry.Name.IndexOf(".ini") < 0) 
        { 
         string fullPath = directoryName + "\\" + theEntry.Name; 
         fullPath = fullPath.Replace("\\ ", "\\"); 
         string fullDirPath = Path.GetDirectoryName(fullPath); 
         if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath); 
         FileStream streamWriter = File.Create(fullPath); 
         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(); 
      if (deleteZipFile) 
       File.Delete(zipPathAndFile); 
     } 
    } 
} 
+1

您應該使用編輯器工具欄中的代碼格式化按鈕來使您的發佈可讀。 – 2010-09-17 13:17:40

+0

請正確格式化您的代碼。首先複製粘貼或編寫代碼,然後選擇代碼,然後單擊代碼按鈕:) – Searock 2010-09-17 13:24:37

+0

thnxs的建議..下次肯定會記住這一點 – 2010-09-19 16:36:31