Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why ZipFile(file, ZipFile.OPEN_READ) does not work for GZip

Tags:

java

zip

gzip

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

like image 402
C4CodeE4Exe Avatar asked Nov 30 '12 11:11

C4CodeE4Exe


People also ask

What is ZIP file ZIP file?

Python's zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.


2 Answers

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.

like image 165
Andy Avatar answered Sep 21 '22 19:09

Andy


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) )
);
like image 39
Matteo Avatar answered Sep 19 '22 19:09

Matteo