<commit_before>package gohandy

import (
	"archive/tar"
	"io"
	"os"
	"path/filepath"
)

func Unpack(r *tar.Reader, toPath string) error {
	for {
		hdr, err := r.Next()
		if err == io.EOF {
			break
		}

		if err != nil {
			return err
		}

		err = nil

		if hdr.Typeflag == tar.TypeDir {
			dirpath := filepath.Join(toPath, hdr.Name)
			err = os.MkdirAll(dirpath, 0777)
		} else if hdr.Typeflag == tar.TypeReg || hdr.Typeflag == tar.TypeRegA {
			err = writeFile(toPath, hdr.Name, r)
		}

		if err != nil {
			return nil
		}

	}

	return nil
}

func writeFile(toPath, filename string, r *tar.Reader) error {
	path := filepath.Join(toPath, filename)
	out, err := os.Create(path)
	if err != nil {
		return err
	}
	defer out.Close()

	if _, err := io.Copy(out, r); err != nil {
		return err
	}

	return nil
}<commit_msg>Refactor to make code more readable.<commit_after>package gohandy

import (
	"archive/tar"
	"io"
	"os"
	"path/filepath"
)

func Unpack(r *tar.Reader, toPath string) error {
	for {
		hdr, err := r.Next()
		switch {
		case err == io.EOF:
			break
		case err != nil:
			return err
		default:
			if err := doOnType(hdr.Typeflag, toPath, hdr.Name, r); err != nil {
				return err
			}
		}

	}

	return nil
}

func doOnType(typeFlag byte, toPath string, name string, r *tar.Reader) error {
	fullpath := filepath.Join(toPath, name)
	switch typeFlag {
	case tar.TypeReg, tar.TypeRegA:
		return writeFile(fullpath, r)
	case tar.TypeDir:
		return os.MkdirAll(fullpath, 0777)
	default:
		return nil
	}
}

func writeFile(path string, r *tar.Reader) error {
	out, err := os.Create(path)
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, r)
	return err
}
{'fraction_non_alphanumeric': 0.13725490196078433, 'fraction_numerical': 0.004753416518122401, 'mean_word_length': 3.048076923076923, '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': '5947925', 'n_tokens_mistral': 729, 'n_tokens_neox': 663, 'n_words': 179}