Class: AozoraZip::Core
- Inherits:
-
Object
- Object
- AozoraZip::Core
- Defined in:
- lib/aozora_zip/core.rb
Instance Attribute Summary collapse
-
#dirname ⇒ Object
readonly
Returns the value of attribute dirname.
-
#text_file ⇒ Object
readonly
Returns the value of attribute text_file.
Class Method Summary collapse
Instance Method Summary collapse
- #find_text_file(dir) ⇒ Object
-
#initialize(dirname = nil) ⇒ Core
constructor
A new instance of Core.
- #text ⇒ Object
- #unzip(filename, dirname, force: nil, verbose: nil) ⇒ Object
- #zip(filename, force: nil, verbose: nil) ⇒ Object
Constructor Details
#initialize(dirname = nil) ⇒ Core
Returns a new instance of Core.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/aozora_zip/core.rb', line 18 def initialize(dirname = nil) @dirname = dirname if @dirname unless File.directory?(@dirname) raise AozoraZip::Error, "No such directory '#{dirname}'" end @text_file = find_text_file(@dirname) end end |
Instance Attribute Details
#dirname ⇒ Object (readonly)
Returns the value of attribute dirname.
8 9 10 |
# File 'lib/aozora_zip/core.rb', line 8 def dirname @dirname end |
#text_file ⇒ Object (readonly)
Returns the value of attribute text_file.
8 9 10 |
# File 'lib/aozora_zip/core.rb', line 8 def text_file @text_file end |
Class Method Details
.unzip(filename, dirname, **kargs) ⇒ Object
14 15 16 |
# File 'lib/aozora_zip/core.rb', line 14 def self.unzip(filename, dirname, **kargs) self.new.unzip(filename, dirname, **kargs) end |
.zip(filename, dirname, **kargs) ⇒ Object
10 11 12 |
# File 'lib/aozora_zip/core.rb', line 10 def self.zip(filename, dirname, **kargs) self.new(dirname).zip(filename, **kargs) end |
Instance Method Details
#find_text_file(dir) ⇒ Object
30 31 32 33 |
# File 'lib/aozora_zip/core.rb', line 30 def find_text_file(dir) path = Pathname.new(Dir.glob("#{dir}/*.txt")[0]) path.relative_path_from(dir).to_s end |
#text ⇒ Object
35 36 37 |
# File 'lib/aozora_zip/core.rb', line 35 def text AozoraZip::Text.new(text_file, self) end |
#unzip(filename, dirname, force: nil, verbose: nil) ⇒ Object
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 88 89 90 91 |
# File 'lib/aozora_zip/core.rb', line 61 def unzip(filename, dirname, force: nil, verbose: nil) if !File.exist?(filename) raise AozoraZip::Error, "No such file '#{filename}'" end if File.exist?(dirname) && !force raise AozoraZip::Error, "target directory '#{dirname}' already exists" end @dirname = dirname Zip::File.open(filename) do |f| f.each do |entry| next if (entry.directory? || entry.name.end_with?('/')) sep = if entry.name.start_with?('/') '' else '/' end filepath = dirname + sep + entry.name puts "extract #{filepath}" if verbose FileUtils.mkdir_p(File.dirname(filepath)) entry.extract(filepath) FileUtils.touch(filepath, mtime: entry.time) end end @text_file = find_text_file(@dirname) end |
#zip(filename, force: nil, verbose: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aozora_zip/core.rb', line 39 def zip(filename, force: nil, verbose: nil) if File.exist?(filename) && !force raise AozoraZip::Error, "target file '#{filename}' already exists" end Zip::File.open(filename, Zip::File::CREATE) do |f| Dir.chdir(dirname) do Dir.entries('.').each do |item| next if (item == '.' || item == '..') next if !File.file?(item) puts "add #{item}" if verbose fullpath = File.join(Dir.pwd, item) filetime = File.mtime(fullpath) entry = Zip::Entry.new(f, item, nil, nil, nil, nil, nil, nil, Zip::DOSTime.at(filetime)) f.add(entry, fullpath) end end end end |