Class: SevenZipRuby::SevenZipWriter
- Inherits:
-
Object
- Object
- SevenZipRuby::SevenZipWriter
- Defined in:
- lib/seven_zip_ruby/seven_zip_writer.rb
Overview
SevenZipWriter creates 7zip archive.
Properties
methodCompression method. "LZMA", "LZMA2", "PPMd", "BZIP2", "DEFLATE" or "COPY". Default value is "LZMA".
levelCompression level. 0, 1, 3, 5, 7 or 9. Default value is 5.
solidSolid compression. true or false. Default value is true.
header_compressionHeader compression. true or false. Default value is true.
header_encryptionHeader encryption. true or false. Default value is false.
multi_threadingMulti 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
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
Class Method Summary collapse
-
.add_dir ⇒ Object
Create 7zip archive which includes the specified directory recursively.
-
.add_directory(stream, dir, param = {}) ⇒ Object
Create 7zip archive which includes the specified directory recursively.
-
.add_file(stream, filename, param = {}) ⇒ Object
Create 7zip archive which includes the specified file recursively.
-
.open(stream, param = {}, &block) ⇒ Object
Open 7zip archive to write.
Instance Method Summary collapse
-
#add_data(data, filename, opt = {}) ⇒ Object
Add file entry to 7zip archive.
-
#add_directory(directory, opt = {}) ⇒ Object
(also: #add_dir)
Add directory and files recursively to 7zip archive.
-
#add_file(filename, opt = {}) ⇒ Object
Add file entry to 7zip archive.
-
#compress ⇒ Object
Compress and output data to archive file.
-
#mkdir(directory_name, opt = {}) ⇒ Object
Add an entry of empty directory to 7zip archive.
-
#open(stream, param = {}) ⇒ Object
Open 7zip archive to create.
Class Method Details
.add_dir ⇒ Object
Create 7zip archive which includes the specified directory recursively.
Args
streamOutput stream to write 7zip archive. stream.write is needed.
dirDirectory to be added to the 7zip archive. dir must be a relative path.
paramOptional 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.
104 105 106 107 108 109 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 104 def add_directory(stream, dir, param = {}) password = { password: param.delete(:password) } self.open(stream, password) 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
streamOutput stream to write 7zip archive. stream.write is needed.
dirDirectory to be added to the 7zip archive. dir must be a relative path.
paramOptional 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
98 99 100 101 102 103 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 98 def add_directory(stream, dir, param = {}) password = { password: param.delete(:password) } self.open(stream, password) 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
streamOutput stream to write 7zip archive. stream.write is needed.
fileFile to be added to the 7zip archive. file must be a relative path.
paramOptional 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
118 119 120 121 122 123 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 118 def add_file(stream, filename, param = {}) password = { password: param.delete(:password) } self.open(stream, password) do |szw| szw.add_file(filename, param) end end |
.open(stream, param = {}, &block) ⇒ Object
Open 7zip archive to write.
Args
streamOutput stream to write 7zip archive. stream.write is needed.
paramOptional 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
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 74 def open(stream, param = {}, &block) # :yield: szw szw = self.new szw.open(stream, param) if (block) block.call(szw) szw.compress szw.close else szw end end |
Instance Method Details
#add_data(data, filename, opt = {}) ⇒ Object
Add file entry to 7zip archive.
Args
dataData to be added to the 7zip archive.
filenameFile name of the entry to be added to the 7zip archive. filename must be a relative path.
optOptional 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
222 223 224 225 226 227 228 229 230 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 222 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
directoryDirectory to be added to the 7zip archive. directory must be a relative path if :as option is not specified.
optOptional 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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 248 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) do |entry| 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
filenameFile to be added to the 7zip archive. file must be a relative path if :as option is not specified.
optOptional 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
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 189 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 |
#compress ⇒ Object
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
166 167 168 169 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 166 def compress compress_impl(compress_proc) return self end |
#mkdir(directory_name, opt = {}) ⇒ Object
Add an entry of empty directory to 7zip archive.
Args
directory_nameDirectory name to be added to 7z archive.
optOptional 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
293 294 295 296 297 298 299 300 301 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 293 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
streamOutput stream to write 7zip archive. stream.write is needed.
paramOptional hash parameter. :password key represents password of this archive.
Examples
File.open("filename.7z", "wb") do |file| szw = SevenZipRuby::SevenZipWriter.open(file) # ... szw.compress szw.close end
139 140 141 142 143 |
# File 'lib/seven_zip_ruby/seven_zip_writer.rb', line 139 def open(stream, param = {}) stream.set_encoding(Encoding::ASCII_8BIT) open_impl(stream, param) return self end |