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

Constant Summary collapse

PATH_ENCODING =

Encoding used for path string in 7zip archive.

Encoding::UTF_8
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.



61
62
63
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 61

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 represents password of this archive.

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.



159
160
161
162
163
164
165
166
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 159

def add_directory(stream, dir, param = {})
  open_param = {
    password: param.delete(:password)
  }
  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 represents password of this archive.

Examples

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


151
152
153
154
155
156
157
158
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 151

def add_directory(stream, dir, param = {})
  open_param = {
    password: param.delete(:password)
  }
  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 represents password of this archive.

Examples

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


173
174
175
176
177
178
179
180
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 173

def add_file(stream, filename, param = {})
  open_param = {
    password: param.delete(:password)
  }
  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 represents password of this archive.

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


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 87

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 represents password of this archive.

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


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 123

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)


314
315
316
317
318
319
320
321
322
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 314

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


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 340

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


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 281

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



223
224
225
226
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 223

def close
  close_impl
  close_file
end

#close_fileObject

:nodoc:



228
229
230
231
232
233
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 228

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


256
257
258
259
260
261
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 256

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)


388
389
390
391
392
393
394
395
396
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 388

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 represents password of this archive.

Examples

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


199
200
201
202
203
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 199

def open(stream, param = {})
  stream.set_encoding(Encoding::ASCII_8BIT)
  open_impl(stream, param)
  return self
end

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

Open 7zip archive file to create.

Args

filename

7zip archive filename.

param

Optional hash parameter. :password key represents password of this archive.

Examples

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


217
218
219
220
221
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 217

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