aspose file tools
The moose likes Java in General and the fly likes Is there a way to check if a file is a zip file in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is there a way to check if a file is a zip file in java?" Watch "Is there a way to check if a file is a zip file in java?" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way to check if a file is a zip file in java?
 
Similar Threads
ZipOutputStream and nested zip files
Is it possible to check if a Zip file can be opened, and list all file names inside the ZIP?
Winzip file uploading problem
How to zip a file?
cant add class file to jar