Author Topic: tarpy  (Read 762 times)

0 Members and 2 Guests are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
tarpy
« on: May 29, 2013, 05:13:21 am »
My own util for tar files and such. Been using it for a while with downloads and such. One file at a time though, haven't expanded since I haven't had a reason too and all.

If you can make it better and expand then do so. I find this much easier than using untar because of the ridiculous flags for file types and all. Hashbangs may very, on Arch ATM.

-----------EDIT------------------------
The code posted before wasn't the right one. This is the code currently in use under /usr/bin.
What you seen before was just messing around and thoughts on error checking/etc...

Python 2.x
Code: (python) [Select]
#!/usr/bin/python2

import tarfile
import os, sys

def unCompress(f):
    os.mkdir(f.split('.')[0])
    t = tarfile.open(f, 'r')
    t.extractall(f.split('.')[0])


print sys.argv
if len(sys.argv) >= 2:
    unCompress(sys.argv[1])
else:
    print "Specify a file"
« Last Edit: May 29, 2013, 08:01:55 pm by techb »
>>>import this
-----------------------------

Offline str0be

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • <!-- hi
    • View Profile
Re: tarpy
« Reply #1 on: May 29, 2013, 10:55:17 am »
I think your code got mangled a bit; I'm not sure what check() or sys.ext() are supposed to do. But anyway, I've  thought about writing a similar tool :) so I've made some improvements. Added support for zip files and processing multiple files at once.

Code: (python) [Select]
#!/usr/bin/env python

# ^ leverage env for a universal hashbang
# USAGE: ./unarchive file1 file2 file3 ...

import tarfile
import zipfile
import os

def is_zipfile(fname):
    return fname.split('.')[-1].lower() == "zip"

def extract(fname, path="."):
    if is_zipfile(fname):
        archive = zipfile.ZipFile(fname, 'r')
    else:
        archive = tarfile.open(fname, "r:*")
    archive.extractall(path)

if __name__ == "__main__":
    import sys

    files = sys.argv[1:]
    for fname in files:
        path = fname.split(".")[0]
        print("Extracting %s" % (fname)) # Treat print as a function, env might
        extract(fname, path)             # find python3 instead of python2.


Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: tarpy
« Reply #2 on: June 04, 2013, 04:54:23 am »
I think your code got mangled a bit; I'm not sure what check() or sys.ext() are supposed to do. But anyway, I've  thought about writing a similar tool :) so I've made some improvements. Added support for zip files and processing multiple files at once.

Code: (python) [Select]
#!/usr/bin/env python

# ^ leverage env for a universal hashbang
# USAGE: ./unarchive file1 file2 file3 ...

import tarfile
import zipfile
import os

def is_zipfile(fname):
    return fname.split('.')[-1].lower() == "zip"

def extract(fname, path="."):
    if is_zipfile(fname):
        archive = zipfile.ZipFile(fname, 'r')
    else:
        archive = tarfile.open(fname, "r:*")
    archive.extractall(path)

if __name__ == "__main__":
    import sys

    files = sys.argv[1:]
    for fname in files:
        path = fname.split(".")[0]
        print("Extracting %s" % (fname)) # Treat print as a function, env might
        extract(fname, path)             # find python3 instead of python2.

Updated the OP a bit I guess, it was quick, dirty, and meant for usefulness for me. I like your version though and might compound on it and what not later on or something. I can see needed support for other compression types and all.

+1 though.
>>>import this
-----------------------------