0

In my project I'm uploading a 7Zip, Zip, Tar, Winzip, WinRAR, and etc file.

Now I want to check if particular file is present in uploaded archive using C#.

Can anyone please help me?

krzyski
  • 318
  • 2
  • 15
Nikunj Satasiya
  • 831
  • 9
  • 25
  • 2
    What have you tried ? https://stackoverflow.com/help/how-to-ask – HariHaran Oct 18 '19 at 09:43
  • Why do you need to check if it exists *in* WinRAR, 7Zip, Zip, Tar or WinZip as opposed to just checking if it exists? – Alan Kavanagh Oct 18 '19 at 09:43
  • 1
    Something like here :https://stackoverflow.com/questions/5978567/detecting-if-a-file-is-an-archive-using-7zip – Francois Borgies Oct 18 '19 at 09:44
  • In my project, the user will upload a WinRAR, 7Zip, Zip, Tar, or Winzip file but for few validations such as uploaded zip contains valid files/folders, there is any virus within uploaded files or any invalid files or any other resources or files which we not allowing, etc. – Nikunj Satasiya Oct 18 '19 at 09:46
  • i.g, I have 3 different file ABC1.txt, ABC2.txt, and ABC3.txt now I creating WinRAR file for the same. Now as per my record WinRAR file is ok there are 3 different files, but now inbetween someone open that file in the server and by mistake delete any file supposes ABC2.txt is deleted then while I extract WinRAR file then ABC2.txt will not found and as per I further depended/functionality of my project logic my code will throw custom exception file ABC2.txt FILE NOT FOUND. – Nikunj Satasiya Oct 18 '19 at 09:51
  • https://stackoverflow.com/questions/307774/how-can-i-list-the-contents-of-a-zip-folder-in-c – xdtTransform Oct 18 '19 at 09:58
  • @NikunjSatasiya For .zip is simple using deflate streams in C#. For .rar is more complicated because it is a closed format, you'll need a licence or crack it. Fir .tar and .7zip I don't know, but you'll have read their official docs in order how to read this file formats. – Javier Silva Ortíz Oct 19 '19 at 04:28
  • @ JavierSilvaOrtíz Thanks, for your valuable response I will go thought your solution. – Nikunj Satasiya Oct 22 '19 at 11:39

1 Answers1

0
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
    Boolean isFolderExist = false;

    foreach (ZipArchiveEntry entry in archive.Entries) {
        if (entry.FullName.Contains("PDFsDir/")) {                       
            isFolderExist = true;
        }
    }

    if (isFolderExist) {
        Console.WriteLine("the folder which name is pictures exists in zip file");
        if(entry.FileName.Contains( "PDFsDir" )==true) {
            Console.WriteLine("File Exist");
        }
    } else {
        Console.WriteLine("the folder doesn't exist ");
    }
}
Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55