Class: Suvii::Extract::Targz

Inherits:
Suvii::Extract show all
Defined in:
lib/suvii/extract/targz.rb

Overview

Since:

  • 0.1.0

Constant Summary collapse

CHUNK_SIZE =

Since:

  • 0.1.0

65_536
DIR_UP =

Since:

  • 0.1.0

".."
PAX_GLOBAL_HEADER =

Since:

  • 0.1.0

"pax_global_header"

Constants inherited from Suvii::Extract

TARGZ_RE, UnknownFormatError, ZIP_RE

Instance Attribute Summary

Attributes inherited from Suvii::Extract

#source, #strip_components

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Suvii::Extract

class_for, #initialize

Constructor Details

This class inherits a constructor from Suvii::Extract

Class Method Details

.copy_stream(tar_io, destination) ⇒ Object

can’t use IO.copy_stream because TarReader::Entry#read has different arity when IO#read

Since:

  • 0.1.0



13
14
15
16
17
18
19
# File 'lib/suvii/extract/targz.rb', line 13

def self.copy_stream(tar_io, destination)
  File.open(destination, "wb") do |destination_file|
    until tar_io.eof?
      destination_file.write(tar_io.read(CHUNK_SIZE))
    end
  end
end

Instance Method Details

#extract_to(destination) ⇒ String

Performs archive extraction.

Parameters:

  • destination (String)

    directory where the archive should be extracted.

Returns:

  • (String)

    destination.

Since:

  • 0.1.0



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
# File 'lib/suvii/extract/targz.rb', line 22

def extract_to(destination)
  Zlib::GzipReader.open(source) do |gz|
    Gem::Package::TarReader.new(gz) do |tar|
      tar.each do |tarfile|
        next if tarfile.full_name == PAX_GLOBAL_HEADER

        if tarfile.full_name.include?(DIR_UP)
          raise SecurityError, "can't write outside of destination directory, entry filename is #{tarfile.full_name.inspect}"
        end

        path = path_with_stripped_components(tarfile.full_name)
        destination_file = File.join(destination, path)

        if tarfile.directory?
          FileUtils.mkdir_p(destination_file)
        else
          destination_directory = File.dirname(destination_file)
          FileUtils.mkdir_p(destination_directory)
          Targz.copy_stream(tarfile, destination_file)
        end
      end
    end
  end
  destination
end