• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is Zip or not?  RSS feed

 
Iban Cardona
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I wonder how I know if a particular file is a zip or not.
Anyone have any ideas?

Thank you very much.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest is probably to try to open it with a ZipInputStream and check whether any exceptions are thrown - there's probably one that means as much as "not a valid zip file".
 
Iban Cardona
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The easiest is probably to try to open it with a ZipInputStream and check whether any exceptions are thrown - there's probably one that means as much as "not a valid zip file".


Hi,

Thanks for your prompt response.

Now I try to do this:



The problem is that I can not distinguish between a non-zip file and a empty zip file...
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An empty zip file is still a zip file, so it shouldn't produce any errors. If you mean a 0 byte file with the .zip extension, then that is simply put not a zip file.

If you want to disallow empty zip files (zip files without any entries), simply count the number of entries, and after the loop throw an exception if the count is 0.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!