-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecompactador.rb
151 lines (128 loc) · 4.48 KB
/
recompactador.rb
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env ruby
$:.unshift(File.expand_path("vendor/rubyzip-1.2.2/lib"))
$:.unshift(File.expand_path("vendor/ruby-progressbar-1.10.0/lib"))
require 'fileutils'
require 'zip'
require 'ruby-progressbar'
in_zip_or_folder = ARGV[0]
if in_zip_or_folder.nil? || (!File.file?(in_zip_or_folder) && !File.directory?(in_zip_or_folder))
puts "ERRO: Pasta ou Arquivo inválido!"
return
end
out_zip_name = ARGV[1]
if out_zip_name.nil?
out_zip_name = File.basename(in_zip_or_folder, '.zip')
puts "Nome do arquivo destino não informado, setado como: #{out_zip_name}"
end
chunk_mb = ARGV[2]
if chunk_mb.nil?
chunk_mb = 50
puts "Tamanho limite dos arquivos não informado, setado como: #{chunk_mb}mb"
end
base_dir = File.dirname(in_zip_or_folder)
out_zip_path = File.join(base_dir, out_zip_name)
def count_files_dir(dir_path)
Dir.glob(File.join(dir_path, '**', '*')).select do |file|
File.file?(file)
end.count
end
def mb_to_bytes(mb = 0)
mb.to_i * (2 ** 20)
end
def progress_bar(title, total)
ProgressBar.create(
:format => '%a%E %B %p%% %t',
:title => title,
:total => total
)
end
def unzip_it(zip_path, destination_dir)
Zip.default_compression = Zlib::BEST_COMPRESSION
Zip::File.open(zip_path) do |zip_file|
zip_name = File.basename(zip_file.name)
progressbar = progress_bar("Descompactando #{zip_name}", zip_file.entries.size)
zip_file.each do |zip_entry|
next if zip_entry.name =~ /__MACOSX/ || zip_entry.name =~ /\.DS_Store/ || !zip_entry.file?
filename = File.basename(zip_entry.name)
entry_path = File.join(destination_dir, filename)
FileUtils.rm_r(entry_path) if File.file?(entry_path)
zip_entry.extract(entry_path)
progressbar.increment
end
progressbar.finish unless progressbar.finished?
end
destination_dir
end
def zip_it(zip_path, files)
FileUtils.rm_r(zip_path) if File.file?(zip_path)
Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile|
zip_name = File.basename(zip_path)
progressbar = progress_bar("Compactando #{zip_name}", files.size)
files.each do |file_path|
next if file_path =~ /__MACOSX/ || file_path =~ /\.DS_Store/ || !File.file?(file_path)
entry_name = File.basename(file_path)
zipfile.add(entry_name, file_path)
progressbar.increment
end
progressbar.finish unless progressbar.finished?
end
zip_path
end
def finish_tmp_file(tmp_path, zip_path, stream_file = nil, mb_limit = 0)
tmp_size = File.size(tmp_path)
if tmp_size >= mb_to_bytes(mb_limit)
FileUtils.rm_r(zip_path) if File.file?(zip_path)
FileUtils.mv tmp_path, zip_path
FileUtils.rm_r(tmp_path) if File.file?(tmp_path)
stream_file.close if stream_file
return true
end
false
end
def add_file_to_stream(file_path, stream_file)
stream_file.put_next_entry File.basename(file_path)
stream_file.write File.read(file_path)
end
def package_it(out_zip_path, files_dir, split_in_mb = nil)
return zip_it("#{out_zip_path}.zip", files_dir) if split_in_mb.nil?
zip_counter = 0
zip_path = nil
tmp_path = nil
stream_file = nil
progressbar = progress_bar("...", count_files_dir(files_dir))
zip_pool = []
Dir.foreach(files_dir) do |file_name|
file_path = File.join(files_dir, file_name)
next if file_path =~ /__MACOSX/ || file_path =~ /\.DS_Store/ || !File.file?(file_path)
if zip_path.nil?
zip_counter += 1
zip_path = "#{out_zip_path}_#{zip_counter}.zip"
zip_dirname, zip_basename = ::File.split(zip_path)
zip_pool << zip_basename
tmp_path = Dir::Tmpname.create(zip_basename, zip_dirname) {}
progressbar.title = "Compactando #{File.basename(zip_path)}"
stream_file = Zip::OutputStream.new(tmp_path)
end
add_file_to_stream(file_path, stream_file)
if finish_tmp_file(tmp_path, zip_path, stream_file, split_in_mb)
zip_path = nil
end
progressbar.increment
end
finish_tmp_file(tmp_path, zip_path, stream_file)
progressbar.finish if progressbar && !progressbar.finished?
if zip_pool.size > 0
puts "#{zip_pool.size} arquivos gerados:"
zip_pool.each {|name| puts "-- #{name}"}
end
end
if File.directory?(in_zip_or_folder)
package_it(out_zip_path, in_zip_or_folder, chunk_mb)
else
base_name = File.basename(in_zip_or_folder, '.zip')
destionation_dir = File.join(base_dir, base_name)
FileUtils.mkpath(destionation_dir) unless File.directory?(destionation_dir)
unzip_it(in_zip_or_folder, destionation_dir)
package_it(out_zip_path, destionation_dir, chunk_mb)
FileUtils.rm_r(destionation_dir)
end