DEV Community

loading...

Why I used Python to unzip a file

alvinmilton profile image Alvin Milton ・2 min read

So my day has started like this:

I'm ssh'd into a pretty locked down server in a data center.

This server runs an instance of Wordpress that I have been tasked with migrating over to wordpress.com.

Picture wordpress.com now:
Requires importing with large XML files, no access to the server via terminal, no access to mysql, no logs, only wp-admin via front end.

I needed to install a plugin on the site I'm migrating from but could not through the admin panel because its locked down.

Fine.

Normally, I would just download the plug in and run scp from local to remote to get it to where it has to go.

That looks like this:

cd /to/install/dir
scp <filename> serveraddres:/dir/to/save/file/to

Enter your ssh password and thats it.

Alternate path...

However, sometimes I am already ssh'd into the server and this was one of those occasions. I could have opened a new terminal tab and did the above but again trying to expedite in the least amount of steps.

What I did was use curl to download the file to the server from source skipping the step of putting it on my machine first to then copy it to the server.

That looks like this:
curl http://sitename.com/wp-fileIwant.zip --output wp-fileIwant.zip

Great the file is on the server.

So I tried first running unzip to no avail because it wasn't installed.
Then I tried installing unzip and that was a no go for some reason i didn't feel like debugging. I am wary of the rabbit holes that persistently force us devs to context switch.

Then I came across this gem:

all you need to unzip a file with python

Which funnily enough, I could not open with nano, or emacs because neither was installed. So vim came in handy here.

Wrote the code and saved it in the dir.

Then I ran it. That looked like this:
python unzip.py wp-fileIwant.zip

And lo and behold my file was unzipped.

I could activate it via the front end and do what I needed.

Thus, python.

Discussion

pic
Editor guide
Collapse
ericfrederich profile image
Eric L. Frederich

I've been in a locked down Docker container for a GitLab runner. Didn't feel like atp-get, yum, or apk installing unzip. You don't need that script, you can run Python's unzip as a module...

python -m zipfile whatever.zip

... Or something like that.

Collapse
alvinmilton profile image
Alvin Milton Author

Hey Eric,
I tried this and it didn't work locally or on the server.
Can you clarify?

Collapse
serhiystorchaka profile image
Serhiy Storchaka

Try just
python3 -e wp-fileIwant.zip .

Collapse
alvinmilton profile image
Alvin Milton Author

tried it.
got unknown option -e as a response...

Collapse
serhiystorchaka profile image
Serhiy Storchaka

Sorry, some words were missed. The correct line is:
python3 -m zipfile -e wp-fileIwant.zip .

Collapse
aduen profile image
Aduen Darriba Frederiks

tar can also be used for decompressing zip files in most cases

Collapse
alvinmilton profile image
Alvin Milton Author

Totally forgot about tar!

Collapse
elabftw profile image
eLabFTW

Enter your ssh password

Have you heard about ssh keys? ;p

Collapse
alvinmilton profile image
Alvin Milton Author

If you are referring to adding my key to ssh-agent, I just did that.

Thanks for the tip.

Collapse
mastereuclid profile image
Kyle M.

Cute story. Made me smile. Took my mind off the grind for a second.

Collapse
alvinmilton profile image
Collapse
mujeebishaque profile image
Mujeeb Ishaque

Awesome write-up. Thanks, Alvin.

Collapse
alvinmilton profile image
Alvin Milton Author

thanks. appreciated.

Collapse
monokal profile image
Info Comment marked as low quality/non-constructive by the community. View code of conduct
Daniel Middleton

This REALLY didn't need an article. It's the tech-equivalent of blogging about every meal.

Collapse
danjmillier profile image
Daniel Millier

This comment REALLY didn't need to be made either, but here we are.

Collapse
lack profile image
lack

Sick Daniel-on-Daniel burn