7

I've received files in .tar.gz type but the application I'm using only accepts .zip files. I need an easy way of converting .tar.gz files to .zip without loosing any of the data.

Does renaming to .zip will work? I've tried it but when I click on the file it's showing it as a invalid file.

Any thoughts or inputs for this?

naresh ss
  • 93
  • 1
  • 1
  • 4

1 Answers1

13

You can probably convert your archive foo.tar.gz into a zipped archive foo.zip with a command similar to this one:

tar xzOf foo.tar.gz | zip foo.zip $(tar tf foo.tar.gz)

In details: tar xzOf foo.tar.gz extracts (x) the gzipped (z) archive to standard output (O). Here f is used to provide the input file name.

Standard input is piped into zip, whose first argument is the name of the zipped file to produce. The following argument is the list of the files to compress, that we can obtain directly from tar with the t command, run on the initial .tar.gz file.

Edit: I thought the above one-liner could be used to easily convert a tar archive into a zip file. Reading again the command one year later or so, it turns out it looks badly broken: it just takes the names of the files from the tar archive, and zip from the original files (non-compressed), if they are still on the disk. I don't see how to produce a simple one-liner, since you apparently cannot provide both the name and the contents of the file to zip on the command line (see also this question and its answers).

So we probably need two steps to do this: first uncompress from the tar archive, then compress again. Something like:

tar xzf foo.tar.gz && zip foo.zip $(tar tf foo.tar.gz)

We can also add a third part to remove the temporarily decompressed files: && rm -r -- $(tar tf foo.tar.gz), but be cautious and make sure this does not erase stuff you want to keep.

For more details, have a look at the manual pages of zip and tar utilities.

Qeole
  • 8,284
  • 1
  • 24
  • 52
  • Thanks!! It worked for me. – naresh ss Aug 22 '17 at 04:32
  • This is neat, but looks like it doesn't work for files in the top-level directory, those are missing in the zip file. – Berdir Jun 26 '18 at 08:37
  • Thanks for the feedback @Berdir. Actually I'm reading my answer again, and I think the whole command is broken. It looks like `zip` is taking the file names to compress from `$(tar tf foo.tar.gz)`, but does not take the contents of those files from `tar` output (it loads them from the uncompressed files again). Does this even work if the original files have been moved or removed? – Qeole Jun 26 '18 at 10:20
  • 1
    Good point, that might actually be the reason for the behavior I saw, the folder did exist in the local folder while the other two files did not. I ended up doing it manually in two steps, first decompress in a temporary folder and then zipping it – Berdir Jun 26 '18 at 19:05
  • 2
    If you're dealing with an archive containing lots of files (80k+), you may hit `argument list too long` error. The quick solution is to make `zip` read list of files from stdin like this: `tar tf foo.tar.gz | zip -@rj foo.zip` – ghost Aug 20 '18 at 05:44
  • How to make it work if my source archive is in a different directory like that: zip files/test.zip $(tar tf files/test.gz) – Splinteer Sep 25 '18 at 10:19