You must set the file name explicitly for the zip-archive:
zip.AddFile(fileName).FileName = System.IO.Path.GetFileName(fileName);
Pergunta
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);
}
Solução
You must set the file name explicitly for the zip-archive:
zip.AddFile(fileName).FileName = System.IO.Path.GetFileName(fileName);
Outras dicas
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