-
Notifications
You must be signed in to change notification settings - Fork 0
/
filepacker.rb
executable file
·76 lines (67 loc) · 1.96 KB
/
filepacker.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
class ZipThemAll
require 'fileutils'
require 'open-uri'
require 'rubygems'
require 'zip/zipfilesystem'
attr_accessor :list_of_file_paths, :zip_file_path, :root_path
def initialize( root_path, zip_file_path)
@zip_file_path = zip_file_path+'.zip'
@root_path = root_path
@swop_file = []
@list_of_file_paths = []
end
def add( list )
Array(list).each do |path|
list_of_file_paths << parse_path(path)
end
end
def parse_path(file_path)
unless file_path.match(/^(http:\/\/|ftp:\/\/)/)
file_path
else
file_regexp = /(?:(?:[-_a-zA-Z\d]*)\.(?:[-_a-zA-Z\d])*)$/
download file_path, file_path.match(file_regexp).to_s
file_path = file_path.match(file_regexp).to_s
@swop_file.push(file_path)
file_path
end
end
def rm
unless @swop_file.empty?
@swop_file.each do |file_name|
directories = Dir.glob(File.join(@root_path+file_name))
FileUtils.rm_rf directories
end
end
end
def download full_url, to_here
writeOut = open(@root_path+to_here, "wb")
writeOut.write(open(full_url).read)
writeOut.close
end
def zip
unless @list_of_file_paths == nil
zip = Zip::ZipFile.open(self.root_path+self.zip_file_path, Zip::ZipFile::CREATE) do | zip_file |
@list_of_file_paths.each { | file_path | archiving file_path, zip_file }
end
rm
end
end
def archiving file_path, zip_file
if File.exists? file_path
file_name = File.basename( file_path )
if zip_file.find_entry( file_name )
zip_file.replace( file_name, file_path )
else
zip_file.add( file_name, file_path)
end
else
puts "Warning: file #{file_path} does not exist"
end
end
end
## DEMO
z = ZipThemAll.new '/home/muk/rails_project/filepacker/', 'pic'
z.add ['http://musicgeneration.com.ua/blog/wp-content/uploads/2011/05/download_large.gif']
z.add ['http://intrigan.com/uploads/images/6/7/3/a/5/90cfdb9fdf.jpg']
z.zip