-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7zipMassExtract.rb
199 lines (154 loc) · 4.57 KB
/
7zipMassExtract.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
SEVEN_ZIP = "7z.exe"
ARCHIVE_ARG = "--archive"
PASSWORD_ARG = "--password"
DESTINATION_ARG = "--destination"
EXTENSION_ARG = "--extension"
HELP_ARG = "--help"
class Archive
attr_accessor :rootFile, :password, :destination, :extension
def initialize(rootFile)
@rootFile = rootFile
@password = nil
@destination = "."
@extension = ".rar"
end
end
def printHelp
helpString = []
helpString.push("7zip Mass Extractor for Raws")
helpString.push("Author: Andrew Johnson")
helpString.push("")
helpString.push("Usage: ruby <this script> [options]")
helpString.push("")
helpString.push("Options:")
#Print help
helpString.push("\t#{HELP_ARG}")
helpString.push("\t\tPrint this help screen")
helpString.push("")
#Root archive names
helpString.push("\t#{ARCHIVE_ARG} [<root_file_name 1>;<root_file_name 2>;...]")
helpString.push("\t\tThe name(s) of the root archive file(s) you want to extract.")
helpString.push("\t\tAll archives must be in THIS folder.")
helpString.push("")
#Passwords
helpString.push("\t#{PASSWORD_ARG} [<archive password 1>;<archive password 2>;...]")
helpString.push("\t\tThe password(s) of the archive(s) you want to use to extract.")
helpString.push("\t\tIf only one password is given, it will be used for all files.")
helpString.push("\t\tIf the number of passwords given is less than the number of files,")
helpString.push("\t\tthen the last password will be used on the remaining files.")
helpString.push("")
#Destination
helpString.push("\t#{DESTINATION_ARG} [<folder 1>;<folder 2>;...]")
helpString.push("\t\tThe destination folder you want the extract files to go")
helpString.push("")
#Extension
helpString.push("\t#{EXTENSION_ARG} [<root archive extension 1>;<root archive extension 2>;...]")
helpString.push("\t\tThe archive extensions of the files.")
helpString.push("\t\tDefault = .rar")
helpString.push("")
#Print strings
helpString.each{|e| puts(e)}
end
def extractFile(file, destination, password=nil)
if password == nil then
command = "\"#{SEVEN_ZIP}\" x -o#{destination} \"#{file}\""
system(command)
else
command = "\"#{SEVEN_ZIP}\" x -p#{password} -o#{destination} \"#{file}\""
system(command)
end
end
def isPrimaryArchive(name, rootArchiveName, extension)
result = name.scan(/((#{rootArchiveName})\.(part)\d{1,}\.(part01#{extension}))/)
result2 = name.scan(/((#{rootArchiveName})\.(part)\d{1,}\.(part1#{extension}))/)
if result.size > 0 || result2.size > 0 then
return true
else
return false
end
end
def isChosenArchive(file, archive)
return isPrimaryArchive(file, archive.rootFile, archive.extension)
end
def processFile(file, archives)
for a in archives
if isChosenArchive(file, a) then
extractFile(file, a.destination, a.password)
end
end
end
def processArchives(archives)
cd = Dir.new(".")
cd.each{|file| processFile(file, archives)}
end
def doesUserWantHelp(argVector)
for a in argVector
if a.casecmp(HELP_ARG) == 0 then
return true
end
end
return false
end
def processArgs(argVector)
if argVector.size < 1 || doesUserWantHelp(argVector) then
printHelp
exit
end
allArchivesObjects = []
i = 0
allArchives = []
allPasswords = []
allDestinations = []
allExtensions = []
begin
while i < argVector.size
currentArg = argVector[i]
currentValue = argVector[i+1]
case currentArg
when ARCHIVE_ARG
allArchives = currentValue.split(";")
when PASSWORD_ARG
allPasswords = currentValue.split(";")
when DESTINATION_ARG
allDestinations = currentValue.split(";")
when EXTENSION_ARG
allExtensions = currentValue.split(";")
end
i = i + 2
end
rescue
puts "Arguments given didn't match up"
exit(1)
end
lastPassword = nil
lastDestination = nil
for i in (0...allArchives.size)
currentArchiveFile = allArchives[i]
currentPassword = allPasswords[i]
currentDestination = allDestinations[i]
currentExtension = allExtensions[i]
archiveObject = Archive.new(currentArchiveFile)
if currentPassword != nil then
lastPassword = currentPassword
archiveObject.password = lastPassword
elsif lastPassword != nil then
archiveObject.password = lastPassword
end
if currentDestination != nil then
lastDestination = currentDestination
archiveObject.destination = lastDestination
elsif lastDestination != nil then
archiveObject.destination = lastDestination
end
if currentExtension != nil then
archiveObject.extension = currentExtension
end
allArchivesObjects.push(archiveObject)
end
return allArchivesObjects
end
def main
allArchives = processArgs(ARGV)
processArchives(allArchives)
end
main