Class: MdsFileUtils::ZipSplitter

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/mds_file_utils/zip_splitter.rb

Constant Summary collapse

FILES_PER_ZIP =
1500
EXTRACTED_DIR =
'extracted'
COMPONENTS_DIR =
'components'

Instance Method Summary collapse

Constructor Details

#initialize(path, original_filename = path) ⇒ ZipSplitter

Returns a new instance of ZipSplitter.



10
11
12
13
# File 'lib/mds_file_utils/zip_splitter.rb', line 10

def initialize(path, original_filename=path)
  @path = path
  @original_filename = original_filename
end

Instance Method Details

#split(tmp_storage_path = '/tmp') ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mds_file_utils/zip_splitter.rb', line 15

def split(tmp_storage_path='/tmp')
  splitter_path = File.join(tmp_storage_path, self.class.name)

  zip_base_name = base_file_name

  # Create a directory in which to place the contents of the zip file
  extracted_path = File.join(splitter_path, EXTRACTED_DIR, zip_base_name)
  mkdir_p extracted_path

  # Extract the contents of the zip file to the extracted path
  %x{unzip -d #{extracted_path} '#{@path}'}
  raise "Error extracting ZIP file '#{@path}' to '#{extracted_path}'" if $? != 0

  entries = directory_entries(extracted_path)

  puts "Number of entries: #{entries}"

  if entries.size > FILES_PER_ZIP
    result_zip_file = create_composite_zip_from_entries(zip_base_name, entries, splitter_path, extracted_path)
  else
    # The file is fine as is; just copy the file to the storage
    # location and return the path
    result_zip_file = File.join(splitter_path, File.basename(@path))
    cp @path, result_zip_file
  end

  result_zip_file
end