0

I am trying to zip and encrypt a chosen file from the user. Everything works fine except that I am zipping the whole path, i.e not the file itself. Below is my code, any help on how I can zip and encrypt on the chosen file.

openFileDialog1.ShowDialog();
var fileName = string.Format(openFileDialog1.FileName);
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

using (ZipFile zip = new ZipFile())
{
    zip.Password = "test1";
    zip.Encryption = EncryptionAlgorithm.WinZipAes256;
    zip.AddFile(fileName);
    zip.Save(path + "\\test.ZIP");
    MessageBox.Show("File Zipped!", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
user3274252
  • 57
  • 1
  • 6

2 Answers2

2

You must set the file name explicitly for the zip-archive:

zip.AddFile(fileName).FileName = System.IO.Path.GetFileName(fileName);
user2964420
  • 148
  • 1
  • 8
  • Seems kinda lame... There is a method called AddFile(string fileName) but you have to specify the FileName property of its return value to make it work?!? – Rand Random Apr 29 '14 at 07:24
1

This is how you can zip up a file, and rename it within the archive . Upon extraction, a file will be created with the new name.

using (ZipFile zip1 = new ZipFile())
{
   string newName= fileToZip + "-renamed";
   zip1.AddFile(fileToZip).FileName = newName; 
   zip1.Save(archiveName);
}

Reference : C# Examples

Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36