我帮助创建了一段压缩用户输入的代码。然后代码将此压缩版本传输到文件中。代码还将输入发送到文件中以查看文件已压缩了多少。
import gzip, time
plaintext = input("Please enter the text you want to compress: ")
file_ = open('text.txt', 'w')
file_.write(str(plaintext))
file_.close()
with gzip.open('compressed_file.text' + ".gz", "wb") as outfile:
outfile.write(bytes(plaintext, 'UTF-8'))
with open("data.txt","wb") as fh:
with open('compressed_file.text.gz', 'rb') as fd:
fh.write(fd.read())
我想要一些关于如何解压缩文件以进行原始用户输入的帮助。
答案 0 :(得分:2)
我认为,只需阅读GZIP文件并写回文件就可以帮到你。
if (KEY_STATUS.left) {
this.x -= this.speed;
if (this.x <= 0) // Keep player within the screen
this.x = 0;
}
else if (KEY_STATUS.right) {
this.x += this.speed;
if (this.x >= this.canvasWidth - this.width) this.x = this.canvasWidth - this.width;
}
if (KEY_STATUS.up) {
console.log(this.y);
this.y -= this.speed;
if (this.y < 0) this.y = 0;
// if (this.y <= this.canvasHeight / 4 * 3) this.y = this.canvasHeight / 4 * 3;
}
else if (KEY_STATUS.down) {
this.y += this.speed;
if (this.y >= this.canvasHeight - this.height) this.y = this.canvasHeight - this.height;
console.log(this.canvasHeight);
console.log(this.height);
}
if (KEY_STATUS.shift) {
this.speed += 2;
}
else if (KEY_STATUS.ctrl) {
if (this.speed > 2) {
this.speed -= 2;
}
else {
this.speed = 2;
}
}
输出
import gzip
plaintext = input("Please enter the text you want to compress: ")
with open("text.txt","wb") as file_:
file_.write(str(plaintext.encode('utf-8')))
filename = input("Please enter the desired filename: ")
print("Name of file to be zipped is text.txt")
print("Name of GZIP file is ",filename+'.gz')
print("Zipping file...")
with gzip.GzipFile(filename + ".gz", "wb") as outfile:
outfile.write(open("text.txt").read())
print("Name of unzipped file is unzip_text.txt")
print("Unzipping ...")
with gzip.GzipFile(filename + ".gz", 'rb') as inF:
with file("unzip_text.txt", 'wb') as outF:
s = inF.read()
outF.write(s.encode('utf-8'))
print("Content of unzip file is...")
with open("unzip_text.txt") as fd:
for line in fd.readlines():
print line
答案 1 :(得分:0)
这是我在另一个问题上找到的答案。
plaintext = input('Please enter some text')
filename = 'foo.gz'
with gzip.open(filename, 'wb') as outfile:
outfile.write(bytes(plaintext, 'UTF-8'))
with gzip.open(filename, 'r') as infile:
outfile_content = infile.read().decode('UTF-8')
print(outfile_content)
这会压缩输入,将其存储在文件中,并解压缩文件以进行原始输入。然后将此解压缩的输入打印到shell。