user584018
user584018

Reputation: 11364

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.

enter image description here

enter image description here

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

Upvotes: 0

Views: 72

Answers (1)

Rajesh  Mopati
Rajesh Mopati

Reputation: 1514

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

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

enter image description here

In Azure portal.

enter image description here

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.

enter image description here

Upvotes: 1

Related Questions