extracter 1.2.21

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of extracter might be problematic. Click here for more details.

@@ -0,0 +1,277 @@
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 "Extracting `#{sfancy(name_of_the_archive)}` "\
58
+ "to `#{sdir(extract_to)}` next."
59
+ else
60
+ copn; e "Extracting `#{sfancy(i)}` to `#{sdir(extract_to)}` "\
61
+ "next."
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)}."
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
+ # === .tar.xz
77
+ #
78
+ # Note that .txz is just .tar.xz. Support for .txz was added here
79
+ # in January of 2012.
80
+ # ===================================================================== #
81
+ when /\.tar\.xz$/i,
82
+ /\.txz$/i,
83
+ /\.xz$/i
84
+ esystem consider_verbosity_for(COMMAND_TO_EXTRACT_TAR_XZ_FILES)+' '+i+padded_extract_to?
85
+ # ===================================================================== #
86
+ # === .tar
87
+ #
88
+ # This entry point is for simple .tar files.
89
+ # ===================================================================== #
90
+ when /\.tar$/i
91
+ esystem COMMAND_TO_EXTRACT_TAR_FILES+' '+i+padded_extract_to?
92
+ # ===================================================================== #
93
+ # === zip
94
+ # ===================================================================== #
95
+ when /.zip$/i,
96
+ /.xpi$/i,
97
+ /.docx$/i,
98
+ /.odt$/i, # .docx and .odt format types can be unpacked with zip too.
99
+ /.apkg$/
100
+ # =================================================================== #
101
+ # 'ar -jxf' # unzip #{what} <-- this should work as well.
102
+ # =================================================================== #
103
+ i = pad(i) if i.include?(' ') or i.include?(')')
104
+ esystem "unzip #{i}"
105
+ # ===================================================================== #
106
+ # === tgz
107
+ #
108
+ # This entry point will also handle ".tar.Z" files.
109
+ # ===================================================================== #
110
+ when /\.?tgz$/i,
111
+ /\.?tar.Z$/i,
112
+ /\.?taz$/i
113
+ esystem COMMAND_TO_EXTRACT_TGZ_FILES+' '+i+padded_extract_to?
114
+ # ===================================================================== #
115
+ # === 7z
116
+ # ===================================================================== #
117
+ when '.7z' # 7z does not accept the -C commandline.
118
+ # _ << '7za e' # <- Deprecated this variant as of 05.06.2020.
119
+ esystem "7z x #{i}"
120
+ # ===================================================================== #
121
+ # === .tar.bz2
122
+ # ===================================================================== #
123
+ when /\.tar\.bz2$/i,
124
+ /\.tbz$/i
125
+ esystem COMMAND_TO_EXTRACT_TAR_BZ2_FILES+' '+i+padded_extract_to?
126
+ # ===================================================================== #
127
+ # === gz
128
+ # ===================================================================== #
129
+ when /\.?gz$/i,
130
+ /\.?apk$/i
131
+ if i.include? '.tar' # Handle .tar.gz here.
132
+ esystem 'tar -zxvf '+i+padded_extract_to?
133
+ else
134
+ esystem 'gunzip '+i
135
+ end
136
+ # ===================================================================== #
137
+ # === .bz2
138
+ # ===================================================================== #
139
+ when /\.?bz2$/,
140
+ /\.?tbz2$/
141
+ if i.include? '.tar' # Treat it as a .tar file.
142
+ esystem 'tar -vjxf '+i
143
+ else
144
+ esystem 'bunzip2 '+i
145
+ end
146
+ # ===================================================================== #
147
+ # === rar
148
+ # ===================================================================== #
149
+ when '.rar'
150
+ check_whether_rar_is_available
151
+ esystem 'unrar e '+i
152
+ # ===================================================================== #
153
+ # === .zst
154
+ #
155
+ # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
156
+ # ===================================================================== #
157
+ when '.zst','.tar.zst'
158
+ esystem COMMAND_TO_EXTRACT_ZST_ARCHIVES+' '+i
159
+ # ===================================================================== #
160
+ # === deb
161
+ #
162
+ # We could use dpkg-deb too, such as via "dpkg-deb". But using "ar"
163
+ # seems to be the better choice.
164
+ # ===================================================================== #
165
+ when /\.?deb$/i
166
+ esystem COMMAND_TO_EXTRACT_DEB_FILES+' '+i
167
+ # ===================================================================== #
168
+ # === gem
169
+ #
170
+ # The gem commands needs a specific --target=DIRECTORY option.
171
+ # ===================================================================== #
172
+ when /\.?gem$/i
173
+ esystem GEM_UNPACK_COMMAND+' '+i+" --target=#{extract_to}"
174
+ # ===================================================================== #
175
+ # === lzma
176
+ # ===================================================================== #
177
+ when '.lzma'
178
+ esystem "#{COMMAND_TO_EXTRACT_LZMA_FILES} #{i}"
179
+ # ===================================================================== #
180
+ # === bin
181
+ #
182
+ # This entry point allows the user to handle .bin files. In this
183
+ # context, "handling" means to simply run that file as-is.
184
+ # ===================================================================== #
185
+ when /\.?bin$/i
186
+ esystem("./#{i}")
187
+ # ===================================================================== #
188
+ # === iso
189
+ # ===================================================================== #
190
+ when /\.?iso$/i
191
+ try_to_extract_this_iso_file(i)
192
+ return
193
+ # ===================================================================== #
194
+ # === img
195
+ #
196
+ # Note that .img in this context refers to squafhs .img files.
197
+ # ===================================================================== #
198
+ when /\.?img$/i,
199
+ /\.?squashfs$/i
200
+ try_to_extract_this_img_file(i)
201
+ return # Must return early in this case.
202
+ # ===================================================================== #
203
+ # === lz
204
+ #
205
+ # This entry point requires lzip to be installed.
206
+ # ===================================================================== #
207
+ when /\.?lz$/i,
208
+ /\.?tar\.?lz$/i
209
+ esystem("#{COMMAND_TO_EXTRACT_LZ_FILES} #{i}#{padded_extract_to?}")
210
+ # ===================================================================== #
211
+ # === mp4
212
+ #
213
+ # If it is a .mp4 file, we delegate to MultimediaParadise.extract_audio.
214
+ #
215
+ # Usage example:
216
+ #
217
+ # rubyextract foobar.mp4
218
+ #
219
+ # ===================================================================== #
220
+ when /\.?mp4$/i
221
+ begin
222
+ require 'multimedia_paradise/audio/extract_audio/extract_audio.rb'
223
+ rescue LoadError; end
224
+ if Object.const_defined? :MultimediaParadise
225
+ MultimediaParadise.extract_audio(i)
226
+ end
227
+ # ===================================================================== #
228
+ # === ps
229
+ # ===================================================================== #
230
+ when/\.?ps$/i
231
+ esystem 'ps2ascii '+i+padded_extract_to?
232
+ # ===================================================================== #
233
+ # === .jar
234
+ # ===================================================================== #
235
+ when /\.?jar$/i
236
+ esystem COMMAND_TO_EXTRACT_JAR_ARCHIVES+' '+i
237
+ # ===================================================================== #
238
+ # === rpm
239
+ # ===================================================================== #
240
+ when '.rpm'
241
+ esystem "#{COMMAND_TO_EXTRACT_BSDTAR_ARCHIVES} #{i}"
242
+ # ===================================================================== #
243
+ # === sxz
244
+ # ===================================================================== #
245
+ when '.sxz'
246
+ esystem "unsquashfs #{i}"
247
+ # ===================================================================== #
248
+ # === pdf
249
+ #
250
+ # For pdf-files we will tap into the pdf_paradise project, if it
251
+ # is available.
252
+ # ===================================================================== #
253
+ when /\.?pdf$/
254
+ begin
255
+ require 'pdf_paradise/utility_scripts/convert_pdf_to_text.rb'
256
+ _ = PdfParadise::ConvertPdfToText.new(i)
257
+ e _.output_file?
258
+ rescue LoadError => error
259
+ e 'No, not there.'
260
+ pp error # Show the error to the user here.
261
+ end
262
+ return # Must return early in this case.
263
+ else # else tag. We did not find the extension type.
264
+ notify_the_user_that_this_extension_has_not_been_registered_yet(i)
265
+ end
266
+ else
267
+ if File.exist? i
268
+ notify_the_user_that_this_extension_has_not_been_registered_yet(i)
269
+ else
270
+ opnn; e "No file exists at `#{sfile(i)}`."
271
+ end
272
+ end
273
+ end; alias do_extract_to extract_this_archive # === do_extract_to
274
+ alias do_extract_what_to extract_this_archive # === do_extract_what_to
275
+ alias start extract_this_archive # === start
276
+
277
+ end; end