1

Is there any possible solutions for C# how to check if the file exists even if the file is located in the .zip directory?

A possible path would be: "\\127.0.0.1\ZIP-Bug-Study\TestResultMap\market_coupling_ntc_preoptimization\temp.zip\file.csv"

I would need a solution without splitting the string and so on, simply something like:

FileInfo fi = new FileInfo(path);
if (fi.Exists) { }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ostap Filipenko
  • 235
  • 5
  • 21
  • 1
    Files don't exist "in the .zip directory". There's no such thing as a zip directory. The ZIP archive is itself a file. File Explorer conveniently displays them as though they are folders containing files but they are not. ZIP files are files so you have to treat them that way. .NET provides types in `System.IO.Compression` for working with ZIP files, including accessing the entries within one. It is those entries that File Explorer displays as though they were actual files. You need to do some research on how to read ZIP files in .NET and get the entries within them. – John Jul 15 '22 at 06:17

1 Answers1

1

With .Net Framework 4.5 and ZipArchive

using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Open)){
     try{
        ZipArchiveEntry entry = archive.GetEntry("Existing Filepath with name"); // use entry then
     }
     catch(ex Exception ) { if(ex is ArgumentNullException || ex is ArgumentException) Console.Write("file not found"); }}

Replace ZipArchiveMode.Open with update if you need modifying the file.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
redParrot
  • 440
  • 1
  • 7
  • 14
  • @UweKeim it's Exceptions handling for search a null path &... not error handling. Actually it is the simplest method I could write as he wants simple code, in his question. But yes it is not recommended :) – redParrot Jul 21 '22 at 17:50