How to tell if a file is compressed or not?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, April 20, 2001 04:57 AM

My application reads uploaded files. A client can upload
text or zipped files. How can I tell if a file is compressed
or not? Curently, I can handle text files only.

Re: how to understand if a file is compressed or not ?

Posted By:   Luigi_Viggiano  
Posted On:   Friday, April 20, 2001 07:46 AM

Consider following 2 solutions:

  • By the file name extension
  • Opening the file and looking for the "magic number"

The first is very easy but not sure at 100%.

The "magic number" is a little sequence of bytes inside binary files that identifies the file's type.
Programs that handle those file types always try to check if the file is of correct type, and contains correct data, checking "magic number" at first.


For example for Java .class files the magic number is located as the first four bytes is 0xCAFEBABE hex (decimal: 3405691582). For .zip the first four bytes are "PK" followed by 0x03 then 0x04 (this is 0x504B0304 hex).

And so on...


You can open different kind of compressed files and identify a constant byte pattern that shall be the "magic number" of the file type.


The best way is to use both solutions together.

About | Sitemap | Contact