<commit_before># encoding: utf-8

require 'zlib'
require 'rake/common/classes'
require 'rake/common/logger'
require 'rake/helpers/raketask'

module Rake
  module Delphi
    class GZip < BasicTask
        def initialize(task, params)
            super(task)
            if params.kind_of?(String)
                gzip_file(params)
            elsif params.kind_of?(Array)
                params.each do |file|
                    gzip_file(file)
                end
            end
        end

    private
        def gzip_file(file)
            @task.out "GZip #{file} -> #{file}.gz"
            sfile = File.open(file + '.gz', "w+b")
            gzfile = Zlib::GzipWriter.wrap(sfile) do |gz|
                File.open(file, "rb") do |f|
                    gz.mtime = File.mtime(file)
                    gz.orig_name = File.basename(file)
                    gz.write(f.read)
                    gz.close
                end
            end
        end
    end

    class ZipTask < BasicTask
        def initialize(task, zipfile, files, options = nil)
            super(task)
            Logger.trace(Logger::VERBOSE, [zipfile, files])
            raise "zipfile name is not defined!" if zipfile.nil? || zipfile.empty?
            @norubyzip = nil
            @options = options || {}
            begin
                require 'zipruby'
            rescue LoadError
                begin
                    require 'zip/zip'
                rescue LoadError
                    @norubyzip = true
                end
            end
            raise "no ZIP library (nor zipruby nor rubyzip) found!" if @norubyzip
            if defined? Zip::Archive
                # zipruby used
                Logger.trace(Logger::VERBOSE, '`zipruby` gem is used')
                Zip::Archive.open(zipfile, Zip::CREATE | Zip::TRUNC) do |z|
                    files.each do |f|
                        zip_addfile(z, f)
                    end
                end
            else
                # work with rubyzip
                Logger.trace(Logger::VERBOSE, '`rubyzip` gem is used')
                File.unlink(zipfile) if File.exists?(zipfile)
                Zip.options[:continue_on_exists_proc] = true
                Zip::ZipFile.open(zipfile, Zip::ZipFile::CREATE) do |z|
                    files.each do |f|
                        zip_addfile(z, f)
                    end
                end
            end
        end

    private
        def zip_addfile(zipfile, file)
            return if ! File.exists?(file)
            filename = File.basename(file)
            @task.out "Zipping #{file}..."
            if defined? Zip::Archive
                if @options[:preserve_paths]
                    if ! zipfile.locate_name(File.dirname(file))
                        zipfile.add_dir(File.dirname(file))
                    end
                    filename = file
                end
                zipfile.add_file(filename, file)
            else
                if @options[:preserve_paths]
                    dir = File.dirname(file)
                    # avoid "./<filename>" entries (instead of "<filename>")
                    filename = File.join(dir, filename) if dir != '.'
                end
                zipfile.add(filename, file)
            end
        end
    end
  end
end
<commit_msg>improve: ziptask: remove support for `zipruby` gem<commit_after># encoding: utf-8

require 'zlib'
require 'rake/common/classes'
require 'rake/common/logger'
require 'rake/helpers/raketask'

module Rake
  module Delphi
    class GZip < BasicTask
        def initialize(task, params)
            super(task)
            if params.kind_of?(String)
                gzip_file(params)
            elsif params.kind_of?(Array)
                params.each do |file|
                    gzip_file(file)
                end
            end
        end

    private
        def gzip_file(file)
            @task.out "GZip #{file} -> #{file}.gz"
            sfile = File.open(file + '.gz', "w+b")
            gzfile = Zlib::GzipWriter.wrap(sfile) do |gz|
                File.open(file, "rb") do |f|
                    gz.mtime = File.mtime(file)
                    gz.orig_name = File.basename(file)
                    gz.write(f.read)
                    gz.close
                end
            end
        end
    end

    class ZipTask < BasicTask
        def initialize(task, zipfile, files, options = nil)
            super(task)
            Logger.trace(Logger::VERBOSE, [zipfile, files])
            raise "zipfile name is not defined!" if zipfile.nil? || zipfile.empty?
            @norubyzip = nil
            @options = options || {}
            begin
                require 'zip/zip'
            rescue LoadError
                @norubyzip = true
            end
            raise "no ZIP library (rubyzip) found!" if @norubyzip
            # work with rubyzip
            Logger.trace(Logger::VERBOSE, '`rubyzip` gem is used')
            File.unlink(zipfile) if File.exists?(zipfile) && ! @options[:add]
            Zip.options[:continue_on_exists_proc] = true
            Zip::ZipFile.open(zipfile, Zip::ZipFile::CREATE) do |z|
                files.each do |f|
                    zip_addfile(z, f)
                end
            end
        end

    private
        def zip_addfile(zipfile, file)
            return if ! File.exists?(file)
            filename = File.basename(file)
            @task.out "Zipping #{file}..."
            if @options[:preserve_paths]
                dir = File.dirname(file)
                # avoid "./<filename>" entries (instead of "<filename>")
                filename = File.join(dir, filename) if dir != '.'

            end
            zipfile.add(filename, file)
        end
    end
  end
end

{'fraction_non_alphanumeric': 0.0966007630939993, 'fraction_numerical': 0.000346860908775581, 'mean_word_length': 1.2903097696584591, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2943229', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1555, 'n_words': 384}