Class: PaperclipArchiveProcessor::ZipUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip_archive_processor/zip_util.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.add_file_to_zip(zip, path, excludes) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paperclip_archive_processor/zip_util.rb', line 35

def self.add_file_to_zip(zip, path, excludes)
  if(File.directory?(path))
    zip.dir.mkdir(path)
    Dir.open(path).each do |child|
      if(!excluded?(child, excludes))
        add_file_to_zip(zip, File.join(path, child), excludes)
      end
    end
  else
    File.open(path) do |src|
      zip.file.open(path, 'w') do |dest|
        dest.write src.read
      end
    end
  end
end

.excluded?(str, excludes) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/paperclip_archive_processor/zip_util.rb', line 52

def self.excluded?(str, excludes)
  excludes.each do |exc|
    if(str == exc)
      return true
    end
  end
  return false
end

.pack(input, archive, excludes) ⇒ Object

Pack up an archive from a directory on disk



9
10
11
12
13
# File 'lib/paperclip_archive_processor/zip_util.rb', line 9

def self.pack(input, archive, excludes)
  Zip::ZipFile.open(archive, Zip::ZipFile::CREATE) do |zip|
    add_file_to_zip(zip, input, excludes)
  end
end

.pack_single_file(input, archive) ⇒ Object



15
16
17
18
19
20
# File 'lib/paperclip_archive_processor/zip_util.rb', line 15

def self.pack_single_file(input, archive)
  Zip::ZipFile.open(archive, Zip::ZipFile::CREATE) do |zip|
    file_name = File.basename(input)
    zip.add(file_name, input)
  end
end

.unpack(archive, destination) ⇒ Object

Unpack an archive with all its subdirectories to a directory on disk



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/paperclip_archive_processor/zip_util.rb', line 23

def self.unpack(archive, destination)
  Zip::ZipFile.open(archive) do |zip|
    zip.each do |file|
      file_path = File.join destination, file.name
      FileUtils.mkdir_p File.dirname(file_path)
      # remove existing files
      FileUtils.rm file_path if File.exist?(file_path) && File.file?(file_path)
      zip.extract file, file_path
    end
  end
end