Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

COMPRESSED FILE

Thread view: 
max - 28 Apr 2006 16:12 GMT
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
Thomas Weidenfeller - 28 Apr 2006 16:43 GMT
> 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
Signature

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/

Rogan Dawes - 28 Apr 2006 16:47 GMT
> 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
Roedy Green - 28 Apr 2006 17:26 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 28 Apr 2006 20:30 GMT
On Fri, 28 Apr 2006 16:26:37 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Eric Sosman - 28 Apr 2006 20:44 GMT
Roedy Green wrote On 04/28/06 12:26,:

>>I receive a file in an InputStream , and I'd like to know which kind of
>>file is it, compressed, or not.
[quoted text clipped - 5 lines]
> 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.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.