Class: SevenZipRuby::SevenZipWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/seven_zip_ruby/seven_zip_writer.rb

Overview

SevenZipWriter creates 7zip archive.

Properties

method

Compression method. “LZMA”, “LZMA2”, “PPMd”, “BZIP2”, “DEFLATE” or “COPY”. Default value is “LZMA”.

level

Compression level. 0, 1, 3, 5, 7 or 9. Default value is 5.

solid

Solid compression. true or false. Default value is true.

header_compression

Header compression. true or false. Default value is true.

header_encryption

Header encryption. true or false. Default value is false.

multi_threading

Multi threading. true or false. Default value is true.

Examples

Compress files

# Compress files
File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    szw.add_directory("test_dir")
    szw.add_file("test.txt")
  end
end

# Compress files 2
SevenZipRuby::SevenZipWriter.open_file("filename.7z") do |szw|
  szw.add_directory("test_dir")
  Dir.chdir("parent_dir_of_test.txt") do
    szw.add_file("test.txt")
  end
end

stream = StringIO.new("")
SevenZipRuby::SevenZipWriter.open(stream) do |szw|
  szw.add_file("test.txt")
  szw.add_data(data, "test.bin")
end
# p stream.string

Set various properties

File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file, password: "Password") do |szw|
    szw.method = "LZMA"
    szw.level = 9
    szw.solid = false
    szw.header_compression = false
    szw.header_encryption = true
    szw.multi_threading = false

    szw.add_directory("test_dir")
  end
end

Create a sfx, a self extracting archive for Windows executable binary.

File.open("filename.exe", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file, sfx: true) do |szw|
    szw.add_directory("test_dir")
  end
end

Constant Summary collapse

PATH_ENCODING =

Encoding used for path string in 7zip archive.

Encoding::UTF_8
SFX_FILE_LIST =

Files for self extraction.

{
  default: File.expand_path("../7z.sfx", __FILE__),
  gui:     File.expand_path("../7z.sfx", __FILE__),
  console: File.expand_path("../7zCon.sfx", __FILE__)
}
OPEN_PARAM_LIST =

:nodoc:

[ :password, :sfx ]
COMPRESS_GUARD =

:nodoc:

Mutex.new

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.use_native_input_file_streamObject

Returns the value of attribute use_native_input_file_stream.



77
78
79
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 77

def use_native_input_file_stream
  @use_native_input_file_stream
end

Class Method Details

.add_dirObject

Create 7zip archive which includes the specified directory recursively.

Args

stream

Output stream to write 7zip archive. stream.write is needed.

dir

Directory to be added to the 7zip archive. dir must be a relative path.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

# Create 7zip archive which includes 'dir'.
File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.add_directory(file, 'dir')
end

add_dir is an alias of add_directory.



191
192
193
194
195
196
197
198
199
200
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 191

def add_directory(stream, dir, param = {})
  param = param.clone
  open_param = {}
  OPEN_PARAM_LIST.each do |key|
    open_param[key] = param.delete(key) if param.key?(key)
  end
  self.open(stream, open_param) do |szw|
    szw.add_directory(dir, param)
  end
end

.add_directory(stream, dir, param = {}) ⇒ Object

Create 7zip archive which includes the specified directory recursively.

Args

stream

Output stream to write 7zip archive. stream.write is needed.

dir

Directory to be added to the 7zip archive. dir must be a relative path.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

# Create 7zip archive which includes 'dir'.
File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.add_directory(file, 'dir')
end


181
182
183
184
185
186
187
188
189
190
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 181

def add_directory(stream, dir, param = {})
  param = param.clone
  open_param = {}
  OPEN_PARAM_LIST.each do |key|
    open_param[key] = param.delete(key) if param.key?(key)
  end
  self.open(stream, open_param) do |szw|
    szw.add_directory(dir, param)
  end
end

.add_file(stream, filename, param = {}) ⇒ Object

Create 7zip archive which includes the specified file recursively.

Args

stream

Output stream to write 7zip archive. stream.write is needed.

file

File to be added to the 7zip archive. file must be a relative path.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

# Create 7zip archive which includes 'file.txt'.
File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.add_file(file, 'file.txt')
end


207
208
209
210
211
212
213
214
215
216
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 207

def add_file(stream, filename, param = {})
  param = param.clone
  open_param = {}
  OPEN_PARAM_LIST.each do |key|
    open_param[key] = param.delete(key) if param.key?(key)
  end
  self.open(stream, open_param) do |szw|
    szw.add_file(filename, param)
  end
end

.open(stream, param = {}, &block) ⇒ Object

Open 7zip archive to write.

Args

stream

Output stream to write 7zip archive. stream.write is needed.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

# Open an archive
File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    # Create archive.
    # ...
    # You don't have to call szw.compress. Of cource, you may call it.
    # szw.compress
  end
end

# Open without block.
File.open("filename.7z", "wb") do |file|
  szw = SevenZipRuby::SevenZipWriter.open(file)
  # Create archive.
  szw.compress  # Compress must be called in this case.
  szw.close
end

# Create a self extracting archive. <tt>:gui</tt> and <tt>:console</tt> can be used.
File.open("filename.7z", "wb") do |file|
  szw = SevenZipRuby::SevenZipWriter.open(file, sfx: :gui)
  # Create archive.
  szw.compress  # Compress must be called in this case.
  szw.close
end


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 113

def open(stream, param = {}, &block)  # :yield: szw
  szw = self.new
  szw.open(stream, param)
  if (block)
    begin
      block.call(szw)
      szw.compress
      szw.close
    ensure
      szw.close_file
    end
  else
    szw
  end
end

.open_file(filename, param = {}, &block) ⇒ Object

Open 7zip archive file.

Args

filename

7zip archive filename.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

# Open archive
SevenZipRuby::SevenZipWriter.open_file("filename.7z") do |szw|
  # Create archive.
  # ...
  # You don't have to call szw.compress. Of cource, you may call it.
  # szw.compress
end

# Open without block.
szw = SevenZipRuby::SevenZipWriter.open_file("filename.7z")
# Create archive.
szw.compress  # Compress must be called in this case.
szw.close


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 151

def open_file(filename, param = {}, &block)  # :yield: szw
  szw = self.new
  szw.open_file(filename, param)
  if (block)
    begin
      block.call(szw)
      szw.compress
      szw.close
    ensure
      szw.close_file
    end
  else
    szw
  end
end

Instance Method Details

#add_data(data, filename, opt = {}) ⇒ Object

Add file entry to 7zip archive.

Args

data

Data to be added to the 7zip archive.

filename

File name of the entry to be added to the 7zip archive. filename must be a relative path.

opt

Optional hash parameter. :ctime, :atime and :mtime keys can be specified as timestamp.

Examples

File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    data = "1234567890"

    # Add file entry 'data.bin' in 7zip archive.
    # This entry has the contents "1234567890".
    szw.add_data(data, "data.bin")
  end
end

Raises:

  • (ArgumentError)


361
362
363
364
365
366
367
368
369
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 361

def add_data(data, filename, opt={})
  path = Pathname(filename)
  raise ArgumentError.new("filename should be relative") if (path.absolute?)
  check_option(opt, [ :ctime, :atime, :mtime ])

  name = path.cleanpath.to_s.encode(PATH_ENCODING)
  add_item(UpdateInfo.buffer(name, data, opt))
  return self
end

#add_directory(directory, opt = {}) ⇒ Object Also known as: add_dir

Add directory and files recursively to 7zip archive.

Args

directory

Directory to be added to the 7zip archive. directory must be a relative path if :as option is not specified.

opt

Optional hash parameter. :as key represents directory name used in this archive.

Examples

File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    # Add "dir1" and entries under "dir" recursively.
    szw.add_directory("dir1")

    # Add "C:/Users/test/Desktop/dir" and entries under it recursively.
    szw.add_directory("C:/Users/test/Desktop/dir", as: "test/dir")
  end
end


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 387

def add_directory(directory, opt={})
  directory = Pathname(directory).cleanpath
  check_option(opt, [ :as ])

  if (opt[:as])
    base_dir = Pathname(opt[:as]).cleanpath
    raise ArgumentError.new(":as should contain valid pathname. #{opt[:as]}") if (base_dir.to_s.empty?)
    raise ArgumentError.new(":as should be relative. #{opt[:as]}") if (base_dir.absolute?)

    mkdir(base_dir, { ctime: directory.ctime, atime: directory.atime, mtime: directory.mtime })
  else
    raise ArgumentError.new("directory should be relative #{directory}") if (directory.absolute?)

    mkdir(directory, { ctime: directory.ctime, atime: directory.atime, mtime: directory.mtime })
  end

  Pathname.glob(directory.join("**", "*").to_s, File::FNM_DOTMATCH) do |entry|
    basename = entry.basename.to_s
    next if (basename == "." || basename == "..")

    name = (base_dir + entry.relative_path_from(directory)).cleanpath if (base_dir)

    if (entry.file?)
      add_file(entry, as: name)
    elsif (entry.directory?)
      mkdir(name || entry, { ctime: entry.ctime, atime: entry.atime, mtime: entry.mtime })
    else
      raise "#{entry} is invalid entry"
    end
  end

  return self
end

#add_file(filename, opt = {}) ⇒ Object

Add file entry to 7zip archive.

Args

filename

File to be added to the 7zip archive. file must be a relative path if :as option is not specified.

opt

Optional hash parameter. :as key represents filename used in this archive.

Examples

File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    # Add file entry 'test.txt' in 7zip archive.
    # This entry has the contents of the local file 'test.txt'.
    szw.add_file("test.txt")

    # Add file entry 'desk/test.txt' in 7zip archive.
    # This entry has the contents of the local file 'C:/Users/test/Desktop/test2.txt'.
    szw.add_file("C:/Users/test/Desktop/test2.txt", as: "desk/test.txt")
  end
end


328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 328

def add_file(filename, opt={})
  path = Pathname(filename)
  check_option(opt, [ :as ])

  if (opt[:as])
    filename = Pathname(opt[:as]).cleanpath
    raise ArgumentError.new(":as should contain valid pathname. #{opt[:as]}") if (filename.to_s.empty?)
    raise ArgumentError.new(":as should be relative. #{opt[:as]}") if (filename.absolute?)
  else
    raise ArgumentError.new("filename should be relative. #{filename}") if (path.absolute?)
    filename = path.cleanpath
  end
  add_item(UpdateInfo.file(filename.to_s.encode(PATH_ENCODING), path, self))
  return self
end

#closeObject



270
271
272
273
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 270

def close
  close_impl
  close_file
end

#close_fileObject

:nodoc:



275
276
277
278
279
280
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 275

def close_file  # :nodoc:
  if (@stream)
    @stream.close rescue nil
    @stream = nil
  end
end

#compressObject

Compress and output data to archive file. You don’t have to call this method when you use block-style SevenZipWriter.open.

Examples

# Open archive
File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    # Create archive.
    # ...
    # You don't have to call szw.compress. Of cource, you may call it.
    # szw.compress
  end
end

# Open without block.
File.open("filename.7z", "wb") do |file|
  szw = SevenZipRuby::SevenZipWriter.open(file)
  # Create archive.
  szw.compress  # Compress must be called in this case.
  szw.close
end


303
304
305
306
307
308
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 303

def compress
  synchronize do
    compress_impl(compress_proc)
  end
  return self
end

#mkdir(directory_name, opt = {}) ⇒ Object

Add an entry of empty directory to 7zip archive.

Args

directory_name

Directory name to be added to 7z archive.

opt

Optional hash parameter. :ctime, :atime and :mtime keys can be specified as timestamp.

Examples

File.open("filename.7z", "wb") do |file|
  SevenZipRuby::SevenZipWriter.open(file) do |szw|
    # Add an empty directory "dir1".
    szw.mkdir("dir1")
  end
end

Raises:

  • (ArgumentError)


435
436
437
438
439
440
441
442
443
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 435

def mkdir(directory_name, opt={})
  path = Pathname(directory_name)
  raise ArgumentError.new("directory_name should be relative") if (path.absolute?)
  check_option(opt, [ :ctime, :atime, :mtime ])

  name = path.cleanpath.to_s.encode(PATH_ENCODING)
  add_item(UpdateInfo.dir(name, opt))
  return self
end

#open(stream, param = {}) ⇒ Object

Open 7zip archive to create.

Args

stream

Output stream to write 7zip archive. stream.write is needed.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

File.open("filename.7z", "wb") do |file|
  szw = SevenZipRuby::SevenZipWriter.new
  szw.open(file)
  # ...
  szw.compress
  szw.close
end


237
238
239
240
241
242
243
244
245
246
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 237

def open(stream, param = {})
  param = param.clone
  param[:password] = param[:password].to_s if (param[:password])
  stream.set_encoding(Encoding::ASCII_8BIT)

  add_sfx(stream, param[:sfx]) if param[:sfx]

  open_impl(stream, param)
  return self
end

#open_file(filename, param = {}) ⇒ Object

Open 7zip archive file to create.

close method must be called later.

Args

filename

7zip archive filename.

param

Optional hash parameter.

:password key specifies password of this archive.

:sfx key specifies Self Extracting mode. :gui and :console can be used. true is same as :gui.

Examples

szw = SevenZipRuby::SevenZipWriter.new
szw.open_file("filename.7z")
# ...
szw.compress
szw.close


264
265
266
267
268
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 264

def open_file(filename, param = {})
  @stream = File.open(filename, "wb")
  self.open(@stream, param)
  return self
end