Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to detect if a stream is zipped in Java

Tags:

What is the best way to find out i java.io.InputStream contains zipped data?

like image 644
Fedearne Avatar asked Nov 27 '09 14:11

Fedearne


People also ask

How do you check if a file is a zip file in Java?

Test if a file is a ZIP or JAR archive. boolean isArchive = true; ZipFile zipFile = null; try { zipFile = new ZipFile(file); } catch (ZipException zipCurrupted) { isArchive = false; } catch (IOException anyIOError) { isArchive = false; ... Checks if a file is zipped.

Can Java read a .zip file?

Java API provides extensive support to read Zip files, all classes related to zip file processing are located in the java. util. zip package. One of the most common tasks related to zip archive is to read a Zip file and display what entries it contains, and then extract them in a folder.

How do I find a zip file?

Right-click in Explorer, go to "View" and select "Details." ZIP files will list "Compressed (zipped) Folder" as the file type.


2 Answers

Introduction

Since all the answers are 5 years old I feel a duty to write down, what's going on today. I seriously doubt one should read magic bytes of the stream! That's a low level code, it should be avoided in general.

Simple answer

miku writes:

If the Stream can be read via ZipInputStream, it should be zipped.

Yes, but in case of ZipInputStream "can be read" means that first call to .getNextEntry() returns a non-null value. No exception catching et cetera. So instead of magic bytes parsing you can just do:

boolean isZipped = new ZipInputStream(yourInputStream).getNextEntry() != null; 

And that's it!

General unzipping thoughts

In general, it appeared that it's much more convenient to work with files while [un]zipping, than with streams. There are several useful libraries, plus ZipFile has got more functionality than ZipInputStream. Handling of zip files is discussed here: What is a good Java library to zip/unzip files? So if you can work with files you better do!

Code sample

I needed in my application to work with streams only. So that's the method I wrote for unzipping:

import org.apache.commons.io.IOUtils; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;  public boolean unzip(InputStream inputStream, File outputFolder) throws IOException {      ZipInputStream zis = new ZipInputStream(inputStream);      ZipEntry entry;     boolean isEmpty = true;     while ((entry = zis.getNextEntry()) != null) {         isEmpty = false;         File newFile = new File(outputFolder, entry.getName());         if (newFile.getParentFile().mkdirs() && !entry.isDirectory()) {             FileOutputStream fos = new FileOutputStream(newFile);             IOUtils.copy(zis, fos);             IOUtils.closeQuietly(fos);         }     }      IOUtils.closeQuietly(zis);     return !isEmpty; } 
like image 55
Innokenty Avatar answered Sep 19 '22 10:09

Innokenty


The magic bytes for the ZIP format are 50 4B. You could test the stream (using mark and reset - you may need to buffer) but I wouldn't expect this to be a 100% reliable approach. There would be no way to distinguish it from a US-ASCII encoded text file that began with the letters PK.

The best way would be to provide metadata on the content format prior to opening the stream and then treat it appropriately.

like image 26
McDowell Avatar answered Sep 23 '22 10:09

McDowell