Skip to content

Instantly share code, notes, and snippets.

@ShahriyarR
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShahriyarR/9548274 to your computer and use it in GitHub Desktop.
Save ShahriyarR/9548274 to your computer and use it in GitHub Desktop.
"""
Shahriyar Rzayev, rzayev.sehriyar@gmail.com
Written in Python 3 using shutil.make_archive. (Tested with Python 3.3.5)
Purpose of class to create tar.gz archive files.
General usage is to create crontab, for archiving log files or another .zip files, to one archive file.
After All usage process there will be created zip_archive.tar.gz file.
Compression level: Default
"""
from shutil import make_archive, move, rmtree
import os
class Archiver:
def __init__(self):
self.local_dir = '/home'
self.archive_dir = '/home/archive'
self.archive_name = os.path.join(self.local_dir, 'zip_archive')
def file_search(self):
"""
Search for ZIP files
"""
file_list = []
for i in os.listdir(self.local_dir):
if os.path.splitext(i)[1] == '.zip':
file_list.append(i)
return file_list
def move_files(self):
"""
Move ZIP files to archive directory. It will be a base directory for archiving
"""
if len(self.file_search()) > 0:
if os.path.exists(self.archive_dir):
rmtree(self.archive_dir)
if not os.path.exists(self.archive_dir):
os.makedirs(self.archive_dir)
if os.path.exists(self.archive_dir):
for i in self.file_search():
move(os.path.join(self.local_dir,i),self.archive_dir)
print("Successfully Moved")
return True
else:
print("Move Failed")
return False
else:
if not os.path.exists(self.archive_dir):
os.makedirs(self.archive_dir)
if os.path.exists(self.archive_dir):
for i in self.file_search():
move(os.path.join(self.local_dir,i),self.archive_dir)
print("Successfully Moved")
return True
else:
print("Move Failed")
return False
else:
print("Move Failed, There no such files to move")
return False
def create_tar_gz(self):
"""
Creating Archive File.
"""
if self.move_files():
make_archive(self.archive_name, 'gztar', self.archive_dir)
print("success! Archive file created!")
else:
print("Nothing Happened!!")
x = Archiver()
x.create_tar_gz()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment