1

At work I'm implementing a new webservice that works with files. The specifications say that we should not accept .zip files if they are compressed.

Is there such a thing as a not compressed .zip file? If yes, what do you think would be the best way to detect one using Java (1.8)?

Pistacchio
  • 445
  • 4
  • 15
  • ZipFile package. And yes, there is a format in there for uncompressed. ZipEntry.STORED –  Apr 17 '19 at 14:47
  • From Wikipedia: "ZIP allows contained files to be compressed using many different methods, as well as simply storing a file without compressing it" – SantaXL Apr 17 '19 at 14:49

2 Answers2

6

Yes, a zip can be uncompressed. In fact, early .jar files were just uncompressed .zip files.

The ZipEntry class has getMethod to get the compression method of an entry. One of the options is STORED (e.g., not compressed).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you, I had no idea about this. I'm just curious about one thing tough, would every entry inside a zip file be either stored or not? I do think so, but I'm not sure. – Pistacchio Apr 18 '19 at 13:20
  • @Pistacchio - Every entry in a zip has its own flag for whether and how it's compressed, yes. So to disallow a "compressed zip," you'd need to check every entry in the zip to see if any of them is compressed (well, you only need to check every one until you find one that's compressed). The vast majority of the time you'll probably find that they're all compressed with the same algorithm, or all uncompressed, but the format allows differences in compression per-entry. – T.J. Crowder Apr 18 '19 at 13:30
1

zip is just a container-format, which can hold many possible types of compressed and uncompressed entries.

Often files are deflated, which means, they are compressed. Others are simply stored, which means they are uncompressed.

SirFartALot
  • 1,215
  • 5
  • 25