Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DotNetZip - rename file entry in zip file while compressing

Tags:

Using DotNetZip, is it possible to compress a file such that the zip lists a different file name for a file than the file name on disk? For example, I want to add myFile.txt to a zip file, but I want it to be called otherFile.txt.

like image 628
SFun28 Avatar asked Jun 20 '11 14:06

SFun28


People also ask

How do I Rename a compressed ZIP file?

Click the file or folder, then press F2. Click the file or folder twice, slowly. Right-click the file or folder and choose Rename from the shortcut menu.

Can you Rename a ZIP file extension?

Right-click the file or selection of files, and select Add to zip. Right-click the resulting archive and select Rename. Change the last three letters to a file extension not blocked by the attachment filter (for example, txt ), or remove the extension altogether.

How do you Rename a ZIP file on a PC?

Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location. To rename it, press and hold (or right-click) the folder, select Rename, and then type the new name.


2 Answers

From the DotNetZip FAQ:

Add an entry, overriding its name in the archive

  using (ZipFile zip1 = new ZipFile())   {       zip1.AddFile("myFile.txt").FileName = "otherFile.txt";        zip1.Save(archiveName);   } 
like image 96
Martin Avatar answered Nov 09 '22 04:11

Martin


        var zip = new ZipFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.zip"));         var e = zip.AddFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "testfile.pdf"), "/");         e.FileName = "PewPewGotcha.pdf";         zip.Save(); 

Once the ZipFile is saved, the name is updated.

like image 25
Bob G Avatar answered Nov 09 '22 03:11

Bob G