class RealZip::RealZip

Public Instance Methods

save(file) click to toggle source
# File lib/real_zip.rb, line 57
def save file
  File.delete file if File.exist? file
  Zip::ZipFile.open file, Zip::ZipFile::CREATE do |z|
    # first_dir = nil
    dirs(struct).each do |dir|
      first_dir = dir
      z.dir.mkdir(dir) unless z.file.exist? dir
    end

    hash =  struct 
    hash.each do |dir,file_arr|
      new_files = files({dir => file_arr})
      new_files.each_with_index do |file, index|
        orignal_file_path = file_arr[index]
        if File.directory?(orignal_file_path)
          puts "..... directory found ......"
          source = "#{orignal_file_path}"
          # directory_name = source.split('/').last
          Dir.glob("#{source}**/*").reject {|fn| File.directory?(fn) }.each do |file|
            string  = File.open(file, 'r'){ |f| f.read }
            destination_path = file.sub(source, '' )
            destination_path = "#{dir.to_s}/#{destination_path}"
            puts "adding file    #{destination_path} ..."
            z.file.open(destination_path, "w") { |f| f.write string }
          end
        else
          puts "..... File found ......"  
          string  = File.open(orignal_file_path, 'r'){ |f| f.read }
          file_name = file.split('/').last
          new_file_name = ( files( {dir => [ "#{file_name}" ] } ) ).first
          z.file.open(new_file_name, "w") { |f| f.write string }
        end 
      end
    end 
  end
end
struct(given=file_structure) click to toggle source
# File lib/real_zip.rb, line 94
def struct given=file_structure
  given.is_a?(String) ? YAML.load(given) : given
end