簡體   English   中英

使用DotNetZip壓縮文件

[英]Zipping file with DotNetZip

我正在嘗試壓縮和加密用戶選擇的文件。 一切正常,除了我壓縮整個路徑,即不壓縮文件本身。 以下是我的代碼,以及有關如何對所選文件進行壓縮和加密的任何幫助。

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);

這是您可以壓縮文件並在存檔中重命名的方法。 提取后,將使用新名稱創建文件。

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

參考: C#示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM