Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import os
- import time
- import tarfile
- import zipfile
- def __cf(word, coding="utf-8", to_str=False):
- """
- char format
- :param to_str: format in str. Default: False
- """
- if to_str:
- if isinstance(word, str):
- return word
- if isinstance(word, unicode):
- try:
- return word.encode(coding)
- except UnicodeEncodeError:
- return __cf(word.encode("gbk"), to_str=True)
- if isinstance(word, unicode):
- return word
- if isinstance(word, str):
- try:
- return word.decode(coding)
- except UnicodeDecodeError:
- return __cf(word.decode("gbk"))
- class Compress(object):
- def __init__(self):
- self.tar_mode_map = dict(default="", gz="gz", bz2="bz2", bz="bz2")
- @classmethod
- def tar(self, path, mode="bz2", **kwargs):
- var = ":".join("w", self.tar_mode_map[mode])
- default_kwargs = dict(mode=var, encoding="gbk", ignore_zeros=True)
- default_kwargs.update(kwargs)
- filename = os.path.join(os.getcwd(), os.path.basename(path) + "_v%s.tar.bz2" % int(time.time()))
- with tarfile.open(__cf(filename), **default_kwargs) as tar_obj:
- tar_obj.add(__cf(path), arcname=os.path.basename(path), recursive=True)
- @staticmethod
- def zip(self):
- """
- python -m zipfile -c monty.zip life-of-brian_1979/
- """
- pass
- class Decompress(object):
- def __init__(self):
- self.tar_mode_map = dict(default="", gz="gz", bz2="bz2", bz="bz2")
- @classmethod
- def tar(self, path, mode="bz2", encoding="gbk", **kwargs):
- var = ":".join("r", self.tar_mode_map[mode])
- with tarfile.open(path, mode=mode, encoding=encoding) as tar_obj:
- tar_obj.extractall()
- @staticmethod
- def zip(sdir, ddir=os.getcwd()):
- """
- python -m zipfile -e monty.zip target-dir/
- """
- with zipfile.ZipFile(sdir, "r") as zip_obj:
- zip_obj.extractall(ddir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement