Module: Compressible

Included in:
Normalizer, Smepable
Defined in:
lib/escoffier/compressible.rb

Instance Method Summary collapse

Instance Method Details

#unzip(entry, options = Hash.new) ⇒ Object

Unzips a file on the filesystem using the bzip2 algorithm. Pass in a path to a file, and optional options.

options: :output_path => ‘path’ Where the file should go if not the same directory as the zipped file. :remove_original => [true] Specify if the original zipped file should be removed after unzipping. :recursive => [true]



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/escoffier/compressible.rb', line 10

def unzip(entry, options = Hash.new)
  
  if options[:recursive]
    Find.find(entry) do |entry|
      next unless File.exists?(entry)
      next if File.directory?(entry)
      next unless entry =~ /.bz2$/
      
      unzip_entry(entry, options)
    end
  else
    unzip_entry(entry, options)
  end
  
end

#zip(file, options = Hash.new) ⇒ Object

Zips a file on the filesystem using the bzip2 algorithm. Pass in a path to a file, and optional options. options: :output_path => Where the file should go if not the same directory as the unzipped file. :remove_original => [true] Specify if the original zipped file should be removed after unzipping.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/escoffier/compressible.rb', line 32

def zip(file, options = Hash.new)
  if options[:output_path]
    output_path = options[:output_path]
  else 
    output_path = File.dirname(File.expand_path(file))
  end
  
  options[:remove_original] = true if options[:remove_original].nil?
  
  decompressed_filename = File.expand_path(file)
  compressed_filename = File.join(output_path, File.basename(file) + '.bz2')
  puts "Compressing #{decompressed_filename} to #{compressed_filename}"
  Bzip2::Writer.open(compressed_filename, 'w') do |compressed_file|
    File.open(decompressed_filename, 'r') do |decompressed_file|
      compressed_file << decompressed_file.read
    end
  end

  if File.exist?(compressed_filename)
    File.delete(decompressed_filename) if options[:remove_original]
  end
  
  return compressed_filename
end