<commit_before># This is a wrapper around the Rubygems TarReader and Zlib
# http://dracoater.blogspot.com/2013/10/extracting-files-from-targz-with-ruby.html

require 'rubygems/package'
require 'zlib'

module ChefJava
  module Helpers
    class Tar

      def initialize(archive, destination)
        @archive = archive
        @destination = destination
      end

      TAR_LONGLINK = '././@LongLink'

      def extract_tar
        tar_reader(gzip_reader(@archive)) do |tar|
          tar.each do |entry|
            write_file(get_destination(entry), entry)
          end
        end
      end

      private

      # TODO: Clean me.
      # I don't think we need all these rm_rf calls.
      def write_file(dest, entry)
        if entry.directory?
          FileUtils.rm_rf(dest) unless File.directory?(dest)
          FileUtils.mkdir_p(dest, mode: entry.header.mode, verbose: false)
        elsif entry.file?
          FileUtils.rm_rf(dest) unless File.file?(dest)
          # write only - binary mode
          File.open(dest, 'wb') do |f|
            f.print entry.read
          end
          FileUtils.chmod(entry.header.mode, dest, verbose: false)
        elsif entry.header.typeflag == '2' #Symlink!
          File.symlink(entry.header.linkname, dest)
        end
      end

      def longlink?(entry)
       entry.full_name == TAR_LONGLINK
      end

      def gzip_reader(archive)
        @gzip_reader ||= Zlib::GzipReader.open(archive)
      end

      def tar_reader(archive)
        @tar_reader ||= Gem::Package::TarReader.new(archive)
      end

      def longlink(destination, entry)
        File.join(destination, entry.read.strip)
      end

      def link(destination, entry)
        File.join(destination, entry.full_name)
      end

      def get_destination(entry)
        if longlink?(entry)
          longlink(@destination, entry)
        else
          link(@destination, entry)
        end
      end


    end
  end
end<commit_msg>refactor write file. rename local variables for many methods to be consistent.<commit_after># This is a wrapper around the Rubygems TarReader and Zlib
# http://dracoater.blogspot.com/2013/10/extracting-files-from-targz-with-ruby.html

require 'rubygems/package'
require 'zlib'

module ChefJava
  module Helpers
    class Tar

      def initialize(archive, destination)
        @archive = archive
        @destination = destination
      end

      TAR_LONGLINK = '././@LongLink'

      def extract_tar
        tar_reader(gzip_reader(@archive)) do |tar|
          tar.each do |entry|
            write_file(get_destination(entry), entry)
          end
        end
      end

      private

      def write_file(dest, entry)
        if entry.directory?
          tar_mkdir(dest, entry)
        elsif entry.file?
          tar_file(dest, entry)
        elsif entry.header.typeflag == '2' #Symlink!
          tar_symlink(dest, entry)
        end
      end

      def tar_mkdir(tar_dest, tar_entry)
        FileUtils.rm_rf(tar_dest) unless File.directory?(tar_dest)
        FileUtils.mkdir_p(tar_dest, mode: tar_entry.header.mode, verbose: false)
      end

      def tar_file(tar_dest, tar_entry)
        FileUtils.rm_rf(tar_dest) unless File.file?(tar_dest)
        File.open(tar_dest, 'wb') do |f|
          f.print tar_entry.read
        end
        FileUtils.chmod(tar_entry.header.mode, tar_dest, verbose: false)
      end

      def tar_symlink(tar_dest, tar_entry)
        File.symlink(tar_entry.header.linkname, tar_dest)
      end

      def longlink?(tar_entry)
        tar_entry.full_name == TAR_LONGLINK
      end

      def gzip_reader(archive)
        @gzip_reader ||= Zlib::GzipReader.open(archive)
      end

      def tar_reader(archive)
        @tar_reader ||= Gem::Package::TarReader.new(archive)
      end

      def longlink(destination, tar_entry)
        File.join(destination, tar_entry.read.strip)
      end

      def link(destination, tar_entry)
        File.join(destination, tar_entry.full_name)
      end

      def get_destination(tar_entry)
        if longlink?(tar_entry)
          longlink(@destination, tar_entry)
        else
          link(@destination, tar_entry)
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.0905624404194471, 'fraction_numerical': 0.003336510962821735, 'mean_word_length': 2.2235023041474653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '776905', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1281, 'n_words': 292}