forked from vikram7/vkrunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvkrunch.rb
More file actions
99 lines (94 loc) · 3.08 KB
/
vkrunch.rb
File metadata and controls
99 lines (94 loc) · 3.08 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def to_binary(array)
array.pack("S*")
end
def from_binary(binary)
binary.unpack("S*")
end
def compress(to_compress)
# create dictionary with all 256 ascii codes
dictionary = Hash.new
256.times do |ascii_code|
dictionary[ascii_code.chr] = ascii_code
end
s = to_compress[0]
to_compress.each_char.reduce [] do |output, c|
if dictionary.include?(s + c)
s = s + c
else
output << dictionary[s]
dictionary[s + c] = dictionary.size
s = c
end
output
end
end
def uncompress(to_uncompress)
dictionary = (0..255).to_a.map { |element| element.chr }
output = Array.new
current = to_uncompress.shift
output << current
to_uncompress.each do |index|
previous = current
current = index
if current < dictionary.length
s = dictionary[current]
output << s
dictionary << dictionary[previous] + s[0]
else
s = dictionary[previous]
output << s
dictionary << s
end
end
output.shift
output
end
if ARGV[0] != "-c" && ARGV[0] != "-u" || !File.exist?(ARGV[1])
puts "To use this utility, please see below:"
puts "To compress file.txt : ruby vkrunch.rb -c file.txt"
puts "To uncompress file.txt.vkrunch : ruby vkrunch.rb -u file.txt.vkrunch"
elsif ARGV[0] == "-u" && !ARGV[1].match(/.\.(vkrunch)$/)
puts "Sorry, vkrunch extensions are the only type supported right now."
puts "To compress file.txt : ruby vkrunch.rb -c file.txt"
puts "To uncompress file.txt.vkrunch : ruby vkrunch.rb -u file.txt.vkrunch"
else
starttime = Time.now
option = ARGV[0]
filename = ARGV[1]
file = File.open(filename, "r")
contents = file.read
if ARGV[0] == "-c" && contents.match(/[^\x00-\x7F]/)
puts "Sorry, but " + filename + " contains non-ASCII characters. We don't support non-ASCII characters yet. Please try again after encoding the file in ASCII format."
else
filesize = file.size / 1000
file.close
if option == "-c"
compressed = compress(contents)
packed = to_binary(compressed)
outputname = filename + ".vkrunch"
f = File.new(outputname, "w")
f.write(packed)
endtime = Time.now
newfilesize = f.size / 1000
ratio = 1 - newfilesize / filesize.to_f
puts
puts outputname + " created"
puts "________________________________________________________"
puts "Original file name : " + filename
puts "VKrunched file name : " + outputname
puts "Original file size : " + filesize.to_s + "K"
puts "VKrunched file size : " + newfilesize.to_s + "K"
puts "Compression took " + "%.4f" % (endtime - starttime).to_s + " seconds"
puts "VKrunched file is " + "%.1f" % (ratio*100).to_s + "% smaller than the original file"
puts "________________________________________________________"
elsif option == "-u"
unpacked = from_binary(contents)
uncompressed = uncompress(unpacked).join
outputname = "_" + filename.gsub(".vkrunch","")
f = File.new(outputname, "w")
f.write(uncompressed)
puts "file uncompressed"
puts outputname + " created"
end
end
end