Module: MultiZip::Backend::Cli::InfoZip::InstanceMethods
- Defined in:
- lib/multi_zip/backend/cli/info_zip.rb
Instance Method Summary collapse
- #list_members(prefix = nil, options = {}) ⇒ Object
- #member_info(member_path, options = {}) ⇒ Object
- #raise_info_zip_error!(message, options = {}) ⇒ Object
- #read_member(member_path, options = {}) ⇒ Object
- #remove_member(member_path, options = {}) ⇒ Object
- #write_member(member_path, member_content, options = {}) ⇒ Object
Instance Method Details
#list_members(prefix = nil, options = {}) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/multi_zip/backend/cli/info_zip.rb', line 159 def list_members(prefix = nil, ={}) archive_exists! response = MultiZip::Backend::Cli::InfoZip.spawn([ UNZIP_PROGRAM, UNZIP_PROGRAM_LIST_MEMBERS_SWITCHES, @filename ].flatten) return [] if response.first.to_s =~ /^#{UNZIP_PROGRAM_EMPTY_ZIPFILE_MESSAGE}/ if response.first member_list = response.first.split("\n").sort member_list = member_list.select{|m| m =~ /^#{prefix}/} if prefix return member_list else # error, response.last should contain error message raise_info_zip_error!(response.last) end end |
#member_info(member_path, options = {}) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/multi_zip/backend/cli/info_zip.rb', line 219 def member_info(member_path, ={}) archive_exists! response = MultiZip::Backend::Cli::InfoZip.spawn([ UNZIP_PROGRAM, UNZIP_PROGRAM_MEMBER_INFO_SWITCHES, @filename, member_path ].flatten).compact if response.join =~ /#{UNZIP_PROGRAM_INVALID_FILE_MESSAGE}/ raise_info_zip_error!(response.join) end # example line: # -rwx------ 2.1 unx 558 bX defN 15-Jun-22 17:53 ROBO3DR1PLUSV1/BlinkM.cpp line = response.detect{|r| r.strip.match(/#{member_path}$/)} raise MultiZip::MemberNotFoundError.new(member_path) if line.nil? || line =~ /#{UNZIP_PROGRAM_MEMBER_NOT_FOUND_MESSAGE}/i fields = line.split(/\s+/) path = fields.last unless path == member_path raise MultiZip::Backend::Cli::InfoZip::ResponseError, "Unexpected file name format or position: #{line.inspect}" end size = fields[3] unless size =~ /\d+/ raise MultiZip::Backend::Cli::InfoZip::ResponseError, "Unexpected file size format or position: #{line.inspect}" end type = case fields[0].slice(0,1) when 'd' :directory when 'l' :symlink when '-' :file else raise MultiZip::Backend::Cli::InfoZip::ResponseError, "Unexpected file type field format or position: #{line.inspect}" end { :path => fields.last, :size => size.to_i, :type => type, :original => line } end |
#raise_info_zip_error!(message, options = {}) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/multi_zip/backend/cli/info_zip.rb', line 269 def raise_info_zip_error!(, ={}) infozip_error = MultiZip::Backend::Cli::InfoZip::ResponseError.new() case when /#{UNZIP_PROGRAM_INVALID_FILE_MESSAGE}/ raise MultiZip::InvalidArchiveError.new(@filename, infozip_error) when /cannot find or open/ raise MultiZip::ArchiveNotFoundError.new(@filename, infozip_error) when /filename not matched/ raise MultiZip::MemberNotFoundError.new([:member_path]) else raise MultiZip::UnknownError.new(@filename, infozip_error) end end |
#read_member(member_path, options = {}) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/multi_zip/backend/cli/info_zip.rb', line 176 def read_member(member_path, ={}) archive_exists! member_not_found!(member_path) if member_path =~ /\/$/ response = MultiZip::Backend::Cli::InfoZip.spawn([ UNZIP_PROGRAM, UNZIP_PROGRAM_READ_MEMBER_SWITCH, @filename, member_path ].flatten) return response.first if response.first raise_info_zip_error!(response.last, :member_path => member_path) end |
#remove_member(member_path, options = {}) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/multi_zip/backend/cli/info_zip.rb', line 206 def remove_member(member_path, ={}) archive_exists! raise MultiZip::MemberNotFoundError.new(member_path) unless member_exists?(member_path) response = MultiZip::Backend::Cli::InfoZip.spawn([ ZIP_PROGRAM, ZIP_PROGRAM_REMOVE_MEMBER_SWITCH, @filename, member_path ].flatten) return response.first if response.first raise_info_zip_error!(response.last, :member_path => member_path) end |
#write_member(member_path, member_content, options = {}) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/multi_zip/backend/cli/info_zip.rb', line 188 def write_member(member_path, member_content, ={}) Dir.mktmpdir do |tempdir| member_file = File.new("#{tempdir}/#{member_path}", 'wb') member_file.print member_content member_file.close cwd = Dir.pwd Dir.chdir(tempdir) response = MultiZip::Backend::Cli::InfoZip.spawn([ ZIP_PROGRAM, @filename, member_path ]) Dir.chdir(cwd) end true end |