Convert Tar.Gz to Zip

Is there any command on Linux to convert .tar.gz files to .zip?

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.

How to convert tar.gz file to zip using Python only?

You would have to use the tarfile module, with mode 'r|gz' for reading.
Then use zipfile for writing.

import tarfile, zipfile
tarf = tarfile.open( name='mytar.tar.gz', mode='r|gz' )
zipf = zipfile.ZipFile( file='myzip.zip', mode='a', compression=zipfile.ZIP_DEFLATED )
for m in tarf:
f = tarf.extractfile( m )
fl = f.read()
fn = m.name
zipf.writestr( fn, fl )
tarf.close()
zipf.close()

You can use is_tarfile() to check for a valid tar file.

Perhaps you could also use shutil, but I think it cannot work on memory.

PS: From the brief testing that I performed, you may have issues with members m which are directories.
If so, you may have to use is_dir(), or even first get the info on each tar file member with tarf.getmembers(), and the open the tar.gz file for transferring to zip, since you cannot do it after tarf.getmembers() (you cannot seek backwards).

Convert gz to zip format

gzip is only compressing data, while ZIP is both a compressor and archiver. A gzip stream can only compress one file while ZIP can compress several. So simply renaming the file cannot work.

You need to decompress the gzip stream to a file, then recompress as ZIP.

NameError while converting tar.gz to zip

ZIP_DEFLATED is a name defined by the zipfile module; reference it from there:

zipf = zipfile.ZipFile(
'myzip.zip', mode='a',
compression=zipfile.ZIP_DEFLATED)

Note that you don't use the ZipFile.open() method here; you are not opening members in the archive, you are writing to the object.

Also, the correct ZipFile class signature names the 3rd argument compression. compress_type is only used as an attribute on ZipInfo objects and for the ZipFile.writestr() method. The first argument is not named name either; it's file, but you normally would just pass in the value as a positional argument.

Next, you can't seek in a gzip-compressed tarfile, so you'll have issues accessing members in order if you use tarf.getmembers(). This method has to do a full scan to find all members to build a list, and then you can't go back to read the file data anymore.

Instead, iterate directly over the object, and you'll get member objects in order at a point you can still read the file data too:

for m in tarf:
f = tarf.extractfile( m )
fl = f.read()
fn = m.name
zipf.writestr( fn, fl )

How to create tar.gz file in windows by only one step?

After digging into the 7-zip source code, i extended the 7-zip context menu by adding "add to tar.gz" item. and it works !

Convert .tar.gz file to .zip using TrueZip?

The issue is that the target archive file already exists as an empty file once you've called File.createTempFile(*), which will be treated as a false positive archive file by the TrueZIP Kernel. According to this logic, your subsequent call to TFile.cp_rp(*) tries to recursively copy a virtual directory to a plain file, which cannot work.

To make your code work, simply call File.delete() on the object returned by File.createTempFile(*). The remainder of your code should then work.



Related Topics



Leave a reply



Submit