extracter 1.2.32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +210 -0
- data/bin/extract +7 -0
- data/bin/extract_it +13 -0
- data/doc/README.gen +165 -0
- data/extracter.gemspec +109 -0
- data/lib/extracter/base/base.rb +178 -0
- data/lib/extracter/class/extract_this_archive.rb +289 -0
- data/lib/extracter/class/extracter.rb +1069 -0
- data/lib/extracter/constants/constants.rb +157 -0
- data/lib/extracter/extract_it/extract_it.rb +241 -0
- data/lib/extracter/requires/require_the_extracter_project.rb +8 -0
- data/lib/extracter/toplevel_methods/is_this_a_valid_archive.rb +41 -0
- data/lib/extracter/toplevel_methods/misc.rb +34 -0
- data/lib/extracter/version/version.rb +21 -0
- data/lib/extracter.rb +5 -0
- data/test/testing_class_extracter.rb +25 -0
- data/test/testing_class_method_extract_what_to.rb +9 -0
- metadata +106 -0
@@ -0,0 +1,289 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# require 'extracter/class/extract_this_archive.rb'
|
6
|
+
# =========================================================================== #
|
7
|
+
require 'extracter/base/base.rb'
|
8
|
+
|
9
|
+
module Extracter
|
10
|
+
|
11
|
+
class Extracter < ::Extracter::Base # === Extracter::Extracter
|
12
|
+
|
13
|
+
# ========================================================================= #
|
14
|
+
# === consider_verbosity_for
|
15
|
+
# ========================================================================= #
|
16
|
+
def consider_verbosity_for(i)
|
17
|
+
i = i.dup
|
18
|
+
unless be_verbose?
|
19
|
+
case i
|
20
|
+
# "tar -xvf" must become "tar -xvf" here.
|
21
|
+
when COMMAND_TO_EXTRACT_TAR_XZ_FILES
|
22
|
+
i.delete!('v')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
return i
|
26
|
+
end
|
27
|
+
|
28
|
+
# ========================================================================= #
|
29
|
+
# === extract_this_archive (extract tag)
|
30
|
+
# ========================================================================= #
|
31
|
+
def extract_this_archive(
|
32
|
+
i, extract_to = extract_to?
|
33
|
+
)
|
34
|
+
i = File.absolute_path(i)
|
35
|
+
# ======================================================================= #
|
36
|
+
# Next handle the situation when we are on Windows:
|
37
|
+
# ======================================================================= #
|
38
|
+
if ::Extracter.are_we_on_windows?
|
39
|
+
# ===================================================================== #
|
40
|
+
# On windows we will overrule this completely, for now.
|
41
|
+
# ===================================================================== #
|
42
|
+
_ = UNPACK_COMMAND_TO_USE_ON_WINDOWS+' '+
|
43
|
+
File.absolute_path(i)+
|
44
|
+
' | '+
|
45
|
+
SECOND_UNPACK_COMMAND_TO_USE_ON_WINDOWS
|
46
|
+
esystem _
|
47
|
+
return
|
48
|
+
end
|
49
|
+
# ======================================================================= #
|
50
|
+
# First determine whether we can extract the archive or whether we can
|
51
|
+
# not:
|
52
|
+
# ======================================================================= #
|
53
|
+
if ::Extracter.is_this_a_valid_archive?(i) or i.end_with?('.pdf')
|
54
|
+
if be_verbose?
|
55
|
+
if show_only_the_short_name_of_the_archive?
|
56
|
+
name_of_the_archive = File.basename(i)
|
57
|
+
copn; e "#{rev}Extracting `#{sfancy(name_of_the_archive)}#{rev}` "\
|
58
|
+
"to `#{sdir(extract_to)}#{rev}`."
|
59
|
+
else
|
60
|
+
copn; e "#{rev}Extracting `#{sfancy(i)}` to "\
|
61
|
+
"`#{sdir(extract_to)}#{rev}`."
|
62
|
+
end
|
63
|
+
end
|
64
|
+
unless File.writable? extract_to
|
65
|
+
copn; ewarn 'You do not have sufficient permissions to'
|
66
|
+
copn; ewarn "modify #{sdir(extract_to)}#{swarn('.')}"
|
67
|
+
return
|
68
|
+
end
|
69
|
+
# ===================================================================== #
|
70
|
+
# Next, pad it if it includes a ' ' character or (). This was
|
71
|
+
# disabled as of July 2022.
|
72
|
+
# ===================================================================== #
|
73
|
+
# i = pad(i) if i.include?(' ') or i.include?(')')
|
74
|
+
case i # case tag; those listed on top are more important.
|
75
|
+
# ===================================================================== #
|
76
|
+
# === .rpm
|
77
|
+
#
|
78
|
+
# This entry point will handle .rpm files.
|
79
|
+
# ===================================================================== #
|
80
|
+
when /\.rpm$/i
|
81
|
+
name_of_the_directory = i.delete_suffix('.rpm')
|
82
|
+
mkdir(name_of_the_directory)
|
83
|
+
set_extract_to(File.absolute_path(name_of_the_directory))
|
84
|
+
cd(name_of_the_directory)
|
85
|
+
esystem 'rpm2cpio ../'+File.basename(i)+' | cpio -idmv'
|
86
|
+
return # Early return.
|
87
|
+
# ===================================================================== #
|
88
|
+
# === .tar.xz
|
89
|
+
#
|
90
|
+
# Note that .txz is just .tar.xz. Support for .txz was added here
|
91
|
+
# in January of 2012.
|
92
|
+
# ===================================================================== #
|
93
|
+
when /\.tar\.xz$/i,
|
94
|
+
/\.txz$/i,
|
95
|
+
/\.xz$/i
|
96
|
+
esystem consider_verbosity_for(COMMAND_TO_EXTRACT_TAR_XZ_FILES)+' '+i+padded_extract_to?
|
97
|
+
# ===================================================================== #
|
98
|
+
# === .tar
|
99
|
+
#
|
100
|
+
# This entry point is for simple .tar files.
|
101
|
+
# ===================================================================== #
|
102
|
+
when /\.tar$/i
|
103
|
+
esystem COMMAND_TO_EXTRACT_TAR_FILES+' '+i+padded_extract_to?
|
104
|
+
# ===================================================================== #
|
105
|
+
# === zip
|
106
|
+
# ===================================================================== #
|
107
|
+
when /.zip$/i,
|
108
|
+
/.xpi$/i,
|
109
|
+
/.docx$/i,
|
110
|
+
/.odt$/i, # .docx and .odt format types can be unpacked with zip too.
|
111
|
+
/.apkg$/
|
112
|
+
# =================================================================== #
|
113
|
+
# 'ar -jxf' # unzip #{what} <-- this should work as well.
|
114
|
+
# =================================================================== #
|
115
|
+
i = pad(i) if i.include?(' ') or i.include?(')')
|
116
|
+
esystem "unzip #{i}"
|
117
|
+
# ===================================================================== #
|
118
|
+
# === tgz
|
119
|
+
#
|
120
|
+
# This entry point will also handle ".tar.Z" files.
|
121
|
+
# ===================================================================== #
|
122
|
+
when /\.?tgz$/i,
|
123
|
+
/\.?tar.Z$/i,
|
124
|
+
/\.?taz$/i
|
125
|
+
esystem COMMAND_TO_EXTRACT_TGZ_FILES+' '+i+padded_extract_to?
|
126
|
+
# ===================================================================== #
|
127
|
+
# === 7z
|
128
|
+
# ===================================================================== #
|
129
|
+
when '.7z' # 7z does not accept the -C commandline.
|
130
|
+
# _ << '7za e' # <- Deprecated this variant as of 05.06.2020.
|
131
|
+
esystem "7z x #{i}"
|
132
|
+
# ===================================================================== #
|
133
|
+
# === .tar.bz2
|
134
|
+
# ===================================================================== #
|
135
|
+
when /\.tar\.bz2$/i,
|
136
|
+
/\.tbz$/i
|
137
|
+
esystem COMMAND_TO_EXTRACT_TAR_BZ2_FILES+' '+i+padded_extract_to?
|
138
|
+
# ===================================================================== #
|
139
|
+
# === gz
|
140
|
+
# ===================================================================== #
|
141
|
+
when /\.?gz$/i,
|
142
|
+
/\.?apk$/i
|
143
|
+
if i.include? '.tar' # Handle .tar.gz here.
|
144
|
+
esystem 'tar -zxvf '+i+padded_extract_to?
|
145
|
+
else
|
146
|
+
esystem 'gunzip '+i
|
147
|
+
end
|
148
|
+
# ===================================================================== #
|
149
|
+
# === .bz2
|
150
|
+
# ===================================================================== #
|
151
|
+
when /\.?bz2$/,
|
152
|
+
/\.?tbz2$/
|
153
|
+
if i.include? '.tar' # Treat it as a .tar file.
|
154
|
+
esystem 'tar -vjxf '+i
|
155
|
+
else
|
156
|
+
esystem 'bunzip2 '+i
|
157
|
+
end
|
158
|
+
# ===================================================================== #
|
159
|
+
# === rar
|
160
|
+
# ===================================================================== #
|
161
|
+
when '.rar'
|
162
|
+
check_whether_rar_is_available
|
163
|
+
esystem 'unrar e '+i
|
164
|
+
# ===================================================================== #
|
165
|
+
# === .zst
|
166
|
+
#
|
167
|
+
# This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
|
168
|
+
# ===================================================================== #
|
169
|
+
when '.zst','.tar.zst'
|
170
|
+
esystem COMMAND_TO_EXTRACT_ZST_ARCHIVES+' '+i
|
171
|
+
# ===================================================================== #
|
172
|
+
# === deb
|
173
|
+
#
|
174
|
+
# We could use dpkg-deb too, such as via "dpkg-deb". But using "ar"
|
175
|
+
# seems to be the better choice.
|
176
|
+
# ===================================================================== #
|
177
|
+
when /\.?deb$/i
|
178
|
+
esystem COMMAND_TO_EXTRACT_DEB_FILES+' '+i
|
179
|
+
# ===================================================================== #
|
180
|
+
# === gem
|
181
|
+
#
|
182
|
+
# The gem commands needs a specific --target=DIRECTORY option.
|
183
|
+
# ===================================================================== #
|
184
|
+
when /\.?gem$/i
|
185
|
+
esystem GEM_UNPACK_COMMAND+' '+i+" --target=#{extract_to}"
|
186
|
+
# ===================================================================== #
|
187
|
+
# === lzma
|
188
|
+
# ===================================================================== #
|
189
|
+
when '.lzma'
|
190
|
+
esystem "#{COMMAND_TO_EXTRACT_LZMA_FILES} #{i}"
|
191
|
+
# ===================================================================== #
|
192
|
+
# === bin
|
193
|
+
#
|
194
|
+
# This entry point allows the user to handle .bin files. In this
|
195
|
+
# context, "handling" means to simply run that file as-is.
|
196
|
+
# ===================================================================== #
|
197
|
+
when /\.?bin$/i
|
198
|
+
esystem("./#{i}")
|
199
|
+
# ===================================================================== #
|
200
|
+
# === iso
|
201
|
+
# ===================================================================== #
|
202
|
+
when /\.?iso$/i
|
203
|
+
try_to_extract_this_iso_file(i)
|
204
|
+
return
|
205
|
+
# ===================================================================== #
|
206
|
+
# === img
|
207
|
+
#
|
208
|
+
# Note that .img in this context refers to squafhs .img files.
|
209
|
+
# ===================================================================== #
|
210
|
+
when /\.?img$/i,
|
211
|
+
/\.?squashfs$/i
|
212
|
+
try_to_extract_this_img_file(i)
|
213
|
+
return # Must return early in this case.
|
214
|
+
# ===================================================================== #
|
215
|
+
# === lz
|
216
|
+
#
|
217
|
+
# This entry point requires lzip to be installed.
|
218
|
+
# ===================================================================== #
|
219
|
+
when /\.?lz$/i,
|
220
|
+
/\.?tar\.?lz$/i
|
221
|
+
esystem("#{COMMAND_TO_EXTRACT_LZ_FILES} #{i}#{padded_extract_to?}")
|
222
|
+
# ===================================================================== #
|
223
|
+
# === mp4
|
224
|
+
#
|
225
|
+
# If it is a .mp4 file, we delegate to MultimediaParadise.extract_audio.
|
226
|
+
#
|
227
|
+
# Usage example:
|
228
|
+
#
|
229
|
+
# rubyextract foobar.mp4
|
230
|
+
#
|
231
|
+
# ===================================================================== #
|
232
|
+
when /\.?mp4$/i
|
233
|
+
begin
|
234
|
+
require 'multimedia_paradise/audio/extract_audio/extract_audio.rb'
|
235
|
+
rescue LoadError; end
|
236
|
+
if Object.const_defined? :MultimediaParadise
|
237
|
+
MultimediaParadise.extract_audio(i)
|
238
|
+
end
|
239
|
+
# ===================================================================== #
|
240
|
+
# === ps
|
241
|
+
# ===================================================================== #
|
242
|
+
when/\.?ps$/i
|
243
|
+
esystem 'ps2ascii '+i+padded_extract_to?
|
244
|
+
# ===================================================================== #
|
245
|
+
# === .jar
|
246
|
+
# ===================================================================== #
|
247
|
+
when /\.?jar$/i
|
248
|
+
esystem COMMAND_TO_EXTRACT_JAR_ARCHIVES+' '+i
|
249
|
+
# ===================================================================== #
|
250
|
+
# === rpm
|
251
|
+
# ===================================================================== #
|
252
|
+
when '.rpm'
|
253
|
+
esystem "#{COMMAND_TO_EXTRACT_BSDTAR_ARCHIVES} #{i}"
|
254
|
+
# ===================================================================== #
|
255
|
+
# === sxz
|
256
|
+
# ===================================================================== #
|
257
|
+
when '.sxz'
|
258
|
+
esystem "unsquashfs #{i}"
|
259
|
+
# ===================================================================== #
|
260
|
+
# === pdf
|
261
|
+
#
|
262
|
+
# For pdf-files we will tap into the pdf_paradise project, if it
|
263
|
+
# is available.
|
264
|
+
# ===================================================================== #
|
265
|
+
when /\.?pdf$/
|
266
|
+
begin
|
267
|
+
require 'pdf_paradise/utility_scripts/convert_pdf_to_text.rb'
|
268
|
+
_ = PdfParadise::ConvertPdfToText.new(i)
|
269
|
+
e _.output_file?
|
270
|
+
rescue LoadError => error
|
271
|
+
e 'No, not there.'
|
272
|
+
pp error # Show the error to the user here.
|
273
|
+
end
|
274
|
+
return # Must return early in this case.
|
275
|
+
else # else tag. We did not find the extension type.
|
276
|
+
notify_the_user_that_this_extension_has_not_been_registered_yet(i)
|
277
|
+
end
|
278
|
+
else
|
279
|
+
if File.exist? i
|
280
|
+
notify_the_user_that_this_extension_has_not_been_registered_yet(i)
|
281
|
+
else
|
282
|
+
opnn; e "No file exists at `#{sfile(i)}`."
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end; alias do_extract_to extract_this_archive # === do_extract_to
|
286
|
+
alias do_extract_what_to extract_this_archive # === do_extract_what_to
|
287
|
+
alias start extract_this_archive # === start
|
288
|
+
|
289
|
+
end; end
|