Tarfile_Archive Access














































Tarfile_Archive Access



Archive File

An archive file is a file that is composed of one or more computer files along with metadata. Archive files are used to collect multiple data file together into a single file  for easier portability and storage, or simply to compress files to use less storage space.


Different Types of Archive Formats:-


  • Archiving only-
                             formats store metadata and concatenate files

  • Compression only-
                             formats only compress files
  • Multi-function-
                             formats can store metadata, concatenate, compress, encrypt, create error detection and recovery information, and package the archive into self-extracting and self-expanding files.

  • Software packaging- 
                            formats are used to create software packages that may be self-installing files.

  • Disk image-
                            formats are used to create disk images of mass storage volumes.



Examples:-



Creating a New Archive-

                                                To create a new archive, open the Tarfile with a mode of 'w'

CODE:-


import tarfile

def hello(a):

print('creating archive')
with tarfile.open(a, mode='w') as out:
print('adding README.txt')
out.add('README.txt')

print()
print('Contents:')
with tarfile.open(a, mode='r') as t:
for member_info in t.getmembers():
print(member_info.name)

a = 'phayes-geoPHP-1.2-20-g6855624.tar.gz'
hello(a)



OUTPUT:-

         

                let's run this code and see the output





Appending To Archives-

                                         It is possible to append to an existing file by using mode 'a'.



CODE:-



import tarfile

def appending(a):

print('creating archive')
with tarfile.open(a, mode='w') as out:
out.add('README.txt')

print('contents:',)
with tarfile.open(a, mode='r') as t:
print([m.name for m in t.getmembers()])

print('adding index.rst')
with tarfile.open(a, mode='a') as out:
out.add('index.rst')

print('contents:',)
with tarfile.open(a, mode='r') as t:
print([m.name for m in t.getmembers()])

a='phayes-geoPHP-1.2-20-g6855624.tar.gz'
appending(a)



OUTPUT:-

         

                let's run this code and we see that the resulting archive ends up with two members.






Comments