4

Does anyone know how multiple files can be GZIP compressed?
I have lots of files in a folder and I need to compress them individually.

Actually, I've found a way to do it. GZIP command line binary does the job.

I've used gzip -r dir.

Thanks for your efforts.

3
  • Which operating system ?
    – Karolos
    Feb 10, 2012 at 20:19
  • Win7 , sorry :)
    – avetarman
    Feb 10, 2012 at 20:20
  • 1
    @avetarman, Please add what you did as an answer, and accept it, rather than just putting the answer in the question
    – soandos
    Feb 10, 2012 at 21:14

6 Answers 6

2

Just use the gzip alone.

by default, gzip will take any file passed to it and compress it and add the .gz extension. i.e.

gzip dir/* -r

would gzip every file in dir/* (and sub-directories).

4

Try the following in powershell (after going to the correct directory):

$files = get-childitem
foreach ($file in $files) {gzip $file}

That will go through all the files in the directory, and compress all of them.

Edit: If you want to do all of the files in a directory tree (i.e. in a folder and all its subfolders) just change $files = get-childitem to $files = get-childitem -recurse

3
  • 2
    This could be extended for just about any CLI-capable compression utility, including 7zip. Feb 10, 2012 at 20:59
  • Yup. Nice and adaptable that way. (on an unrelated note, when you edit links, check them first as I have seen some missed parenthesis on some of your edits. Not a big deal, but I thought you should know)
    – soandos
    Feb 10, 2012 at 21:04
  • Thx for the heads up. Feb 10, 2012 at 21:11
3

I would use a for loop. Are you concerned that the executable will be loaded many times? That should not be much, compared to the time taken to compress.

1
  • 1
    Perhaps provide the code that will do this?
    – soandos
    Feb 10, 2012 at 20:35
2

GZIP can be used to compress individual files sure... but most of the time you'll see minimal amounts of disk-space savings. Files that take up 1 sector will consume 1-full-sector of disk space, even if the file is less than the sector-size. (i.e. sector-size = 16kb, the file is 8kb... 'compressed' is 5kb... still takes up 16kb of "disk-space") The power of gzip is much more evident when you combine all the files into a single file (using tar typically) and then compress the singular archive. (combined, the "archive" can take all the files and combine them into 1 singular file on the disk... which will have less "dead-space" between sectors)

For example:

tar -czf compressed_archive.tgz folder/*

tar -c creates an archive, z uses gzip compression, and f writes the results to a file called compressed_archive.tgz. You can also switch to using bz2 compression (which does better in many cases) by simply switching the z switch to a j.

If you want to then extract files from the archive, you can simply do the reverse

tar -zxf compressed_archive.tgz

which would x extract the files that were z gzip'd, f from the archive file called compressed_archive.tgz.

1
  • Thanks. I don't need an archive. I was looking for compressing the files recursively. It's for a sitemap. Thanks again.
    – avetarman
    Feb 10, 2012 at 20:51
1

Use 7-Zip for better compression

2
  • Thanks, but it only makes a single compressed archive. It doesn't treat the files individually.
    – avetarman
    Feb 10, 2012 at 20:29
  • Siva that is not an answer to the question. It is a statement of a true fact. A unrelate true fact Feb 11, 2012 at 0:58
1

I found I had to adapt soandos powershell method to get this to work as expected (I would have added comment, but not allowed to yet).

$files = get-childitem -recurse | % { $_.FullName }
foreach ($file in $files) {gzip $file}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .