c#将Directory.GetFiles添加到Zip文件中

时间:2017-07-06 18:14:30

标签: c# file zip

我在一个文件夹中有很多日志,我想只抓取今天有日期的文件并将它们放在zip文件中。

这是我的代码:

static void Main(string[] args)
{
    //Specify todays date
    DateTime todaysDate = DateTime.Today;

    //Create a zip file with the name logs + todays date
    string zipPath = @"C:\Users\Desktop\ZIP\logs" + todaysDate.ToString("yyyyMMdd") + ".zip";
    string myPath = @"C:\Users\Desktop\LOG SEARCH";

    var files = System.IO.Directory.GetFiles(myPath, "*" + todaysDate.ToString("yyyyMMdd") + "*");

    foreach (var file in files)
    {
        Console.WriteLine(file);
    }
}

如何压缩files

2 个答案:

答案 0 :(得分:3)

因此,您可以创建一个临时文件夹,然后添加与其中当前日期匹配的每个文件。完成后,您可以DateTime todaysDate = DateTime.Today; //Create a zip file with the name logs + todays date string zipPath = @"C:\Users\Desktop\ZIP\logs" + todaysDate.ToString("yyyyMMdd") + ".zip"; string myPath = @"C:\Users\Desktop\LOG SEARCH"; string tempPath = @"C:\Users\Desktop\ZIP\logs" + todaysDate.ToString("yyyyMMdd"); var files = System.IO.Directory.GetFiles(myPath, "*" + todaysDate.ToString("yyyyMMdd") + "*"); Directory.CreateDirectory(tempPath); foreach (var file in files) { File.Copy(file, tempPath + @"\" + System.IO.Path.GetFileName(file)); } ZipFile.CreateFromDirectory(tempPath, zipPath); Directory.Delete(tempPath, true); 然后删除临时文件夹

@Pipe({name: 'highlightedWorksFilter', pure: false})
export class HighlightedWorksFilterPipe implements PipeTransform {
transform(works: IHighlightedWork[], activityTypeIds: any[], activityYears: any[]){
    if (works && works.length) {
        return works.filter(work => {
            if(activityTypeIds.findIndex(i => i.id === work.activityTypeId && i.checked) === -1) {
                return false;
            }
            if (activityYears.findIndex(i => i.year === work.activityYear && i.checked) === -1) {
                return false;
            }
            return true;
        });
    }
    else {
        return works;
    }
}
}

答案 1 :(得分:0)

您需要使用System.IO.Compression; 并使用

 ZipFile.CreateFromDirectory(myPath, zipPath);