Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > COMPRESSED FILE

Reply
Thread Tools

COMPRESSED FILE

 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      04-28-2006
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/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
 
Rogan Dawes
Guest
Posts: n/a
 
      04-28-2006
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
 
Reply With Quote
 
 
 
 
max
Guest
Posts: n/a
 
      04-28-2006
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-28-2006
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-28-2006
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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-28-2006


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.

--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read xml file from compressed file using gzip flebber Python 9 06-10-2007 10:40 AM
Re: Script to automate extraction of file from compressed archives Michael Wehner Perl 0 05-21-2006 01:13 AM
Could we send a compressed file (.zip) to server by web service? ad ASP .Net 6 09-11-2005 05:32 AM
Add a file to a compressed tarfile Dennis Hotson Python 8 11-09-2004 01:55 PM
D100 RAW file Compressed vs Uncompressed Paddler Digital Photography 3 10-21-2003 12:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57