Bober02
Bober02

Reputation: 15341

java - creating empty GZIP file

i am trying to solve a simple problem - creating an empty GZip file using Java, so that no excpetion is raised when trying to read from it. if I do:

Files.createFile(outPutFile);
new PrintWriter(new GZIPOutputStream(new FileOutputStream(outPutFile.toFile())), true).close();

It solves the problem - i guess the GZipOutputStream stores some furthr data in the file. is there a more succinct way to achieve the above i.e. not getting Unexpected end of Zlib archive exception?

Upvotes: 1

Views: 2252

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500225

Well, you don't need to call createFile to start with - creating a FileOutputStream will do that. And you don't need the PrintWriter either. So all you need is:

new GZIPOutputStream(new FileOutputStream(outPutFile)).close();

It's odd to capitalize the P in outPutFile by the way - it's not like it's three words...

Upvotes: 3

Related Questions