0

I have had help creating a piece of code that compresses a input from a user.The code then transfers this compressed version into a file. The code also sends the input into a file to see how much the file has been compressed.

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())

I want some help on how to decompress the file to make the original user input.

Mr.Code
  • 9
  • 1
  • 2
  • 11
  • What's giving you trouble? It seems to be a pretty simple problem given the utilities in the `gzip` module... – mgilson Dec 21 '16 at 19:13
  • I want to decompress what i have compressed. But i don't know how and would like some assistance. The second piece of code only sends the compressed sentence back to the user, it don't make the original sentence which is what i want to achieve. – Mr.Code Dec 22 '16 at 11:46

2 Answers2

2

I think, just reading GZIP file and writing back to file will help you.

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

Output :

C:\Users\dinesh_pundkar\Desktop>python gz.py
Please enter the text you want to compress: "My name is Dinesh"
Please enter the desired filename: "dinesh"
Name of file to be zipped is text.txt
('Name of GZIP file is ', 'dinesh.gz')
Zipping file...
Name of unzipped file is unzip_text.txt
Unzipping ...
Content of unzip file is...
My name is Dinesh

C:\Users\dinesh_pundkar\Desktop>
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
  • Traceback (most recent call last): File "E:\GCSE WORK\Computing\A 4.5.3\Coursework\Task 3\Task 3.py", line 13, in with gzip.open('text.txt.gz', 'rb') as fd: File "C:\Python33\lib\gzip.py", line 52, in open binary_file = GzipFile(filename, gz_mode, compresslevel) File "C:\Python33\lib\gzip.py", line 184, in __init__ fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') FileNotFoundError: [Errno 2] No such file or directory: 'text.txt.gz' >>> – Mr.Code Dec 21 '16 at 19:20
  • Replace "text.txt.gz" by the GZIP file name you code is creating above – Dinesh Pundkar Dec 21 '16 at 19:22
  • I have but it comes out saying that "FileNotFoundError: [Errno 2] No such file or directory: 'comp2'" comp2 is the file name. – Mr.Code Dec 21 '16 at 19:27
  • I altered the code and it did not give me the decompressed file, it gave me the compressed file back – Mr.Code Dec 21 '16 at 19:52
  • Your bit of code simply returns the compressed file, it does not decompress it to make the original input. – Mr.Code Dec 21 '16 at 19:53
  • it comes up with error TypeError: 'str' does not support the buffer interface – Mr.Code Dec 21 '16 at 20:09
  • i am using python 3.3.3 – Mr.Code Dec 21 '16 at 20:10
  • Gotcha... Just add encode('utf-8'). Please check updated code. – Dinesh Pundkar Dec 21 '16 at 20:18
  • file_.write(str(plaintext.encode('utf-8'))) TypeError: 'str' does not support the buffer interface – Mr.Code Dec 21 '16 at 20:50
  • I have changed the code so it removes the str from the top part of the code. Please check the code Know. But i still get an error message(self.crc = zlib.crc32(data, self.crc) & 0xffffffff TypeError: 'str' does not support the buffer interface) – Mr.Code Dec 22 '16 at 11:20
0

This is the answer as I found it on the answer to another question.

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)

This compresses the input, stores it in the a file and also decompress the file to make the original input. This decompressed input is then printed to the shell.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Mr.Code
  • 9
  • 1
  • 2
  • 11