5

I am working on decompressing Zip archives. As there are two types of archives - Zip and GZip.

I am using following

ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);

But it is giving following error for GZip types of archives

java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)

This code is working fine for Zip compressed type archives, not for GZip

Is there a way to use the above code as I have existing functionality using ZipFile across various functions.If I change ZipFile interface to ZipInputStream or GZipInputStream, then I need to change multiple things.

EDIT : If incoming archives are of type Zip and GZip, do I need different implementation as per @Joachim Sauer's comment

C4CodeE4Exe
  • 3,840
  • 8
  • 39
  • 46
  • 4
    Because [ZIP](https://en.wikipedia.org/wiki/ZIP_(file_format)) and [gzip](https://en.wikipedia.org/wiki/Gzip) are two different formats that don't have much in common except parts of the name and that they are both somewhat about compression. For example: gzip *only* does compression, ZIP does compression *and* multi-file-storage. – Joachim Sauer Nov 30 '12 at 11:40

2 Answers2

4

As @Joachim_Sauer stated already, they are apples and plums.

Take a look at apache's compress, which can handle both (and more) compression/archive types. However, you need to do different implementations AND you have to figure out which implementation to use (whether you handle a zip or gzip file) on your own.

Andy
  • 1,964
  • 1
  • 15
  • 29
  • so I am using apache compress ..is it a good idea to use it right – C4CodeE4Exe Dec 05 '12 at 07:47
  • any idea how can I check archive type whether it is Zip or GZip – C4CodeE4Exe Dec 05 '12 at 09:17
  • Either you guess based on the filename-extension (.zip, .tgz, .gzip, ...), or you try to guess based on the file-content (header information), see also: http://stackoverflow.com/questions/12560824/how-to-know-the-filetype-in-java-android – Andy Dec 05 '12 at 10:31
  • @C4CodeE4Exe: I've just seen, you unaccepted my answer. Why? If it is because the additional question - it is just that, another question... – Andy Dec 05 '12 at 12:59
  • OK I am marking this as an answer as this answer states that I need different implementation. – C4CodeE4Exe Dec 10 '12 at 05:56
3

Zip and gzip are two completely different file formats. Take a look at java.util.zip.GZIPOutputStream and java.util.zip.GZIPInputStream:

InputStream in = new BufferedInputStream(
    new GZIPInputStream( new FileInputStream( file) )
);
Matteo
  • 14,696
  • 9
  • 68
  • 106