0

Is it possible to identify all types of compressed (.zip, .7z, .rar, etc) files in c#.

I know that can be done by identifying file extension but is that the reliable way or is there any better way to do so?

Ronak
  • 187
  • 2
  • 17
  • https://stackoverflow.com/questions/11996299/c-net-identify-zip-file ,have you try this – ABCD Feb 27 '20 at 04:18
  • 1
    In general... probably, categorically... probably not. How are you going to determine Disneylands mickey-mouse humpty dumpty compression algorithm invented and used by joe blogs of the blogs foundation 25 years ago? – TheGeneral Feb 27 '20 at 04:18
  • Inspection of the “magic bytes” (hint: search term) is usually a more honest indicator, although both can be lies. There are databases that contain such “magic byte” rules, and likely some with C# implementations. Proper extraction is the ultimate guarantee of content. – user2864740 Feb 27 '20 at 04:18
  • How does the “FileInfo” class help? – user2864740 Feb 27 '20 at 04:20
  • 2
    https://en.wikipedia.org/wiki/List_of_file_signatures – TheGeneral Feb 27 '20 at 04:22
  • you can identify common/publicly available formats, but obviously it's impossible to identify **all** types of compressed formats. Do you know how many formats out there? – phuclv Aug 04 '22 at 16:58

1 Answers1

0

You can try the below code (I did not try)

//YOUR FILE
FileAttributes attributes = File.GetAttributes("c:/Temp/SomeFile");

//CHECK FOR COMPRESSED 
if ((attributes & FileAttributes.Compressed) == FileAttributes.Compressed)
{
    Console.WriteLine("The file is compressed.");
}

Reading:

https://learn.microsoft.com/en-us/dotnet/api/system.io.fileattributes?view=netframework-4.8

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34