Python API

Archive

class RPA.Archive.Archive

Archive is a library for operating with ZIP and TAR packages.

Examples

*** Settings ***
Library  RPA.Archive

*** Tasks ***
Creating a ZIP archive
   Archive Folder With ZIP   ${CURDIR}${/}tasks  tasks.zip   recursive=True  include=*.robot  exclude=/.*
   @{files}                  List Archive             tasks.zip
   FOR  ${file}  IN  ${files}
      Log  ${file}
   END
   Add To Archive            .${/}..${/}missing.robot  tasks.zip
   &{info}                   Get Archive Info
from RPA.Archive import Archive

lib = Archive()
lib.archive_folder_with_tar('./tasks', 'tasks.tar', recursive=True)
files = lib.list_archive('tasks.tar')
for file in files:
   print(file)
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
add_to_archive(files: Union[List, str], archive_name: str, folder: Optional[str] = None) None

Add file(s) to the archive

Parameters
  • files – name of the file, or list of files, to add

  • archive_name – filename of the archive

  • folder – name of the folder where the file will be added, relative path within the archive

Returns

None

This keyword adds a file or list of files into an existing archive. Files can be added to archive structure with relative path using argument folder.

Example:

Add To Archive  extrafile.txt  myfiles.zip
Add To Archive  stat.png       archive.tar.gz  images
@{files}        Create List    filename1.txt   filename2.txt
Add To Archive  ${files}       files.tar
archive_folder_with_tar(folder: str, archive_name: str, recursive: bool = False, include: Optional[str] = None, exclude: Optional[str] = None) None

Create a tar/tar.gz archive of a folder

Parameters
  • folder – name of the folder to archive

  • archive_name – filename of the archive

  • recursive – should sub directories be included, default is False

  • include – define file pattern to include in the package, default is None which means all files are included

  • exclude – define file pattern to exclude from the package, default is None

Returns

None

This keyword creates an TAR or TAR.GZ archive of a local folder. Type of archive is determined by the file extension. By default subdirectories are not included, but they can included using recursive argument.

To include only certain files, like TXT files, the argument include can be used. Similarly to exclude certain file, like dotfiles, the argument exclude can be used.

Example:

Archive Folder With TAR  ${CURDIR}${/}documents  documents.tar
Archive Folder With TAR  ${CURDIR}${/}tasks      tasks.tar.gz   include=*.robot
Archive Folder With TAR  ${CURDIR}${/}tasks      tasks.tar      exclude=/.*
Archive Folder With TAR  ${CURDIR}${/}documents  documents.tar  recursive=True
archive_folder_with_zip(folder: str, archive_name: str, recursive: bool = False, include: Optional[str] = None, exclude: Optional[str] = None, compression: str = 'stored') None

Create a zip archive of a folder

Parameters
  • folder – name of the folder to archive

  • archive_name – filename of the archive

  • recursive – should sub directories be included, default is False

  • include – define file pattern to include in the package, default is None which means all files are included

  • exclude – define file pattern to exclude from the package, default is None

  • compression – type of package compression method, default is “stored”

Returns

None

This keyword creates an ZIP archive of a local folder. By default subdirectories are not included, but they can be included using the recursive argument.

To include only certain files, like TXT files, the argument include can be used. Similarly to exclude certain files, like dotfiles, the argument exclude can be used.

Compression methods:

  • stored, default

  • deflated

  • bzip2

  • lzma

Example:

Archive Folder With Zip  ${CURDIR}${/}documents  mydocs.zip
Archive Folder With Zip  ${CURDIR}${/}tasks      robottasks.zip   include=*.robot
Archive Folder With Zip  ${CURDIR}${/}tasks      no_dotfiles.zip  exclude=/.*
Archive Folder With Zip  ${CURDIR}${/}documents  documents.zip    recursive=True
Archive Folder With Zip  ${CURDIR}               packagelzma.zip  compression=lzma
Archive Folder With Zip  ${CURDIR}               bzipped.zip      compression=bzip2
from RPA.Archive import Archive

lib = Archive()
lib.archive_folder_with_zip('./documents', 'mydocs.zip')
lib.archive_folder_with_zip('./tasks', 'robottasks.zip', include='*.robot')
lib.archive_folder_with_zip('./tasks', 'no_dotfiles.zip', exclude='/.*')
lib.archive_folder_with_zip('./documents', 'documents.zip', recursive=True)
lib.archive_folder_with_zip('./', 'packagelzma.zip', compression='lzma')
lib.archive_folder_with_zip('./', 'bzipped.zip', compression='bzip2')
extract_archive(archive_name: str, path: Optional[str] = None, members: Optional[Union[List, str]] = None) None

Extract files from archive into local directory

Parameters
  • archive_name – filename of the archive

  • path – filepath to extract file into, default is current working directory

  • members – list of files to extract from archive, by default all files in archive are extracted

Returns

None

This keyword supports extracting files from zip, tar and tar.gz archives.

By default file is extracted into current working directory, but path argument can be set to define extraction path.

Example:

Extract Archive    myfiles.zip   ${CURDIR}${/}extracted
@{files}           Create List   filename1.txt    filename2.txt
Extract Archive    archive.tar   C:${/}myfiles${/}  ${files}
extract_file_from_archive(filename: str, archive_name: str, path: Optional[str] = None) None

Extract a file from archive into local directory

Parameters
  • filename – name of the file to extract

  • archive_name – filename of the archive

  • path – filepath to extract file into, default is current working directory

Returns

None

This keyword supports extracting a file from zip, tar and tar.gz archives.

By default file is extracted into current working directory, but path argument can be set to define extraction path.

Example:

Extract File From Archive    extrafile.txt   myfiles.zip
Extract File From Archive    background.png  images.tar.gz  ${CURDIR}${/}extracted
get_archive_info(archive_name: str) dict

Get information about the archive

Parameters

archive_name – filename of the archive

Returns

dictionary variable containing the keys filename, filemode, size, mtime, modified

Returns following file attributes in a dictionary:

  • filename

  • filemode

  • size

  • mtime

  • last modification time in format %d.%m.%Y %H:%M:%S

Example:

&{archiveinfo}   Get Archive Info    myfiles.zip
list_archive(archive_name: str) list

List files in an archive

Parameters

archive_name – filename of the archive

Returns

dictionary variable containing the keys name, size, mtime, modified

Returns list of files, where each file in a list is a dictionary with following attributes:

  • name

  • size

  • mtime

  • last modification time in format %d.%m.%Y %H:%M:%S

Example:

@{files}   List Archive    myfiles.zip
FOR  ${file}  IN   ${files}
    Log  ${file}[filename]
    Log  ${file}[size]
    Log  ${file}[mtime]
END