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

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
 
P

Peter Duniho

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
 
N

nickdu

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
 
G

Gregory A. Beamer

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! |
*******************************************
 
P

Peter Duniho

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top