简体   繁体   中英

how to find the file with max date/time inside a zip file having more than one sub directories

In a down stream system Every day one data folder is created within folder Data and files are generated over time within a sub folder TS and finally it's zip with name Data.zip and uploaded to azure blob by customer.

在此处输入图像描述

在此处输入图像描述

Now I am downloading the zip file and trying to find out the one file which has max date/time. using below code I am able to print all files name inside the zip file, but how to get (print) only the file with max date/time?

var blobClient = new BlobClient("conn-string", "upload", "Data.zip");
await DownloadFromStream(blobClient);


public static async Task DownloadFromStream(BlobClient blobClient)
    {
        int i = 0;
        var stream = await blobClient.OpenReadAsync(new BlobOpenReadOptions(false));
        using ZipArchive archive = new ZipArchive(stream);
        foreach (ZipArchiveEntry entry in archive.Entries.OrderBy(x => x.LastWriteTime))
        {
            if (entry.Name.StartsWith("XXX_TS_"))
            {
                i++;
                Console.WriteLine(i);
                Console.WriteLine(entry.Name);
            }
        }
    }

I have tried in the below way, and it worked for me.

  1. Uploaded the below files to azure container in zip folder.

在此处输入图像描述

In Azure portal.

在此处输入图像描述

Code snippet

var blobClient = new BlobClient("ConnectionString", "ContainerName", "Data.zip");
await DownloadFromStream(blobClient);

public static async Task DownloadFromStream(BlobClient blobClient)
        {
            var stream = await blobClient.OpenReadAsync(new BlobOpenReadOptions(false));
            ZipArchive archive = new ZipArchive(stream);
            List<string> sFileslist = new List<string>();
            foreach (ZipArchiveEntry entry in archive.Entries.OrderBy(x => x.LastWriteTime))
            {
                if (entry.Name.Contains("_TS_"))
                {
                    string[] strFileTokens = entry.Name.Split('_');
                    sFileslist.Add(strFileTokens[2]);
                }   
            }
            string maxValue = sFileslist.Max();
            Console.WriteLine(maxValue);
        }

Fetched the Latest file as output.

在此处输入图像描述

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