Exemplo n.º 1
0
        static void iterateOverFiles(string dirPath, FilePathFunc f)
        {
            try
            {
                var txtFiles = Directory.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories);

                foreach (string currentFile in txtFiles)
                {
                    f(currentFile);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 2
0
        static bool zipFiles(string tarZipFilePath, string baseDir, List <string> sourcePaths)
        {
            if (String.IsNullOrEmpty(tarZipFilePath))
            {
                Console.WriteLine("zipFiles -> tarZipFilePath is EMPTY!");
                return(false);
            }
            var fi = new FileInfo(tarZipFilePath);
            var di = fi.Directory;

            if (fi.Exists && di.Exists)
            {
                bool replaceTarZipFilePath = aksIfUserWantsToReplaceZipFilePath();
                if (replaceTarZipFilePath)
                {
                    File.Delete(tarZipFilePath);
                }
                else
                {
                    tarZipFilePath = askForAlternativeFileName(tarZipFilePath);
                }
                fi = new FileInfo(tarZipFilePath);
            }
            if (String.IsNullOrEmpty(tarZipFilePath) || fi.Exists)
            {
                Console.WriteLine("zipFiles -> tarZipFilePath is EMPTY!");
                return(false);
            }
            else if (fi.Exists)
            {
                Console.WriteLine("zipFiles -> tarZipFilePath does already EXIST!");
                return(false);
            }

            bool totalSuccess = true;

            using (FileStream newZipFile = new FileStream(tarZipFilePath, FileMode.CreateNew))
            {
                using (ZipArchive archive = new ZipArchive(newZipFile, ZipArchiveMode.Create))
                {
                    FilePathFunc zipDirFunc = (absSourceFilePath) => {
                        string zipTarPath = genZipTargetPath(absSourceFilePath, baseDir);
                        if (!String.IsNullOrEmpty(zipTarPath))
                        {
                            totalSuccess = zipFileToArchive(absSourceFilePath, zipTarPath, archive) && totalSuccess;
                        }
                        return(false);
                    };
                    foreach (var sp in sourcePaths)
                    {
                        // FileInfo(path) tests if path is a file an if path exists
                        // DirectoryInfo(path) tests if path is a dir and if path exists
                        // => if FileInfo(directoryPath).Exists == False!!! (differs e.g. from Qt::FileInfo, and is actually pretty neat!)
                        if (new DirectoryInfo(sp).Exists)
                        {
                            iterateOverFiles(sp, zipDirFunc);
                        }
                        else if (new FileInfo(sp).Exists)
                        {
                            totalSuccess = zipFileToArchive(sp, genZipTargetPath(sp, baseDir), archive) && totalSuccess;
                        }
                    }
                }
            }
            return(totalSuccess);
        }