Write a bash script called unpack which can unpack multiplepacked files (a.k.a “archives”),and even traverse folders recursively and unpack all archives inthem - regardless of thespecific algorithm that was used when packing them. Following isthe exact synopsis:unpack [-r] [-v] file [file...]Given a list of filenames as input, this script queries each targetfile (parsing the output of thefile command) for the type of compression used on it. Then thescript automatically invokesthe appropriate decompression command, putting files in the samefolder. If files with thesame name already exist, they are overwritten.Unpack should support 4 unpacking options - gunzip, bunzip2, unzip,uncompress.Adding more options should be VERY simple.Note that file names and extensions have no meaning - the only wayto know what methodto use is through the file command!If a target file is not compressed, the script takes no otheraction on that particular file.If the target is a directory then it decompresses all files in itusing same method.Command echos number of archives decompressedCommand returns number of files it did NOT decompressAdditional switches:-v - verbose - echo each file decompressed & warn for each filethat was NOTdecompressed-r - recursive - will traverse contents of folders recursively,performing unpack on each.
And please don't copy this answer as it is wrong andalready present in answers.
import osimport argparseimport sys
def get_file_type(filename):with open(filename, "rb") as f:header = f.read(4)if header == b"PK\x03\x04":return ".zip"elif header == b"\x42\x5A\x68":return ".bz2"elif header == b"\x1F\x8B\x08":return ".gz"elif header == b"\x1F\x9D":return ".z"else:return None
def unpack_file(filename):filetype = get_file_type(filename)if filetype == ".zip":os.system("unzip " + filename)elif filetype == ".gz":os.system("gunzip " + filename)elif filetype == ".bz2":os.system("bunzip2 " + filename)elif filetype == ".z":os.system("uncompress " + filename)else:return Falsereturn True
def unpack_recursively(filename, verbose_mode=False,recursive_mode=False):if recursive_mode:for root, dirs, files in os.walk(filename):for name in files:filename = os.path.join(root, name)filetype = get_file_type(filename)if filetype == ".zip":if verbose_mode:print("Unpacking " + filename)os.system("unzip " + filename)elif filetype == ".gz":if verbose_mode:print("Unpacking " + filename)os.system("gunzip " + filename)elif filetype == ".bz2":if verbose_mode:print("Unpacking " + filename)os.system("bunzip2 " + filename)elif filetype == ".z":if verbose_mode:print("Unpacking " + filename)os.system("uncompress " + filename)else:if verbose_mode:print("Ignoring " + filename)continue
def main():parser = argparse.ArgumentParser(description="Unpack multiplepacked files.")parser.add_argument("-v", "--verbose", action="store_true",help="echo each file decompressed & warn for each file that wasnot decompressed")parser.add_argument("-r", "--recursive", action="store_true",help="will traverse contents of folders recursively, performingunpack on each")parser.add_argument("filename", nargs="+", help="input filename")args = parser.parse_args()
if len(sys.argv) == 1:parser.print_help()sys.exit(1)
for filename in args.filename:if os.path.isdir(filename):unpack_recursively(filename, args.verbose, args.recursive)else:if unpack_file(filename):if args.verbose:print("Unpacking " + filename)continueelse:if args.verbose:print("Ignoring " + filename)continue
if __name__ == "__main__":main()
Write a bash script called unpack which can unpack multiple packed files (a.k.a “archives”), and even traverse folders r
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am