COMPRESSED FILE

  • Thread starter Thomas Weidenfeller
  • Start date
T

Thomas Weidenfeller

max 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?

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
 
R

Rogan Dawes

max 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
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
 
M

max

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
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
E

Eric Sosman

Roedy Green wrote On 04/28/06 12:26,:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top