Unzip only one file from zip folder using c#

Binumon George 161 Reputation points
2021-11-22T15:42:38.547+00:00

Hi All,
Consider in the folder containing 10 files named like abc.txt, pqr.docx, xyz.txt, etc. I am zipping this folder using c# code. Now I want to Unzip only abc.txt from that zip file. how it's possible using c# code?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2021-11-22T15:54:23.85+00:00

    Ignoring the challenges of finding an arbitrary file in a subfolder contained within the zip file then you just need to find the zip entry first and then extract it.

    static bool ExtractFile ( string zipFile, string filename, string targetPath )
    {
        var zip = ZipFile.OpenRead(zipFile);
        var entry = zip.GetEntry(filename);
        if (entry == null)
            return false;
    
        entry.ExtractToFile(targetPath, true);
        return true;
    }
    
    if (!ExtractFile("myzip.zip", "abc.txt", "c:\destination\abc.txt"))
       //Not found
    

    If you need to find the file in an arbitrary subfolder then you'll need to search for it via the Entries.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.