-
Notifications
You must be signed in to change notification settings - Fork 1
/
filemanager.py
58 lines (51 loc) · 1.98 KB
/
filemanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
from glob import glob
import cStringIO
import binascii
import zlib
class FileManager:
file_write_progress = {}
cached_chunks = {}
def find_chunk(self, file_name, start_byte_number, end_byte_number):
f = open(file_name, 'rb')
ignore_bytes = f.read(start_byte_number)
bytes_to_read = f.read(end_byte_number - start_byte_number)
#return zlib.compress(bytes_to_read, 9)
return bytes_to_read
def uncompress_chunk(self, compressed_chunk):
#return zlib.decompress(compressed_chunk)
return compressed_chunk
def find_file(self, file_name):
# Check if the file_name exists in our current directory
path = glob(file_name)
if path:
return os.path.getsize(file_name)
else:
return None
def receive_chunk(self, file_name, start_byte, finish_byte, chunk):
if file_name not in self.file_write_progress:
self.file_write_progress[file_name] = 0
progress = self.file_write_progress[file_name]
if start_byte == progress:
f = open(file_name, "ab")
f.write(chunk)
print "writing chunk starting at %s" % progress
progress = finish_byte + 1
while ((file_name, progress) in self.cached_chunks):
print "writing chunk starting at %s from cached" % progress
c = self.cached_chunks[(file_name, progress)]
f.write(c[0])
del self.cached_chunks[(file_name, progress)]
progress = c[1] + 1
f.close()
else:
self.cached_chunks[(file_name, start_byte)] = (chunk, finish_byte)
self.file_write_progress[file_name] = progress
if __name__ == "__main__":
f = FileManager()
chunk = f.find_chunk("tozip.txt", 0, 512)
chunk = f.uncompress_chunk(chunk)
print chunk
# a = Find_Chunk()
# compressed_chunk = a.find_chunk('test.txt', 1, 3)
# a.uncompress_chunk(compressed_chunk)