• Post Reply Bookmark Topic Watch Topic
  • New Topic

Streaming to Compressed File  RSS feed

 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to stream files into an existing compressed file in Java? For instance, let's say that I have a daemon that is processing data as it gets it and then writing data to an archive directory on the file system, which results in a lot of files in that directory. Is there a way to stream those files into a compressed file directly instead of creating an archive after the fact?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure there is. Use a ZipOutputStream and wrap it around a FileOutputStream.

ZipOutputStream has a "putNextEntry()" method to start writing an entry in the archive and a "closeEntry()" method to stop writing the entry. In between you can use the ordinary "write()" methods to write the data for the entry.

However you can't update an existing zip archive like this. (In fact you can't update an existing zip archive at all.) You have to create a new archive and write your entries into it.

 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sure there is. Use a ZipOutputStream and wrap it around a FileOutputStream.

ZipOutputStream has a "putNextEntry()" method to start writing an entry in the archive and a "closeEntry()" method to stop writing the entry. In between you can use the ordinary "write()" methods to write the data for the entry.

However you can't update an existing zip archive like this. (In fact you can't update an existing zip archive at all.) You have to create a new archive and write your entries into it.


Excellent! I'll try that as soon as I get a chance to experiment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!