Nikunj Satasiya

Nikunj Satasiya

  • 275
  • 6.1k
  • 3.5m

How to Rename Files and Folder in .rar .7z, .tar, .zip using

Dec 23 2019 12:38 AM
I have a compressed file .rar .7z, .tar and .zip and I want to rename physical file name available in above compressed archived using C#.
I have tried this using a sharpcompress library but I can't find such a feature for rename file or folder name within .rar .7z, .tar and .zip file.
I also have tried using the DotNetZip library but its only support.Zip she what I tried using DotNetZip library.
  1. private static void RenameZipEntries(string file)  
  2.         {  
  3.             try  
  4.             {  
  5.                 int renameCount = 0;  
  6.                 using (ZipFile zip2 = ZipFile.Read(file))  
  7.                 {  
  8.   
  9.                     foreach (ZipEntry e in zip2.ToList())  
  10.                     {  
  11.                         if (!e.IsDirectory)  
  12.                         {  
  13.                             if (e.FileName.EndsWith(".txt"))  
  14.                             {  
  15.                                 var newname = e.FileName.Split('.')[0] + "_new." + e.FileName.Split('.')[1];  
  16.                                 e.FileName = newname;  
  17.                                 e.Comment = "renamed";  
  18.                                 zip2.Save();  
  19.                                 renameCount++;  
  20.                             }  
  21.                         }  
  22.                     }  
  23.                     zip2.Comment = String.Format("This archive has been modified. {0} files have been renamed.", renameCount);  
  24.                     zip2.Save();  
  25.                 }  
  26.             }  
  27.             catch (Exception ex)  
  28.             {  
  29.                 MessageBox.Show(ex.ToString());  
  30.             }  
  31.   
  32.         }  
But actually the same as above I also want for .7z, .rar and .tar, I tried many libraries but still I didn't get any accurate solution.
Please help me.

Answers (1)