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

Discussion in 'Microsoft Dot NET Framework' started by nickdu, Sep 22, 2009.

  1. nickdu

    nickdu Guest

    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

    am
    remove "nospam" change community. to msn.com
     
    nickdu, Sep 22, 2009
    #1
    1. Advertisements

  2. nickdu

    Peter Duniho Guest

    On Tue, 22 Sep 2009 07:33:01 -0700, nickdu <>
    wrote:

    > 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
     
    Peter Duniho, Sep 22, 2009
    #2
    1. Advertisements

  3. nickdu

    nickdu Guest

    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

    am
    remove "nospam" change community. to msn.com


    "Peter Duniho" wrote:

    > On Tue, 22 Sep 2009 07:33:01 -0700, nickdu <>
    > wrote:
    >
    > > 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
    >
     
    nickdu, Sep 22, 2009
    #3
  4. =?Utf-8?B?bmlja2R1?= <> wrote in
    news::

    > 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! |
    *******************************************
     
    Gregory A. Beamer, Sep 22, 2009
    #4
  5. nickdu

    Peter Duniho Guest

    On Tue, 22 Sep 2009 10:33:01 -0700, nickdu <>
    wrote:

    > 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.
     
    Peter Duniho, Sep 23, 2009
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Hildner

    Proper way to determine socket state?

    Mike Hildner, Oct 10, 2003, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    343
    Mike Hildner
    Oct 10, 2003
  2. Guest
    Replies:
    3
    Views:
    1,475
  3. Lin JinCong

    how to get a file has been open which application or process.

    Lin JinCong, Jul 18, 2004, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    165
    Lin JinCong
    Jul 18, 2004
  4. xeroxero

    XML Filter Fast Way?

    xeroxero, Nov 6, 2006, in forum: Microsoft Dot NET Framework
    Replies:
    2
    Views:
    144
    xeroxero
    Nov 6, 2006
  5. Kenneth Baltrinic

    Howto: Determine within a custom attribute, the type it has been declared on?

    Kenneth Baltrinic, Mar 16, 2007, in forum: Microsoft Dot NET Framework
    Replies:
    2
    Views:
    243
    Ben Voigt
    Mar 16, 2007
Loading...

Share This Page