title: Zlib ruby - how to check if data is deflated/compressed before processing it?
body: <p>I'm in a situation where some data in a database is not compressed, but I want to enable compression for new data coming in, without having to update all records currently in the database to make them compressed as well. </p>
  
  <p>So I need to be able to say, if deflated, inflate it and process it, else just process it. But I can't see how to gracefully check whether the data is already compressed before trying to process it, unless I do a 'begin ... rescue' block: </p>
  
  <pre><code>begin 
    process(Zlib::Inflate.inflate(data))
  rescue Zlib::DataError
    process(data) 
  end 
  </code></pre>
  
  <p>Is there a better way? I've seen references to magic numbers and checking the first couple of bytes of the file, but no good examples of how to achieve these things in ruby. Any help appreciated. Thanks.  </p>