| Author |
Is there a way to check if a file is a zip file in java?
|
ken carlino
Greenhorn
Joined: Nov 06, 2006
Posts: 10
|
|
Is there a way to check if a file is a zip file in java? Checking file extension is one way, but is there a better way? Thank you.
|
 |
Prabhu Venkatachalam
Ranch Hand
Joined: Nov 16, 2005
Posts: 502
|
|
Use file filters. There is File utility in Apache common.io package. URL For example, IOFileFilter suffixFilter = FileFilterUtils.suffixFileFilter("zip");
|
Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Aside from looking at the file extension, a zip file (or jar file) should begin with a standard "magic number" in the first four bytes. You can check for it thus: After that, to see if the whole thing is really a valid zip file, the easiest way is probably to open it up using the ZipFile class, read all the entries, and see if it throws an exception.
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: Is there a way to check if a file is a zip file in java?
|
|
|