Name: boT120536 Date: 05/13/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
java.util.zip.ZipInputStream and ZipOutputStream throws an IOException:"stream
closed" if you call the close() method on an already closed stream.
java.util.zip.GZIPInputStream and GZIPOutputStream doesn't behave this way, nor
does any other stream classes in java.io.* to my knowledge.
import java.io.*;
import java.util.zip.*;
public class TestClosingZipStreams
{
public static void main(String[] args)
{
try
{
FileOutputStream gzFile = new FileOutputStream("tmp.gz");
FileOutputStream zipFile = new FileOutputStream("tmp.zip");
GZIPOutputStream gzStream = new GZIPOutputStream(gzFile);
ZipOutputStream zipStream = new ZipOutputStream(zipFile);
gzStream.write(0);
System.out.print("Trying to close the open GZIPOutputStream...");
gzStream.close();
System.out.println("OK");
System.out.print("Trying to close the already closed
GZIPOutputStream...");
gzStream.close();
System.out.println("OK");
zipStream.putNextEntry(new ZipEntry("X"));
zipStream.write(0);
zipStream.closeEntry();
System.out.print("Trying to close the open ZipOutputStream...");
zipStream.close();
System.out.println("OK");
System.out.print("Trying to close the already closed
ZipOutputStream...");
zipStream.close();
System.out.println("OK");
}
catch(IOException e)
{
//System.out.println(e);
e.printStackTrace();
}
}
}
=======================================
Output:
=======================================
Trying to close the open GZIPOutputStream...OK
Trying to close the already closed GZIPOutputStream...OK
Trying to close the open ZipOutputStream...OK
Trying to close the already closed ZipOutputStream...java.io.IOException: Stream
closed
at java.util.zip.ZipOutputStream.ensureOpen(ZipOutputStream.java:46)
at java.util.zip.ZipOutputStream.finish(ZipOutputStream.java:286)
at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:312)
at TestClosingZipStreams.main(TestClosingZipStreams.java:35)
(Review ID: 123138)
======================================================================