Sha256: 9f67487d3ff0bc77d1e18eec71cddc40c4d9d330ce8ace4d558209e5d447e17d

Contents?: true

Size: 1.48 KB

Versions: 207

Compression:

Stored size: 1.48 KB

Contents

module Sprout
  class ZipUtil #:nodoc:

    # Pack up an archive from a directory on disk
    def self.pack(input, archive, excludes)
      Zip::ZipFile.open(archive, Zip::ZipFile::CREATE) do |zip|
        add_file_to_zip(zip, input, excludes)
      end
    end
    
    # Unpack an archive to a directory on disk
    def self.unpack(archive, destination)
      Zip::ZipFile.open(archive) do |zip|
        unpack_file(zip, destination)
      end
    end
    
    def self.unpack_file(zip, destination, path='')
      if(zip.file.file?(path))
        File.open(File.join(destination, path), 'w') do |dest|
          zip.file.open(path) do |src|
            dest.write src.read
          end
        end
      else
        Dir.mkdir(File.join(destination, path))
        zip.dir.foreach(path) do |dir|
          unpack_file(zip, destination, File.join(path, dir))
        end
      end
    end

    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

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

  end
end

Version data entries

207 entries across 207 versions & 1 rubygems

Version Path
sprout-0.7.246 lib/sprout/zip_util.rb
sprout-0.7.246-x86-linux lib/sprout/zip_util.rb
sprout-0.7.246-x86-darwin-10 lib/sprout/zip_util.rb
sprout-0.7.246-mswin32 lib/sprout/zip_util.rb
sprout-0.7.246-darwin lib/sprout/zip_util.rb
sprout-0.7.245 lib/sprout/zip_util.rb
sprout-0.7.245-x86-linux lib/sprout/zip_util.rb
sprout-0.7.245-x86-darwin-10 lib/sprout/zip_util.rb
sprout-0.7.245-mswin32 lib/sprout/zip_util.rb
sprout-0.7.245-darwin lib/sprout/zip_util.rb
sprout-0.7.244 lib/sprout/zip_util.rb
sprout-0.7.244-x86-linux lib/sprout/zip_util.rb
sprout-0.7.244-x86-darwin-10 lib/sprout/zip_util.rb
sprout-0.7.244-mswin32 lib/sprout/zip_util.rb
sprout-0.7.244-darwin lib/sprout/zip_util.rb
sprout-0.7.241 lib/sprout/zip_util.rb
sprout-0.7.241-x86-linux lib/sprout/zip_util.rb
sprout-0.7.241-x86-darwin-10 lib/sprout/zip_util.rb
sprout-0.7.241-mswin32 lib/sprout/zip_util.rb
sprout-0.7.241-darwin lib/sprout/zip_util.rb