A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Java in General
Is there a way to check if a file is a zip file in java?
Joined: Nov 06, 2006
Nov 23, 2006 21:43:00
Is there a way to check if a file is a zip file in
Checking file extension is one way, but is there a better way?
Joined: Nov 16, 2005
Nov 23, 2006 22:25:00
Use file filters. There is File utility in Apache common.io package.
IOFileFilter suffixFilter = FileFilterUtils.suffixFileFilter("zip");
Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />email@example.com
Joined: Jan 30, 2000
Nov 23, 2006 23:38:00
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:
RandomAccessFile raf = new RandomAccessFile(f, "r"); long n = raf.readInt(); raf.close(); if (n == 0x504B0304) System.out.println("Should be a zip file"); else System.out.println("Not a zip file");
After that, to see if the whole thing is really a
zip file, the easiest way is probably to open it up using the
class, read all the entries, and see if it throws an exception.
"I'm not back." - Bill Harding,
I agree. Here's the link:
subject: Is there a way to check if a file is a zip file in java?
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
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2013