1

Do you think this code close correctly the output stream entry ? Knowing that this is not the same type of output stream.

OutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                       ...
((ZipOutputStream)out).closeEntry();

But considering that the both are output stream, I thought they were closing in the same way. And therefore in my case ((ZipOutputStream)out).closeEntry(); was the same as ((JarOutputStream)out).closeEntry();

Can you confirm that if you think is true or correct me if is wrong ? Thanks.

damson
  • 2,635
  • 3
  • 21
  • 29

4 Answers4

2

Since JarOutputStream extends ZipOutputStream, and since all methods in Java are virtual, doing

((ZipOutputStream) out).closeEntry();

calls the exact same method ass

((JarOutputStream) out).closeEntry();

However, I'd suggest you make the static type a bit more precise:

ZipOutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                   ...
out.closeEntry();
aioobe
  • 413,195
  • 112
  • 811
  • 826
2

If you need to call methods that are specific to ZipOutputStream on your out variable, then its type should not be OutputStream, but ZipOutputStream:

ZipOutputStream out = new JarOutputStream(new FileInputstrem(fileName));
                   ...
out.closeEntry();

This doesn't cause any problem, because since JarOutputStream extends ZipOutputStream, a JarOutputStream is also a ZipOutputStream (and is also an OutputStream, and is also an Object).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks I didn't know JarOutputStream was extends ZipOutputStream and it wasn't call methods specific to ZipOutputStream but juste create generic method to create Zip and Jar archive and use common method for close their output stream entry in this method. But now I know JarOutputStream inherits ZipOutputStream, this is easiest to implement. Thanks. – damson Oct 04 '11 at 09:44
  • If `JarOutputStream` did not extend `ZipOutputStream`, then doing `(ZipOutputStream) out` would result in a `ClassCastException`. – aioobe Oct 04 '11 at 09:48
  • Not necessarily because the close entry execute the same work for all output stream entry. And that's why I asked this question. I wasn't sure. – damson Oct 04 '11 at 10:13
1

And therefore in my case ((ZipOutputStream)out).closeEntry(); was the same as ((JarOutputStream)out).closeEntry();

That is correct. No reason to write the former whatsoever.

user207421
  • 305,947
  • 44
  • 307
  • 483
0
ZipOutputStream.closeEntry();

and

JarOutputStream.closeEntry();

both close the ZIP entry, so you can write another entry into the archive file (if you want to store more than one file into one ZIP/JAR). They do not close the output stream itself. If you want to close your JarOutputStream and the underlying FileOutputStream, use out.close();

Cf.: http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipOutputStream.html#close() http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipOutputStream.html#closeEntry()

chabicht
  • 107
  • 6