Any fast way to determine if a file has been compressed?

  • Thread starter Thread starter nickdu
  • Start date Start date
N

nickdu

I'm using the deflate stream to compress a file. Is there an easy (/fast)
way for me to detect that this file has been compressed using the deflate
stream? The reason I'm looking for this efficient method of checking whether
a file has been compressed is that I have a current application which is
working with uncompressed files. Ideally I would like to modify the
application to be able to work with both compressed and uncompressed files.
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
I'm using the deflate stream to compress a file. Is there an easy
(/fast)
way for me to detect that this file has been compressed using the deflate
stream? The reason I'm looking for this efficient method of checking
whether
a file has been compressed is that I have a current application which is
working with uncompressed files. Ideally I would like to modify the
application to be able to work with both compressed and uncompressed
files.

My recollection is that gzip files (like many file formats) start with a
specific signature byte sequence. So you could always open the file and
look for that.

That said, when I've written code to handle both compressed and
uncompressed data, I simply write the code to always try to uncompress the
file first, and then if that fails, to try to read it as uncompressed
data. I've never had any performance problems with that approach; the
file i/o itself, which is unavoidably, dwarfs any overhead in the code in
terms of performance cost. And doing it that way means you never have to
worry about the exact format of a gzip stream; the compression class
handles all that.

Pete
 
Thanks. Sounds like a reasonable suggestion. However, do I have to switch
to using the gzip stream then instead of the deflate stream?
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
I'm using the deflate stream to compress a file. Is there an easy
(/fast) way for me to detect that this file has been compressed using
the deflate stream? The reason I'm looking for this efficient method
of checking whether a file has been compressed is that I have a
current application which is working with uncompressed files. Ideally
I would like to modify the application to be able to work with both
compressed and uncompressed files.

The headers of files have information about the file. You examine the bytes
to the first null char { (char) 0 }. Every compressed file I have seen has
a header that indicates the file type. The same is true of files like
images file, etc.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
Thanks. Sounds like a reasonable suggestion. However, do I have to
switch
to using the gzip stream then instead of the deflate stream?

You should be able to apply the same approach to either class.
 
Back
Top