CoCalc Shared Fileswww / cgi-bin / mfd / bzip2.pyOpen in CoCalc with one click!
Author: William A. Stein
1
import os, tempfile
2
3
def compress(s):
4
fname = tempfile.mktemp()
5
file = open(fname,"w")
6
file.write(s)
7
file.close()
8
cmd = "bzip2 --best %s"%fname
9
os.system(cmd)
10
file = open("%s.bz2"%fname,"r")
11
ans = file.read()
12
file.close()
13
os.remove("%s.bz2"%fname)
14
return ans
15
16
def decompress(s):
17
fname = tempfile.mktemp()
18
file = open("%s.bz2"%fname,"w")
19
file.write(s)
20
file.close()
21
cmd = "bunzip2 %s.bz2"%fname
22
os.system(cmd)
23
file = open(fname,"r")
24
ans = file.read()
25
file.close()
26
os.remove(fname)
27
return ans
28
29
30