3

How to compress in Java several files in one .gz file ?

skaffman
  • 390,936
  • 96
  • 800
  • 764
Damir
  • 51,611
  • 92
  • 240
  • 358
  • 5
    GZIP is a stream compression algorithm, it doesn't do file-handling. Are you referring to `.tar.gz`? – skaffman Jan 24 '11 at 13:46
  • Usually it's better to use ZIP for this kind of thing, unless there is a specific requirement to use .tar.gz. – Sergei Tachenov Jan 24 '11 at 14:29
  • @Sergey: It is usually easier to use ZIP (at least from Java code), but I doubt that it's usually better. If the files have similar content, tar.gz provide a much better compression rate than zip. – jarnbjo Jan 24 '11 at 14:54
  • @jarnbjo, I meant that it is better because it's easier and more "standard". For best compression, tar.gz is much better of course, especially when there is a lot of small files, but usually it isn't that important. Another funny approach would be to create a ZIP with no compression, then compress it using gzip. I haven't seen it in real life, but the good point is that it can be accomplished using the standard Java library. – Sergei Tachenov Jan 24 '11 at 15:02

1 Answers1

4

You must somehow join them before gzipping. Gzip can compress only one file, so most frequent solution is to tar files first and them gzip newly created tar.

Michał Niklas
  • 50,951
  • 17
  • 64
  • 108
  • See this question for information on programmatically tar'ing: http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java – Erick Robertson Jan 24 '11 at 14:11