Ionic Zip无法压缩文件夹C#

时间:2016-04-18 05:19:31

标签: c# zip directory

我正在尝试使用C#中的IonicZip库压缩文件夹。它能够在文件夹中压缩.txt,.exe,.pdf等,但不能压缩文件夹中的文件夹。

Folder

它可以压缩MusicLogs.txt和Video.exe但不能将音乐文件夹压缩。音乐文件夹也包含一些文件夹。即使在调试时它也看不到Music文件夹。我的代码是:

string zipPath = @"C:\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".zip"; // zipped file extracts here
string filename = @"E:\"; // the fodler which should be zipped. File must be exist

using (ZipFile zipFile = new ZipFile())
{
    zipFile.Password = "asd";
    zipFile.Encryption = EncryptionAlgorithm.PkzipWeak;
    foreach (string file in Directory.GetFiles(filename)) // this foreach is for getting all files in a folder. 
    {
        zipFile.AddFile(file, "YESMusic"); // set file
    }

    zipFile.Save(zipPath);

}

问题出在哪里?需要更改AddFile功能吗?感谢

1 个答案:

答案 0 :(得分:1)

这将有效:

using (ZipFile zipFile = new ZipFile())
{
    zipFile.Password = "asd";
    zipFile.Encryption = EncryptionAlgorithm.PkzipWeak;          
    // Adding folders in the base directory
    foreach (var item in Directory.GetDirectories(filename))
    {
        string folderName = new DirectoryInfo(item).Name;
        zipFile.AddDirectory(item, folderName);
    }
    // Adding files in the base directory
    foreach (string file in Directory.GetFiles(filename))
    {
        zipFile.AddFile(file); 
    }
    zipFile.Save(zipPath);
}