<commit_before>﻿using System.Linq;
using DotNet.Basics.IO;
using DotNet.Basics.Sys;
using Microsoft.Extensions.Logging;

namespace DotNet.Basics.Compression
{
    public class SevenZip
    {
        private readonly ILogger _logger;

        public SevenZip(ILogger logger = null)
        {
            _logger = logger;
        }

        public int ExtractToDirectory(string archivePath, string targetDirPath)
        {
            return ExecuteSevenZip("x", $"\"{archivePath}\"", $"\"-o{targetDirPath.ToDir().FullName}\"", "*", "-r");
        }

        public int CreateFromDirectory(string sourceDirPath, string archivePath, bool overwrite = false)
        {
            if (overwrite == false && archivePath.ToPath().Exists())
                throw new System.IO.IOException($"Target archive path already exists: {archivePath}. Set overwrite to true to ignore");

            return ExecuteSevenZip("a", $"\"{archivePath}\"", $"\"{sourceDirPath.ToDir().FullName}\\*\"", "-tzip", "-mx3", "-mmt");
        }

        private int ExecuteSevenZip(string command, params string[] @params)
        {
            using (var temp = new TempDir("7z"))
            {
                var temp7ZExe = temp.Root.ToFile("7za.exe");
                Extract(temp7ZExe.FullName, CompressionResources._7za);
                var temp7ZipDll = temp.Root.ToFile("7za.dll");
                Extract(temp7ZipDll.FullName, CompressionResources._7za1);
                var temp7Zip32Dll = temp.Root.ToFile("7zxa.dll");
                Extract(temp7Zip32Dll.FullName, CompressionResources._7zxa);

                var paramsString = @params.Aggregate(string.Empty, (current, param) => current + $" {param}");
                var script = $"{temp7ZExe.FullName} {command} {paramsString} -y";
                return CommandPrompt.Run(script, _logger);
            }
        }

        private void Extract(string path, byte[] bytes)
        {
            using (var fsDst = new System.IO.FileStream(path, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
                fsDst.Write(bytes, 0, bytes.Length);
            
        }
    }
}<commit_msg>fixed so sevenzip overwrites target file completely instead of updating of overwrite is set to true when extracting<commit_after>﻿using System.Linq;
using DotNet.Basics.IO;
using DotNet.Basics.Sys;
using Microsoft.Extensions.Logging;

namespace DotNet.Basics.Compression
{
    public class SevenZip
    {
        private readonly ILogger _logger;

        public SevenZip(ILogger logger = null)
        {
            _logger = logger;
        }

        public int ExtractToDirectory(string archivePath, string targetDirPath)
        {
            return ExecuteSevenZip("x", $"\"{archivePath}\"", $"\"-o{targetDirPath.ToDir().FullName}\"", "*", "-r");
        }

        public int CreateFromDirectory(string sourceDirPath, string archivePath, bool overwrite = false)
        {
            if (overwrite == false && archivePath.ToPath().Exists())
                throw new System.IO.IOException($"Target archive path already exists: {archivePath}. Set overwrite to true to ignore");

            archivePath.ToPath().DeleteIfExists();
            return ExecuteSevenZip("a", $"\"{archivePath}\"", $"\"{sourceDirPath.ToDir().FullName}\\*\"", "-tzip", "-mx3", "-mmt");
        }

        private int ExecuteSevenZip(string command, params string[] @params)
        {
            using (var temp = new TempDir("7z"))
            {
                var temp7ZExe = temp.Root.ToFile("7za.exe");
                Extract(temp7ZExe.FullName, CompressionResources._7za);
                var temp7ZipDll = temp.Root.ToFile("7za.dll");
                Extract(temp7ZipDll.FullName, CompressionResources._7za1);
                var temp7Zip32Dll = temp.Root.ToFile("7zxa.dll");
                Extract(temp7Zip32Dll.FullName, CompressionResources._7zxa);

                var paramsString = @params.Aggregate(string.Empty, (current, param) => current + $" {param}");
                var script = $"{temp7ZExe.FullName} {command} {paramsString} -y";
                return CommandPrompt.Run(script, _logger);
            }
        }

        private void Extract(string path, byte[] bytes)
        {
            using (var fsDst = new System.IO.FileStream(path, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
                fsDst.Write(bytes, 0, bytes.Length);

        }
    }
}
{'fraction_non_alphanumeric': 0.12610369028752547, 'fraction_numerical': 0.009508716323296355, 'mean_word_length': 2.3243039879608727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4107397', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1259, 'n_words': 272}