简体   繁体   中英

Zipping file with DotNetZip

I am trying to zip and encrypt a chosen file from the user. Everything works fine except that I am zipping the whole path, ie 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);
}

您必须为zip归档文件明确设置文件名:

zip.AddFile(fileName).FileName = System.IO.Path.GetFileName(fileName);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM