Module: Akabei::ArchiveUtils

Defined in:
lib/akabei/archive_utils.rb

Constant Summary collapse

BUFSIZ =
8192

Class Method Summary collapse

Class Method Details

.archive_all(src, dest, comp, format) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/akabei/archive_utils.rb', line 39

def archive_all(src, dest, comp, format)
  Archive::Writer.open_filename(dest.to_s, comp, format) do |archive|
    list_tree_paths(src).sort.each do |path|
      archive.new_entry do |entry|
        entry.pathname = path.relative_path_from(src).to_s
        is_dir = path.directory?
        if is_dir
          entry.pathname += '/'
        end
        entry.copy_stat(path.to_s)
        archive.write_header(entry)
        unless is_dir
          path.open do |f|
            archive.write_data do
              f.read(BUFSIZ)
            end
          end
        end
      end
    end
  end
end

.each_entry(path, &block) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/akabei/archive_utils.rb', line 7

def each_entry(path, &block)
  Archive.read_open_filename(path.to_s) do |archive|
    while entry = archive.next_header
      block.call(entry, archive)
    end
  end
end

.extract_all(src, dest) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/akabei/archive_utils.rb', line 25

def extract_all(src, dest)
  each_entry(src) do |entry, archive|
    path = dest.join(entry.pathname)
    path.parent.mkpath
    if entry.regular?
      path.open('wb') do |f|
        archive.read_data do |buf|
          f.write(buf)
        end
      end
    end
  end
end

.list_paths(path) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/akabei/archive_utils.rb', line 15

def list_paths(path)
  paths = []
  each_entry(path) do |entry|
    paths << entry.pathname
  end
  paths
end

.list_tree_paths(dir) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/akabei/archive_utils.rb', line 62

def list_tree_paths(dir)
  paths = []
  q = dir.each_child.to_a
  until q.empty?
    path = q.shift
    paths << path
    if path.directory?
      q += path.each_child.to_a
    end
  end
  paths
end