Module: ZipFileUtils

Defined in:
lib/write_xlsx/zip_file_utils.rb

Class Method Summary collapse

Class Method Details

.unzip(src, dest, options = {}) ⇒ Object

src zip filename dest destination directory options :fs_encoding=


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/write_xlsx/zip_file_utils.rb', line 38

def self.unzip(src, dest, options = {})
  FileUtils.makedirs(dest)
  Zip::ZipInputStream.open(src) do |is|
    loop do
      entry = is.get_next_entry()
      break unless entry
      dir = File.dirname(entry.name)
      FileUtils.makedirs(dest+ '/' + dir)
      path = encode_path(dest + '/' + entry.name, options[:fs_encoding])
      if(entry.file?())
        File.open(path, File::CREAT|File::WRONLY|File::BINARY) do |w|
          w.puts(is.read())
        end
      else
        FileUtils.makedirs(path)
      end
    end
  end
end

.zip(src, dest, options = {}) ⇒ Object

src file or directory dest zip filename options :fs_encoding=


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/write_xlsx/zip_file_utils.rb', line 14

def self.zip(src, dest, options = {})
  src = File.expand_path(src)
  dest = File.expand_path(dest)
  File.unlink(dest) if File.exist?(dest)
  Zip::ZipFile.open(dest, Zip::ZipFile::CREATE) {|zf|
    if(File.file?(src))
      zf.add(encode_path(File.basename(src), options[:fs_encoding]), src)
      break
    else
      each_dir_for(src){ |path|
        if File.file?(path)
          zf.add(encode_path(relative(path, src), options[:fs_encoding]), path)
        elsif File.directory?(path)
          zf.mkdir(encode_path(relative(path, src), options[:fs_encoding]))
        end
      }
    end
  }
  FileUtils.chmod(0644, dest)
end