tarfile

Safely extract zip or tar using Python

Deadly 提交于 2019-11-29 05:15:05
问题 I'm trying to extract user-submitted zip and tar files to a directory. The documentation for zipfile's extractall method (similarly with tarfile's extractall) states that it's possible for paths to be absolute or contain .. paths that go outside the destination path. Instead, I could use extract myself, like this: some_path = '/destination/path' some_zip = '/some/file.zip' zipf = zipfile.ZipFile(some_zip, mode='r') for subfile in zipf.namelist(): zipf.extract(subfile, some_path) Is this safe?

Python tarfile and excludes

谁都会走 提交于 2019-11-28 04:17:07
问题 This is an excerpt from Python's documentation: If exclude is given it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False). I must admit that I have no idea what that means. Furthermore: Deprecated since version 2.7: The exclude parameter is deprecated, please use the filter parameter instead. For maximum portability, filter should be used as a keyword argument rather than as a

How to construct a TarFile object in memory from byte buffer in Python 3?

≡放荡痞女 提交于 2019-11-27 02:36:23
问题 Is it possible to create a TarFile object in memory using a buffer containing the tar data without having to write the TarFile to disk and open it up again? We get the bytes sent over a socket. Something like this: import tarfile byte_array = client.read_bytes() tar = tarfile.open(byte_array) # how to do this? # use "tar" as a regular TarFile object for member in tar.getmembers(): f = tar.extractfile(member) print(f) Note: one of the reasons for doing this is that we eventually want to be

How to create full compressed tar file using Python?

此生再无相见时 提交于 2019-11-26 18:23:45
How can I create a .tar.gz file with compression in Python? To build a .tar.gz (aka .tgz ) for an entire directory tree: import tarfile def make_tarfile(output_filename, source_dir): with tarfile.open(output_filename, "w:gz") as tar: tar.add(source_dir, arcname=os.path.basename(source_dir)) import tarfile tar = tarfile.open("sample.tar.gz", "w:gz") for name in ["file1", "file2", "file3"]: tar.add(name) tar.close() If you want to create a tar.bz2 compressed file, just replace file extension name with ".tar.bz2" and "w:gz" with "w:bz2". You call tarfile.open with mode='w:gz' , meaning "Open for

How to create full compressed tar file using Python?

﹥>﹥吖頭↗ 提交于 2019-11-26 04:38:23
问题 How can I create a .tar.gz file with compression in Python? 回答1: To build a .tar.gz (aka .tgz ) for an entire directory tree: import tarfile def make_tarfile(output_filename, source_dir): with tarfile.open(output_filename, "w:gz") as tar: tar.add(source_dir, arcname=os.path.basename(source_dir)) This will create a gzipped tar archive containing a single top-level folder with the same name and contents as source_dir . 回答2: import tarfile tar = tarfile.open("sample.tar.gz", "w:gz") for name in