Example #1
0
def encode(text):

	Lziv = open('Lziv_'+file,'w')
	#text = lz.prep_text(file)

	t1 = time.clock()
	lztext = lz.encode(text,windowlen)
	Lziv.write(lztext)
	Lziv.close()
	t2 = time.clock()

	lztext = lz.prep_text('Lziv_'+file)
	t3 = time.clock()
	tree = huff.make_tree(lztext)
	t4 = time.clock()

	h = open('huff_'+file,'w')
	t5 = time.clock()
	htext = huff.encode(tree,lztext)
	#h.write(tree)
	h.write(htext)
	t6 = time.clock()
	h.close

	return [t2-t1,t4-t3,t6-t5],tree
Example #2
0
def encode(text):

    Lziv = open('Lziv_' + file, 'w')
    #text = lz.prep_text(file)

    t1 = time.clock()
    lztext = lz.encode(text, windowlen)
    Lziv.write(lztext)
    Lziv.close()
    t2 = time.clock()

    lztext = lz.prep_text('Lziv_' + file)
    t3 = time.clock()
    tree = huff.make_tree(lztext)
    t4 = time.clock()

    h = open('huff_' + file, 'w')
    t5 = time.clock()
    htext = huff.encode(tree, lztext)
    #h.write(tree)
    h.write(htext)
    t6 = time.clock()
    h.close

    return [t2 - t1, t4 - t3, t6 - t5], tree
Example #3
0
	def compress(self):
		filename, file_extension = os.path.splitext(self.path)
		output_path = filename + "_compressed.txt"

		with open(self.path, 'r+') as file, open(output_path, 'wb') as output:
			text = file.read()
			text = text.rstrip()

			frequency = self.make_frequency_dict(text)
			self.make_heap(frequency)
			self.merge_nodes()
			self.make_codes()

			encoded_text = self.get_encoded_text(text)
			padded_encoded_text = self.pad_encoded_text(encoded_text)

			b = self.get_byte_array(padded_encoded_text)
			output.write(bytes(b))

			# Use Lempel Ziv technique too
			LempelZiv.modified_compress(b)

		print("Compressed")
		return output_path
Example #4
0
	def decompress(self, input_path):
		filename, file_extension = os.path.splitext(self.path)
		output_path = filename + "_decompressed" + ".txt"

		with open(input_path, 'rb') as file, open(output_path, 'w') as output:
			bit_string = ""

			byte = file.read(1)
			while(len(byte) > 0):
				byte = ord(byte)
				bits = bin(byte)[2:].rjust(8, '0')
				bit_string += bits
				byte = file.read(1)

			xyz = LempelZiv.modified_decompress()

			encoded_text = self.remove_padding(bit_string)

			decompressed_text = self.decode_text(encoded_text)

			output.write(decompressed_text)

		print("Decompressed")
		return output_path
Example #5
0
def decode(file, tree):
	f= open('huff_'+file,'r')
	text = f.read()
	f.close
	print lz.decode(huff.decode(tree, text))
Example #6
0
	lztext = lz.prep_text('Lziv_'+file)
	t3 = time.clock()
	tree = huff.make_tree(lztext)
	t4 = time.clock()

	h = open('huff_'+file,'w')
	t5 = time.clock()
	htext = huff.encode(tree,lztext)
	#h.write(tree)
	h.write(htext)
	t6 = time.clock()
	h.close

	return [t2-t1,t4-t3,t6-t5],tree

def decode(file, tree):
	f= open('huff_'+file,'r')
	text = f.read()
	f.close
	print lz.decode(huff.decode(tree, text))

if __name__ == "__main__":
	file = raw_input("File to be encoded?")
	text = lz.prep_text(file)
	start= time.clock()
	times, tree=encode(text)
	print times
	end = time.clock()
	print end-start
	decode(file,str(tree))
Example #7
0
def decode(file, tree):
    f = open('huff_' + file, 'r')
    text = f.read()
    f.close
    print lz.decode(huff.decode(tree, text))
Example #8
0
    t3 = time.clock()
    tree = huff.make_tree(lztext)
    t4 = time.clock()

    h = open('huff_' + file, 'w')
    t5 = time.clock()
    htext = huff.encode(tree, lztext)
    #h.write(tree)
    h.write(htext)
    t6 = time.clock()
    h.close

    return [t2 - t1, t4 - t3, t6 - t5], tree


def decode(file, tree):
    f = open('huff_' + file, 'r')
    text = f.read()
    f.close
    print lz.decode(huff.decode(tree, text))


if __name__ == "__main__":
    file = raw_input("File to be encoded?")
    text = lz.prep_text(file)
    start = time.clock()
    times, tree = encode(text)
    print times
    end = time.clock()
    print end - start
    decode(file, str(tree))