Simple file archiver in Python Part 1:Main

Today I would like to explain how to create a simple file archiver.

First, we need to understand what exactly is file archiving. We can understand the file as a collection of bytes and file archive as a file that contains a group of other files.

So with that knowledge, I can start my code:

import sys
arc_buf = bytearray()
new_file_bytes = [0x10,0xee,0x01,ord("c"),ord("a")]
file_name_bytes = [0x10,0xee,0x02,ord("c"),ord("a")]
file_content_bytes = [0x10,0xee,0x03,ord("c"),ord("a")]
#ord change character to ascii number

Let start with the first line. arc_buf mean archive_buffer and bytearray() is a function that creates an array of bytes. The next three lines are integer arrays that contain byte signatures. Of course, the size of an integer is much bigger than byte, but bytearray can convert int to byte so I don’t need to worry.

arg_list = sys.argv
arg_list.pop(0)
main(arg_list)

Now we get an argument list from sys library that I imported in the previous part of the code and pop the first argument on the list. Why I don’t need the first argument? Because the first argument in sys.argv is always “python”*. The last line calls the main() function.

Now I need to create main() function:

def main(args):
    print("Cold Archiver")
    if args[0] == "-a": archive(args[1:])
    elif args[0] == "-u": unarchive(args[1])
    else:
        print("-a [filenames] - archive")
        print("-u [filenames] - unarchive")

In this part of the code, I need to check what function user wants to use. To do that I need to create a command pattern and implement it. So for archive let use the “python cold_archiver.py -a [file name1] [file name2] [file name…]” and for unarchiving “python cold_archiver.py -u [file name.ca]” also I have to inform the user how to use our program

That’s all for this part.

*- it might not be the first argument when you compile it with pyinstaller

GITHUB: https://github.com/cold0dev/ColdArchiver/blob/master/cold_archiver.py

Leave a Comment