Class: RUtilAnts::Archive::FilesWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rUtilAnts/Archive.rb

Overview

Class used to write files and directories

Constant Summary collapse

FILE_BUFFER_SIZE =

Size of the buffer used to write files contents

8388608

Instance Method Summary collapse

Constructor Details

#initialize(iPassword, iSalt, oFile) ⇒ FilesWriter

Constructor

Parameters
  • iPassword (String): Password encrypting data

  • iSalt (String): Salt encoding data

  • oFile (IO): The IO that will receive encrypted data



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rUtilAnts/Archive.rb', line 203

def initialize(iPassword, iSalt, oFile)
  @ObjectWriter = ObjectWriter.new(iPassword, iSalt, oFile)
  # The list of empty directories
  # list< String >
  @EmptyDirs = []
  # The list of files, along with their size
  # list< [ String, Integer ] >
  @LstFiles = []
  # The total size of bytes
  # Integer
  @TotalSize = 0
end

Instance Method Details

#add_dir(iDirName) ⇒ Object

Add a directory with all its recursive content

Parameters
  • iDirName (String): Name of the directory



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rUtilAnts/Archive.rb', line 230

def add_dir(iDirName)
  lEmpty = true
  lRealDir = iDirName
  if (iDirName == '')
    lRealDir = '.'
  end
  Dir.foreach(lRealDir) do |iFileName|
    if ((iFileName != '.') and
        (iFileName != '..'))
      lEmpty = false
      lCompleteFileName = "#{iDirName}/#{iFileName}"
      if (iDirName == '')
        lCompleteFileName = iFileName
      end
      if (File.directory?(lCompleteFileName))
        add_dir(lCompleteFileName)
      else
        add_file(lCompleteFileName)
      end
    end
  end
  if (lEmpty)
    @EmptyDirs << iDirName
  end
end

#add_file(iFileName) ⇒ Object

Add a single file to write

Parameters
  • iFileName (String): File to add



220
221
222
223
224
# File 'lib/rUtilAnts/Archive.rb', line 220

def add_file(iFileName)
  lFileSize = File.size(iFileName)
  @LstFiles << [ iFileName, lFileSize ]
  @TotalSize += lFileSize
end

#add_files(iFilesFilter) ⇒ Object

Add a files filter

Parameters
  • iFilesFilter (String): The files filter, to be used with glob



260
261
262
263
264
265
266
# File 'lib/rUtilAnts/Archive.rb', line 260

def add_files(iFilesFilter)
  Dir.glob(iFilesFilter).each do |iFileName|
    if (!File.directory?(iFileName))
      add_file(iFileName)
    end
  end
end

#dumpObject

Dump files to write on screen



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rUtilAnts/Archive.rb', line 269

def dump
  log_msg "#{@EmptyDirs.size} empty directories:"
  @EmptyDirs.each_with_index do |iDirName, iIdxDir|
    log_msg "* [#{iIdxDir}]: #{iDirName}"
  end
  log_msg "#{@LstFiles.size} files (#{@TotalSize} bytes):"
  @LstFiles.each_with_index do |iFileInfo, iIdxFile|
    iFileName, iFileSize = iFileInfo
    log_msg "* [#{iIdxFile}]: #{iFileName} (#{iFileSize} bytes)"
  end
end

#writeObject

Write everything in the file



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rUtilAnts/Archive.rb', line 282

def write
  # First, the list of empty directories and the header
  @ObjectWriter << [ @EmptyDirs, @LstFiles.size, @TotalSize ]
  # Then each file
  lEncodedSize = 0
  @LstFiles.each do |iFileInfo|
    iFileName, iFileSize = iFileInfo
    lNbrChunks = iFileSize/FILE_BUFFER_SIZE
    if (iFileSize % FILE_BUFFER_SIZE != 0)
      lNbrChunks += 1
    end
    log_debug "Writing file #{iFileName} (#{iFileSize} bytes, #{lNbrChunks} chunks) ..."
    @ObjectWriter << [ iFileName, lNbrChunks ]
    File.open(iFileName, 'rb') do |iFile|
      lNbrChunks.times do |iIdxChunk|
        lFileDataChunk = iFile.read(FILE_BUFFER_SIZE)
        @ObjectWriter << lFileDataChunk
        lEncodedSize += lFileDataChunk.size
        $stdout.write("#{(lEncodedSize*100)/@TotalSize} %\015")
      end
    end
  end
  @ObjectWriter.flush
end