简体   繁体   中英

How to get text of .dat files of the zip files which are available in sub folder of one zip file in asp.net core 6.0?

my IFormFile zip file contains multiple folders which again contains zip files which contains.dat types files, so need to read this.dat file's text data.

I tried like below

[HttpPost]
    public async Task<IActionResult> RequestFiles()
    {
        try
        {
            IFormFile file = Request.Form.Files[0];
            Stream stream = file.OpenReadStream();
            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
            {
                foreach (var entry in archive.Entries)
                {
                    if (entry.Name.ToLower().Contains(".zip"))
                    {
                        MemoryStream nestedArchiveStream = new MemoryStream();

                        entry.Open().CopyTo(nestedArchiveStream);
                        
                        using (var nestedArchive = new ZipArchive(nestedArchiveStream))
                        {
                            var datFile = nestedArchive.Entries[0];

                            StreamReader reader = new StreamReader(datFile.ToString());
                            string strAllFile = reader.ReadToEnd();
                        }
                    }
                }
            }
            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

Error: Could not find file 'D:\project'spath\WPNN788_4_11-15-22_084023.dat'.

Just because the codes datFile.ToString() returns the relative path.

在此处输入图像描述

However,the constructor of StreamReader requires the full path

在此处输入图像描述

And the error indicates:If you pass a relative path into the constructor,it would treate the path like:

var fullpath = Path.Combine(System.Environment.CurrentDirectory, relativepath);

You could try to pass the stream instead of the path into the constructor:

StreamReader reader = new StreamReader(datFile.Open());

I debugged and the result:

在此处输入图像描述

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