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



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rUtilAnts/Archive.rb', line 208

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rUtilAnts/Archive.rb', line 235

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



225
226
227
228
229
# File 'lib/rUtilAnts/Archive.rb', line 225

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



265
266
267
268
269
270
271
# File 'lib/rUtilAnts/Archive.rb', line 265

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



274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rUtilAnts/Archive.rb', line 274

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



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rUtilAnts/Archive.rb', line 287

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