COMPRESSED FILE

Discussion in 'Java' started by Thomas Weidenfeller, Apr 28, 2006.

  1. max wrote:
    > I receive a file in an InputStream , and I'd like to know which kind of
    > file is it, compressed, or not.
    > How can I do it?


    Only with difficulties. Java SE does not provide any file type detection
    feature. You would have to re-create what some operating systems like
    Unix do: They look for "magic numbers" in the file, and according to
    what they find they guess a file type.

    If you know the exact encoding of your compressed data, and if the
    encoding starts with some typical magic number you can check for it by
    e.g. using a PushbackInputStream to look ahead in the stream and check
    for some numbers.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Apr 28, 2006
    #1
    1. Advertising

  2. Thomas Weidenfeller

    Rogan Dawes Guest

    max wrote:
    > I receive a file in an InputStream , and I'd like to know which kind of
    > file is it, compressed, or not.
    > How can I do it? Which method can I use on this InputStream
    > Thanks


    There is no existing method that you can use. You can try various
    heuristics that will let you guess what type of file it is.

    e.g. see Unix "file" command.

    Another approach would be to buffer the InputStream. i.e. read it into
    an array or ByteArrayOutputStream, and then create a reusable
    InputStream from that using ByteArrayInputStream.

    Then , in the order of most likely to least likely, or most
    deterministic to least deterministic, simply try your various options.

    e.g.

    byte[] buff = // your entire inputstream read into an array
    InputStream is = new ByteArrayInputStream(buff);
    // is it a GZipped stream?
    try {
    GzipInputStream gzis = new GzipInputStream(is);
    byte[] buff2 = new byte[2048];
    while (gzis.read(buff)>-1);
    gzis.close();
    // looks like it is a Gzipped stream
    } catch (IOException ioe) {
    // guess it isn't
    }

    Hope this helps

    Rogan
    Rogan Dawes, Apr 28, 2006
    #2
    1. Advertising

  3. Thomas Weidenfeller

    max Guest

    I receive a file in an InputStream , and I'd like to know which kind of
    file is it, compressed, or not.
    How can I do it? Which method can I use on this InputStream
    Thanks
    max, Apr 28, 2006
    #3
  4. Thomas Weidenfeller

    Roedy Green Guest

    On Fri, 28 Apr 2006 18:07:22 +0200, max <> wrote,
    quoted or indirectly quoted someone who said :

    >I receive a file in an InputStream , and I'd like to know which kind of
    >file is it, compressed, or not.
    >How can I do it? Which method can I use on this InputStream


    you could try reading it with GZIP and see if it fails. See
    http://mindprod.com/applet/fileio.html for sample code.

    Unfortunately GZIP does not hava file signature, or at least not an
    obvious one. You might read up to see if there is one.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 28, 2006
    #4
  5. Thomas Weidenfeller

    Roedy Green Guest

    On Fri, 28 Apr 2006 16:26:37 GMT, Roedy Green
    <> wrote, quoted or
    indirectly quoted someone who said :

    >Unfortunately GZIP does not hava file signature, or at least not an
    >obvious one. You might read up to see if there is one.


    or just study samples of your files with a hex viewer to see if there
    is any pattern in the header you can detect.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 28, 2006
    #5
  6. Thomas Weidenfeller

    Eric Sosman Guest

    Roedy Green wrote On 04/28/06 12:26,:
    > On Fri, 28 Apr 2006 18:07:22 +0200, max <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >
    >>I receive a file in an InputStream , and I'd like to know which kind of
    >>file is it, compressed, or not.
    >>How can I do it? Which method can I use on this InputStream

    >
    >
    > you could try reading it with GZIP and see if it fails. See
    > http://mindprod.com/applet/fileio.html for sample code.
    >
    > Unfortunately GZIP does not hava file signature, or at least not an
    > obvious one. You might read up to see if there is one.


    According to <http://www.wotsit.org/>, a GZIP data
    stream begins with the two bytes 0x1F, 0x89. A thorough
    checker might choose to examine additional bytes, too,
    although the checking would be more involved than just a
    simple comparison.

    --
    Eric Sosman, Apr 28, 2006
    #6
    1. Advertising

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. ad
    Replies:
    6
    Views:
    3,211
  2. Michael Wehner
    Replies:
    0
    Views:
    1,824
    Michael Wehner
    May 21, 2006
  3. Dennis Hotson

    Add a file to a compressed tarfile

    Dennis Hotson, Nov 5, 2004, in forum: Python
    Replies:
    8
    Views:
    367
    Francesc Alted
    Nov 9, 2004
  4. Ritesh Raj Sarraf
    Replies:
    1
    Views:
    372
    Marc 'BlackJack' Rintsch
    Jul 5, 2006
  5. flebber
    Replies:
    9
    Views:
    517
    John Machin
    Jun 10, 2007
Loading...

Share This Page