Author Topic: anarchive  (Read 1091 times)

0 Members and 1 Guest are viewing this topic.

Offline str0be

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • <!-- hi
    • View Profile
anarchive
« on: June 03, 2013, 02:02:22 am »
anarchive - Inspired by this post by techb: http://evilzone.org/scripting-languages/tarpy/msg58234/#msg58234. This is just a slight update from that thread adding fragile support for .rar files. But eventually I'd like to morph it into a tool that can do something like this:

Code: [Select]
$ anarchive -pattern "python" -o Python.Books.zip

Which would rip all files with "python" in the filename from every archive in the current directory, regardless of archive type, to a zip file (or whatever). I'm wondering, has this problem been solved before? I want something that will work the same across windows/linux/unix.

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

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

import tarfile
import zipfile
import subprocess
import os

#XXX desperate for error checking

class Rarfile:
    def __init__(self, fname):
        self.fname = fname

    def extractall(self, path):
        wd = os.getcwd()
        os.mkdir(path)
        os.rename(fname, os.path.join(path, fname))
        os.chdir(path)
        subprocess.call(["unrar", "x", fname])
        os.rename(fname, os.path.join(wd, fname))
        os.chdir(wd)


def guesstype(fname):
    return fname.split('.')[-1].lower()

def extract(fname, path="."):
    archivetype = guesstype(fname)

    if archivetype == "zip":
        archive = zipfile.ZipFile(fname, 'r')
    elif archivetype == "rar":
        archive = Rarfile(fname)
    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.
« Last Edit: June 03, 2013, 02:04:54 am by str0be »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: anarchive
« Reply #1 on: June 03, 2013, 07:09:42 am »
From the project title I thought you were making an archive with anarchy related things (like bomb recipes, stories and such xD).

Offline Ragehottie

  • Knight
  • **
  • Posts: 313
  • Cookies: -9
  • Hack to learn, not learn to hack.
    • View Profile
Re: anarchive
« Reply #2 on: June 03, 2013, 02:59:52 pm »
using .find() will probably do the job for you:


http://docs.python.org/release/3.1.3/library/stdtypes.html#str.find



« Last Edit: June 03, 2013, 03:44:10 pm by Ragehottie »
Blog: rexmckinnon.tumblr.com

Offline str0be

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • <!-- hi
    • View Profile
Re: anarchive
« Reply #3 on: June 05, 2013, 09:57:33 pm »
From the project title I thought you were making an archive with anarchy related things (like bomb recipes, stories and such xD).


Heh, nothing that exciting...


Ragehottie, I'm actually more interested in hearing if there is something out there that does this already. Btw, is your avatar a stylized swastika?

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: anarchive
« Reply #4 on: June 05, 2013, 10:03:15 pm »
I believe so. With a few simple scripting you can make a script around 7z:
Code: [Select]
7z x archive "path/to/file/inside/archive"
« Last Edit: June 05, 2013, 10:17:32 pm by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline str0be

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • <!-- hi
    • View Profile
Re: anarchive
« Reply #5 on: June 05, 2013, 10:29:26 pm »
Ahh, for some reason I thought that was a windows-only graphical program. A wrapper around 7z will be much better & easier, thanks.
« Last Edit: June 05, 2013, 10:31:08 pm by str0be »