It compiles, works well, solves my problem of zipping old or big log files and removes them from a hard drive. However, following this answer, I would like to know what was wrong with the code.Dir.foreach(FileUtils.pwd()) do |f|    if f.end_with?('log')        File.open(f) do |file|            if File.size(f) > MAX_FILE_SIZE                puts f                puts file.ctime                puts file.mtime                # zipping the file                orig = f                Zlib::GzipWriter.open('arch_log.gz') do |gz|                  gz.mtime = File.mtime(orig)                  gz.orig_name = orig                  gz.write IO.binread(orig)                  puts File has been archived                end                #deleting the file                begin                  File.delete(f)                  puts File has been deleted                rescue Exception => e                  puts File #{f} can not be deleted                  puts        Error #{e.message}                                  puts ======= Please remove file manually ==========                end            end        end    endend
GZip archiver for log files
ruby;file system;file;logging;compression
My proposal, please see also the comments in the code.require 'zlib'MAX_FILE_SIZE = 1024 #One KB#Use a glob to get all log-filesDir[#{Dir.pwd}/*.log].each do |f|  #skip file, if file is small  next unless File.size(f) > MAX_FILE_SIZE  #Make a one line info  puts #{f}: #{File.size(f)/1024}KB, created #{File.ctime(f)} modified #{File.mtime(f)}   # zipping the file - each log in a file  Zlib::GzipWriter.open(arch_#{File.basename(f)}.gz) do |gz|    gz.mtime = File.mtime(f)    gz.orig_name = File.basename(f) #filename without path    #File.read should be fine for log files. Or is there a reason for IO.binread    gz.write File.read(f)     puts File #{f} has been archived  end  #deleting the file  begin    #~ File.delete(f)    puts File #{f} has been deleted  rescue Exception => e    puts File #{f} can not be deleted    puts        Error #{e.message}                    puts ======= Please remove file manually ==========  endendRemark:This solution creates one gz-file per log file. Your old solution created one arch_log.gz. If there were two log-files, the 2nd would overwrite the 1st.