2013-11-28 79 views
0

我想將一個文件夾壓縮成帶有.zz擴展名的文件,並帶有7zip。如何壓縮C#中7Zip的文件夾?

我想知道我怎麼會做這個,因爲我不知道(這就是爲什麼我會問。)

這是C#。

鏈接到頁面或示例代碼會有幫助。

+1

這個問題的可能的複製,http://stackoverflow.com/questions/7646328/how-to-use-the-7z-sdk-to-compress-and-decompress-a-file –

+0

對於簡單的解決方案,使用['Process.Start'](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(v = vs.110).aspx)(這需要7zip被安裝)。否則,請參閱[LZMA SDK](http://www.7-zip.org/sdk.html) –

+0

我只是簡單地將它壓縮到.7z文件,就是這樣。 – user3026440

回答

0

代碼來壓縮或使用7zip的

該代碼用於壓縮的文件夾

public void CreateZipFolder(string sourceName, string targetName) 
     { 
      // this code use for zip a folder 
      sourceName = @"d:\Data Files"; // folder to be zip 
      targetName = @"d:\Data Files.zip"; // zip name you can change 
      ProcessStartInfo p = new ProcessStartInfo(); 
      p.FileName = @"D:\7-Zip\7z.exe"; 
      p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9"; 
      p.WindowStyle = ProcessWindowStyle.Hidden; 
      Process x = Process.Start(p); 
      x.WaitForExit(); 
     } 

該代碼用於壓縮文件

public void CreateZip(string sourceName, string targetName) 
     { 
      // file name to be zip , you must provide file name with extension 
      sourceName = @"d:\ipmsg.log"; 
      // targeted file , you can change file name 
      targetName = @"d:\ipmsg.zip"; 

      ProcessStartInfo p = new ProcessStartInfo(); 
      p.FileName = @"D:\7-Zip\7z.exe"; 
      p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9"; 
      p.WindowStyle = ProcessWindowStyle.Hidden; 
      Process x = Process.Start(p); 
      x.WaitForExit(); 

     } 
解壓縮文件

該代碼用於用於解壓縮

public void ExtractFile(string source, string destination) 
     { 
      // If the directory doesn't exist, create it. 
      if (!Directory.Exists(destination)) 
       Directory.CreateDirectory(destination); 

      string zPath = @"D:\7-Zip\7zG.exe"; 
      try 
      { 
       ProcessStartInfo pro = new ProcessStartInfo(); 
       pro.WindowStyle = ProcessWindowStyle.Hidden; 
       pro.FileName = zPath; 
       pro.Arguments = "x \"" + source + "\" -o" + destination; 
       Process x = Process.Start(pro); 
       x.WaitForExit(); 
      } 
      catch (System.Exception Ex) { } 
     } 

image is used to know files in 7zip folder

0

這裏fileDirPath是我的文件夾的路徑,它具有我所有的文件,preferredPath是我希望我的.zip文件所在的路徑。

例如: var fileDirePath = @「C:\ Temp」; var prefferedPath = @「C:\ Output \ results.zip」;

private void CreateZipFile(string fileDirPath, string prefferedPath) 
    { 
     ProcessStartInfo p = new ProcessStartInfo(); 
     p.FileName = @"C:\Program Files\7-Zip\7z.exe"; 
     p.Arguments = "a \"" + prefferedPath + "\" \"" + fileDirPath + "\""; 
     p.WindowStyle = ProcessWindowStyle.Hidden; 
     Process x = Process.Start(p); 
     x.WaitForExit(); 
     return; 
    }