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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/paperclip_archive_processor/zip_util.rb', line 46

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)


63
64
65
66
67
68
69
70
# File 'lib/paperclip_archive_processor/zip_util.rb', line 63

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 to a directory on disk



23
24
25
26
27
# File 'lib/paperclip_archive_processor/zip_util.rb', line 23

def self.unpack(archive, destination)
  Zip::ZipFile.open(archive) do |zip|
    unpack_file(zip, destination)
  end
end

.unpack_file(zip, destination, path = '') ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/paperclip_archive_processor/zip_util.rb', line 29

def self.unpack_file(zip, destination, path='')
  if(zip.file.file?(path))
    FileUtils.mkdir_p(destination)
    File.open(File.join(destination, path), 'w') do |dest|
      zip.file.open(path) do |src|
        dest.write src.read
      end
    end
  else
    dir = File.join(destination, path)
    Dir.mkdir(dir) rescue puts("Directory Exists #{dir}")
    zip.dir.foreach(path) do |dir|
      unpack_file(zip, destination, File.join(path, dir))
    end
  end
end