<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;

namespace MultiMiner.Utility
{
    public static class Unzipper
    {
        public static void UnzipFileToFolder(string zipFilePath, string destionationFolder)
        {
            Directory.CreateDirectory(destionationFolder);

            if (OSVersionPlatform.GetGenericPlatform() == PlatformID.Unix)
                UnzipFileToFolderUnix(zipFilePath, destionationFolder);
            else
                UnzipFileToFolderWindows(zipFilePath, destionationFolder);
        }

        private static void UnzipFileToFolderUnix(string zipFilePath, string destionationFolder)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.FileName = "tar";
            startInfo.Arguments = String.Format("-xzvf \"{0}\" -C \"{1}\" --strip-components=1", zipFilePath, destionationFolder);

            Process process = Process.Start(startInfo);
            process.WaitForExit();
        }

        private static void UnzipFileToFolderWindows(string zipFilePath, string destinationFolder)
        {
            const bool showProgress = false;
            const bool yesToAll = true;
            
            Shell32.Folder fromFolder = GetShell32NameSpace(zipFilePath);
            Directory.CreateDirectory(destinationFolder);
            Shell32.Folder toFolder = GetShell32NameSpace(destinationFolder);
            Shell32.FolderItems sourceFolderItems = fromFolder.Items();

            Shell32.FolderItems itemsToExtract;

            //if the zip file contains a single directory, extract that directory's contents
            if ((sourceFolderItems.Count == 1) &&
                (sourceFolderItems.Item(0).GetFolder is Shell32.Folder))
            {
                Shell32.FolderItem rootItem = sourceFolderItems.Item(0);
                itemsToExtract = ((Shell32.Folder)rootItem.GetFolder).Items();
            }
            else 
            {
                itemsToExtract = sourceFolderItems;
            }

            int options = 0;
            if (!showProgress)
                options += 4;
            if (yesToAll)<commit_msg>Update Unix unzip to use same logic as on Windows If there's only a single top level folder, extract its contents, otherwise extract from the top level<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;

namespace MultiMiner.Utility
{
    public static class Unzipper
    {
        public static void UnzipFileToFolder(string zipFilePath, string destionationFolder)
        {
            Directory.CreateDirectory(destionationFolder);

            if (OSVersionPlatform.GetGenericPlatform() == PlatformID.Unix)
                UnzipFileToFolderUnix(zipFilePath, destionationFolder);
            else
                UnzipFileToFolderWindows(zipFilePath, destionationFolder);
        }

        private static void UnzipFileToFolderUnix(string zipFilePath, string destinationFolder)
        {

            string temporaryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            Directory.CreateDirectory(temporaryPath);
            
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.FileName = "tar";
            startInfo.Arguments = String.Format("-xzvf \"{0}\" -C \"{1}\"", zipFilePath, temporaryPath);
            
            Process process = Process.Start(startInfo);
            process.WaitForExit();

            DirectoryInfo directoryInfo = new DirectoryInfo(temporaryPath);
            FileInfo[] files = directoryInfo.GetFiles();
            DirectoryInfo[] directories = directoryInfo.GetDirectories();

            //if the zip file contains a single directory, extract that directory's contents
            if ((files.Length == 0) && (directories.Length == 1))
            {
                CopyDirectoryContents(directories[0].FullName, destinationFolder);
            }
            else
            {
                CopyDirectoryContents(temporaryPath, destinationFolder);
            }
            
            Directory.Delete(temporaryPath, true);
        }

        private static void CopyDirectoryContents(string sourceDir, string targetDir)
        {
            Directory.CreateDirectory(targetDir);

            foreach (var file in Directory.GetFiles(sourceDir))
                File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);

            foreach (var directory in Directory.GetDirectories(sourceDir))
                CopyDirectoryContents(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
        }

        private static void UnzipFileToFolderWindows(string zipFilePath, string destinationFolder)
        {
            const bool showProgress = false;
            const bool yesToAll = true;
            
            Shell32.Folder fromFolder = GetShell32NameSpace(zipFilePath);
            Directory.CreateDirectory(destinationFolder);
            Shell32.Folder toFolder = GetShell32NameSpace(destinationFolder);
            Shell32.FolderItems sourceFolderItems = fromFolder.Items();

            Shell32.FolderItems itemsToExtract;

            //if the zip file contains a single directory, extract that directory's contents
            if ((sourceFolderItems.Count == 1) &&
                (sourceFolderItems.Item(0).GetFolder is Shell32.Folder))
            {
                Shell32.FolderItem rootItem = sourceFolderItems.Item(0);
                itemsToExtract = ((Shell32.Folder)rootItem.GetFolder).Items();
            }
            else 
            {
                itemsToExtract = sourceFolderItems;
            }

            int options = 0;
            if (!showProgress)
                options += 4;
            if (yesToAll)
{'fraction_non_alphanumeric': 0.07909698996655519, 'fraction_numerical': 0.009030100334448161, 'mean_word_length': 2.1796916533758637, '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': '7634123', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1389, 'n_words': 326}