Class: Archive::Writer

Inherits:
BaseArchive show all
Defined in:
lib/ffi_libarchive/writer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseArchive

#errno, #error_string

Constructor Details

#initialize(params = {}) ⇒ Writer

Returns a new instance of Writer.

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :compression (Object)
  • :format (Object)
  • :file_name (String)
  • :memory (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ffi_libarchive/writer.rb', line 46

def initialize(params = {})
  super C.method(:archive_write_new), C.method(:archive_write_free)

  begin
    init_compression params[:compression]
    init_format params[:format]

    if params[:file_name]
      init_for_filename params[:file_name]
    elsif params[:memory]
      init_for_memory params[:memory]
    end
  rescue StandardError
    close
    raise
  end
end

Class Method Details

.open_filename(file_name, compression, format) {|| ... } ⇒ Writer

Parameters:

  • file_name (String)

Yield Parameters:

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ffi_libarchive/writer.rb', line 10

def self.open_filename(file_name, compression, format)
  if block_given?
    writer = open_filename file_name, compression, format

    begin
      yield writer
    ensure
      writer.close if writer.respond_to?(:close)
    end
  else
    new file_name: file_name, compression: compression, format: format
  end
end

.open_memory(string, compression, format) {|| ... } ⇒ Writer

Parameters:

  • string (String)

Yield Parameters:

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ffi_libarchive/writer.rb', line 27

def self.open_memory(string, compression, format)
  if block_given?
    writer = open_memory string, compression, format

    begin
      yield writer
    ensure
      writer.close if writer.respond_to?(:close)
    end
  else
    new memory: string, compression: compression, format: format
  end
end

Instance Method Details

#add_entry {|| ... } ⇒ NilClass

Yield Parameters:

Yield Returns:

  • (String)

Returns:

  • (NilClass)

Raises:

  • (ArgumentError)

    If no block given



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ffi_libarchive/writer.rb', line 84

def add_entry
  raise ArgumentError, 'No block given' unless block_given?

  entry = Entry.new
  begin
    data = yield entry

    if data
      entry.size = data.bytesize

      write_header entry
      write_data data
    else
      write_header entry
    end

    nil
  ensure
    entry.close
  end
end

#closeObject



142
143
144
145
# File 'lib/ffi_libarchive/writer.rb', line 142

def close
  super
  @write_callback = nil
end

#new_entry {|| ... } ⇒ Entry

Yield Parameters:

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ffi_libarchive/writer.rb', line 66

def new_entry
  entry = Entry.new

  if block_given?
    begin
      yield entry
    ensure
      entry.close
    end
  else
    entry
  end
end

#write_data(*args) ⇒ Integer

Parameters:

  • args (Array<String>)

Yield Returns:

  • (String)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ffi_libarchive/writer.rb', line 110

def write_data(*args)
  if block_given?
    raise ArgumentError, 'Not support arguments when block given' unless args.empty?

    len = 0
    loop do
      str = yield len
      n   = str.is_a?(String) ? C.archive_write_data(archive, Utils.get_memory_ptr(str), str.bytesize) : 0

      raise Error, self if n < 0
      break if n.zero?

      len += n
    end

    len
  else
    str = args[0]
    raise ArgumentError, 'Invalid String argument' unless str.is_a?(String)

    n = C.archive_write_data(archive, Utils.get_memory_ptr(str), str.bytesize)
    raise Error, self if n < 0

    n
  end
end

#write_header(entry) ⇒ Object

Parameters:

Raises:



138
139
140
# File 'lib/ffi_libarchive/writer.rb', line 138

def write_header(entry)
  raise Error, self if C.archive_write_header(archive, entry.entry) != C::OK
end