Script doesnt work as wished

Robert H. wrote in post #1185769:

Since it requires zlib, there may be some reasons why it may
not work on your system. It is not possible to say what the
reasons are without knowing more specifices.

How can we verify, that Ruby was built with gzip support (and how likely
is it, that we encounter an implementation where this is not supported)?

BTW, if understand the Ruby docs correctly, even if Zlib::GzipWriter
would not be supported, Zlib::Deflate.deflate should work. Am I right
with this?

Ronald

Ruby Amateur wrote in post #1185739:

Hi there
I made a script that should compress and delete files but it doesn’t
work as wished.
What should I change so that it can compress and delete files.

Regards

First, define File.compress(fn) and File.uncompress(fn) :

gem install rubyzip

===
require ‘zip’
require ‘fileutils’

class File
def self.uncompress(zfile)
raise(“file doesn’t exist : ‘#{zfile}’”)unless File.exist?(zfile)
Zip::File.open(zfile) do |zipfile|
zipfile.each do |entry|
IO.copy_stream(entry.get_input_stream,entry.name)
end
end
end
def self.compress(file)
raise(“file doesn’t exist : ‘#{file}’”) unless File.exist?(file)
return file if file =~ /.zip$/
zfile="#{file}.zip"
FileUtils.rm(zfile) if File.exist?(zfile)
Zip::OutputStream.open(zfile) do |zos|
zos.put_next_entry(file)
IO.copy_stream(file,zos)
end
if File.exist?(zfile)
FileUtils.rm(file)
zfile
else
raise(“fail to create zip file…”)
end
end
end

Then use it, replacing File.rm by File.compress :

==
last_month= Date.today << 1
Dir.glob("*.log").select {|f| File.mtime(f).to_date<last_month }.each
{|f|
File.compress(f)
}

Tested in windows 10, ruby 2.2.4p230