Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python gzip

# GZIP compress bytes
import gzip

s_in = b"some random text"
s_out = gzip.compress(s_in)


# GZIP decompress bytes
import gzip

s_in = b"some random text"
s_out = gzip.compress(s_in)
print(s_out)
print(gzip.decompress(s_out))



# GZIP compress file
import gzip
import shutil

with open('/mypath/file.txt', 'rb') as f_in:
    with gzip.open('/mypath/file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

# GZIP Decompress file
import gzip
import shutil
with gzip.open('file.txt.gz', 'rb') as f_in:
    with open('file.txt', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

# GZIP Compress file from command line

python3.10 -m gzip --fast test.txt # fast compression
python3.10 -m gzip --best test.txt # best compression


# GZIP Decompress file from command line
python3.10 -m gzip -d test.txt.gz

Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #python #gzip
ADD COMMENT
Topic
Name
6+9 =