Arbeide med zip-filer i Python

Arbeide med zip-filer i Python
Denne artikkelen forklarer hvordan man kan utføre ulike operasjoner på en zip-fil ved hjelp av et enkelt python-program. Hva er en zip-fil? ZIP er et arkivfilformat som støtter tapsfri datakomprimering. Med tapsfri komprimering mener vi at komprimeringsalgoritmen gjør at originaldata kan rekonstrueres perfekt fra de komprimerte dataene. Så en ZIP-fil er en enkelt fil som inneholder en eller flere komprimerte filer som tilbyr en ideell måte å gjøre store filer mindre og holde relaterte filer sammen. Hvorfor trenger vi zip-filer?
  • For å redusere lagringskravene.
  • For å forbedre overføringshastigheten over standardforbindelser.
For å jobbe med zip-filer ved hjelp av python vil vi bruke en innebygd python-modul kalt zip-fil .

1. Pakk ut en zip-fil

Python
   # importing required modules   from   zipfile   import   ZipFile   # specifying the zip file name   file_name   =   'my_python_files.zip'   # opening the zip file in READ mode   with   ZipFile  (  file_name     'r'  )   as   zip  :   # printing all the contents of the zip file   zip  .  printdir  ()   # extracting all the files   print  (  'Extracting all the files now...'  )   zip  .  extractall  ()   print  (  'Done!'  )   
The above program extracts a zip file named 'my_python_files.zip' in the same directory as of this python script. The output of above program may look like this: Arbeide med zip-filer i PythonLa oss prøve å forstå koden ovenfor i stykker:
  • from zipfile import ZipFile 
    ZipFile is a class of zipfile module for reading and writing zip files. Here we import only class ZipFile from zipfile module.
  • with ZipFile(file_name 'r') as zip: 
    Here a ZipFile object is made by calling ZipFile constructor which accepts zip file name and mode parameters. We create a ZipFile object in LESE modus og navngi den som zip .
  • zip.printdir() 
    printdir() metoden skriver ut en innholdsfortegnelse for arkivet.
  • zip.extractall() 
    extractall() metoden vil trekke ut alt innholdet i zip-filen til gjeldende arbeidskatalog. Du kan også ringe extract() method to extract any file by specifying its path in the zip file. For example:
    zip.extract('python_files/python_wiki.txt') 
    This will extract only the specified file. If you want to read some specific file you can go like this:
    data = zip.read(name_of_file_to_read) 

2. Skrive til en zip-fil

Tenk på en katalog (mappe) med et slikt format: Arbeide med zip-filer i Python Here we will need to crawl the whole directory and its sub-directories in order to get a list of all file paths before writing them to a zip file. The following program does this by crawling the directory to be zipped: Python
   # importing required modules   from   zipfile   import   ZipFile   import   os   def   get_all_file_paths  (  directory  ):   # initializing empty file paths list   file_paths   =   []   # crawling through directory and subdirectories   for   root     directories     files   in   os  .  walk  (  directory  ):   for   filename   in   files  :   # join the two strings in order to form the full filepath.   filepath   =   os  .  path  .  join  (  root     filename  )   file_paths  .  append  (  filepath  )   # returning all file paths   return   file_paths   def   main  ():   # path to folder which needs to be zipped   directory   =   './python_files'   # calling function to get all file paths in the directory   file_paths   =   get_all_file_paths  (  directory  )   # printing the list of all files to be zipped   print  (  'Following files will be zipped:'  )   for   file_name   in   file_paths  :   print  (  file_name  )   # writing files to a zipfile   with   ZipFile  (  'my_python_files.zip'    'w'  )   as   zip  :   # writing each file one by one   for   file   in   file_paths  :   zip  .  write  (  file  )   print  (  'All files zipped successfully!'  )   if   __name__   ==   '__main__'  :   main  ()   
The output of above program looks like this: Arbeide med zip-filer i PythonLa oss prøve å forstå koden ovenfor ved å dele inn i fragmenter:
  • def get_all_file_paths(directory): file_paths = [] for root directories files in os.walk(directory): for filename in files: filepath = os.path.join(root filename) file_paths.append(filepath) return file_paths 
    First of all to get all file paths in our directory we have created this function which uses the os.walk()  metode. I hver iterasjon legges alle filene som finnes i den katalogen til en liste kalt filstier . Til slutt returnerer vi alle filbanene.
  • file_paths = get_all_file_paths(directory) 
    Here we pass the directory to be zipped to the get_all_file_paths() funksjon og få en liste som inneholder alle filstier.
  • with ZipFile('my_python_files.zip''w') as zip: 
    Here we create a ZipFile object in WRITE mode this time.
  • for file in file_paths: zip.write(file) 
    Here we write all the files to the zip file one by one using skrive metode.

3. Få all informasjon om en zip-fil

Python
   # importing required modules   from   zipfile   import   ZipFile   import   datetime   # specifying the zip file name   file_name   =   'example.zip'   # opening the zip file in READ mode   with   ZipFile  (  file_name     'r'  )   as   zip  :   for   info   in   zip  .  infolist  ():   print  (  info  .  filename  )   print  (  '  t  Modified:  t  '   +   str  (  datetime  .  datetime  (  *  info  .  date_time  )))   print  (  '  t  System:  tt  '   +   str  (  info  .  create_system  )   +   '(0 = Windows 3 = Unix)'  )   print  (  '  t  ZIP version:  t  '   +   str  (  info  .  create_version  ))   print  (  '  t  Compressed:  t  '   +   str  (  info  .  compress_size  )   +   ' bytes'  )   print  (  '  t  Uncompressed:  t  '   +   str  (  info  .  file_size  )   +   ' bytes'  )   
The output of above program may look like this:
for info in zip.infolist(): 
Here infoliste() metoden oppretter en forekomst av ZipInfo klasse som inneholder all informasjon om zip-filen. Vi har tilgang til all informasjon som siste endringsdato for filene filnavnsystemet som filene ble opprettet på. Zip-versjonsstørrelsen på filer i komprimert og ukomprimert form osv. Denne artikkelen er bidratt av Nikhil Kumar . Lag quiz