1

Suppose I have a file like this:

MyFile.zip

In Python, what is a neat way to convert the .zip file into a gzip file? e.g. final output MyFile.gz, a valid gzipped file.

Anton
  • 1,458
  • 1
  • 14
  • 28

1 Answers1

2

There's a gzip module for this sort of conversion, I am going off the python docs for this, link here.

Basically what you're looking for is an input i.e. your current file/path/to/zip/file and then taking that as input to the gzip conversion by using shutil.copyfileobj(src, dest). This is kinda like layman terms for how to do it but all the details are in the link.

de_classified
  • 1,927
  • 1
  • 15
  • 19
  • I've just read https://stackoverflow.com/a/20765054/4130137 and realized it is not exactly so simple, since the .zip file is an archive and a .gz file is a compressed single file. I was hoping there would be a simple two-liner like doing a with gzip.open() and the zipfile equivalent and simply copying them across, but it'd probably be necessary to unzip the whole archive, and find the files I want to gzip, or tar the archive and then gzip. – Anton Feb 21 '20 at 05:04
  • or if it's that case then yes, a slightly longer approach may need to be taken – de_classified Feb 21 '20 at 05:18