Open michaelweinold opened 11 months ago
Using a test file (gzipped tarball) generated using
import os
import random
import string
import tarfile
import gzip
def create_tarball_and_gzip(tarball_name, num_files):
# Generate some files to include in the tarball
for i in range(num_files):
# Generate a random string of at least 10MB
file_size = 10 * 1024 * 1024
while True:
file_data = ''.join(random.choices(string.ascii_uppercase + string.digits, k=file_size))
if len(file_data.encode('utf-8')) >= file_size:
break
# Write the file to disk
with open(f'file_{i}.txt', 'w') as f:
f.write(file_data)
# Create the tarball
with tarfile.open(tarball_name, 'w') as tar:
for i in range(num_files):
tar.add(f'file_{i}.txt')
# Gzip the tarball
with open(tarball_name, 'rb') as f_in:
with gzip.open(tarball_name + '.gz', 'wb') as f_out:
f_out.writelines(f_in)
# Clean up the files
for i in range(num_files):
os.remove(f'file_{i}.txt')
create_tarball_and_gzip('testball', 10)
seems to work fine:
tarfile.open(
name = 'testball.gz',
mode = 'r:gz'
)
<tarfile.TarFile at 0x2e39d00>