-4

I have a folder "c:\test" which contains multiple archives. How do I search through all the archives in this folder for a specific file.

This only search a particular archive using ZipPath:

    private void button1_Click(object sender, EventArgs e)
    {
        string zipPath = @"C:\Test\archive1.zip";
        string filetosearch = "testfile";
        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                var position = entry.Name.IndexOf(filetosearch , StringComparison.InvariantCultureIgnoreCase);
                if (position > -1)
                {
                    listView1.Items.Add(entry.Name);
                }
            }
        }
    }

Is it possible to search all the archives in this folder i.e archive1 to archive70

Kurenai Kunai
  • 1,842
  • 2
  • 12
  • 22

3 Answers3

1

You can use the following code:

foreach(var zipPath in Directory.GetFiles("C:\\Test"))
{
    using (ZipArchive archive = ZipFile.OpenRead(zipPath))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            var position = entry.Name.IndexOf(filetosearch , StringComparison.InvariantCultureIgnoreCase);
            if (position > -1)
            {
                listView1.Items.Add(entry.Name);
            }
        }
    }
}

The code gets all the files in the directory and iterates through them. If you need to filter the files by extension you can check it inside the foreach loop.

BVintila
  • 153
  • 5
1

You probably want something along the lines of

string[] filePaths = Directory.GetFiles(@"c:\Test\", "*.zip")

Then change you click code to

foreach(var filePath in filePaths){
    //your code here for each path
}
Daniel Slater
  • 4,123
  • 4
  • 28
  • 39
0

You may use following code

        string[] filePaths = Directory.GetFiles(@"c:\test", "*.zip");
        string filetosearch = "testfile";
        foreach (var item in filePaths)
        {
            string name = Path.GetFileName(item);
            if (name.IndexOf(filetosearch, StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                //item is path of that file
            }
        }
HadiRj
  • 1,015
  • 3
  • 21
  • 41