0

I want to read files form my *jar archive. I read in the internet how to read from zip:

ZipFile zipFile = new ZipFile( "moers.zip" );
ZipEntry entry  = zipFile.getEntry( "DerAlteSack.png" );
InputStream is = zipFile.getInputStream( entry );

So my question: Can I treat a *jar file as a *zip file?

Mat
  • 202,337
  • 40
  • 393
  • 406
Hydroid
  • 119
  • 3
  • 16
  • 1
    Yes a jar file is a zip file. – Ahmed Masud May 12 '13 at 15:34
  • You should use the class loader to get resources. – SLaks May 12 '13 at 15:35
  • Yup, in fact [`JarFile`](http://docs.oracle.com/javase/6/docs/api/java/util/jar/JarFile.html) extends `ZipFile`. – Boris the Spider May 12 '13 at 15:40
  • @AhmedMasud: Sorry but in my opininon it isn't – Hydroid May 12 '13 at 15:42
  • 1
    http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Intro At the binary level it's exactly a zip file. This isn't philosophy, opinions have little room here. A JarFile just happens to contain a special directory structure with a manifest (META-INF) with a bit of meta information. As far as treatment of a jar in Java is concerned there are special classes for it either using JarFile or using class.getResource* methods. But your question about reading a jar file and whether it can be treated as a zip file; which points to the structure of .jar which is identical to a .zip – Ahmed Masud May 12 '13 at 15:54
  • @AhmedMasud: I mean: it isn't a duplicate... – Hydroid May 12 '13 at 16:03

1 Answers1

2

yes a jar IS a Zip file. See the specification: http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Intro

A JAR file is essentially a zip file that contains an optional META-INF directory.

Pierre
  • 34,472
  • 31
  • 113
  • 192