Module: Pione::Util::Zip

Defined in:
lib/pione/util/zip.rb

Class Method Summary collapse

Class Method Details

.compress(src, archive) ⇒ Object

Create a zip archive of the location.

Parameters:

  • src (DataLocation)

    the source directory location

  • archive (DataLocation)

    the archive location

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pione/util/zip.rb', line 11

def compress(src, archive)
  # src should be a directory
  raise ArgumentError.new(src) unless src.directory?

  # make local path
  _src = src.local
  _archive = Location[Temppath.create]

  # compress
  ::Zip::File.open(_archive.path.to_s, ::Zip::File::CREATE) do |zip|
    _src.rel_entries(rec: true).each do |relpath|
      relpath = relpath.to_s
      location = _src + relpath
      if location.directory?
        zip.mkdir(relpath)
      else
        # expand only if it exists, e.g. broken symbolinks are ignored
        if location.exist?
          entry = zip.add(relpath, location.path.to_s)
          entry.time = ::Zip::DOSTime.at(location.mtime)
          entry.extra.delete("UniversalTime")
        else
          Log::Debug.system("Try to archive the file at %s, but it was ignored because of broken link." % location.address)
        end
      end
    end
  end

  # upload archive
  _archive.move(archive)
end

.uncompress(archive, dest) ⇒ Object

Expand the archive into the destination directory.

Parameters:

  • archive (DataLocation)

    the archive location

  • dest (DataLocation)

    the destination directory location



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pione/util/zip.rb', line 49

def uncompress(archive, dest)
  _archive = archive.local
  ::Zip::File.open(_archive.path.to_s) do |zip|
    zip.each do |entry|
      if entry.directory?
        (dest + entry.name).mkdir
      else
        tmp = Temppath.create
        entry.extract(tmp)
        Location[tmp].move(dest + entry.name)
      end
    end
  end
end