Module: IPFS::IO::Tar

Defined in:
lib/ipfs-api/io.rb

Class Method Summary collapse

Class Method Details

.extract(stream, destination) ⇒ Object



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
# File 'lib/ipfs-api/io.rb', line 58

def extract stream, destination
  Gem::Package::TarReader.new(stream) do |tar|
    path = nil
    tar.each do |entry|
      if entry.full_name == '././@LongLink'
        path = File.join(destination, entry.read.strip)
        next
      end
      path ||= File.join(destination, entry.full_name)
      if entry.directory?
        if File.exist?(path) and not File.directory?(path)
          raise IOError.new("Not a directory: #{path}")
        end
        FileUtils.mkdir_p path, :mode => entry.header.mode, :verbose => false
      elsif entry.file?
        if File.exist?(path) and not File.file?(path)
          raise IOError.new("Not a file: #{path}")
        end
        File.open path, "wb" do |fd|
          while (chunk = entry.read(1024))
            fd.write chunk
          end
        end
        FileUtils.chmod entry.header.mode, path, :verbose => false
      end
      path = nil
    end
  end
  true
end