web-dev-qa-db-fra.com

Combinez plusieurs fichiers en un seul fichier

Code:

static void MultipleFilesToSingleFile(string dirPath, string filePattern, string destFile)
{
    string[] fileAry = Directory.GetFiles(dirPath, filePattern);

    Console.WriteLine("Total File Count : " + fileAry.Length);

    using (TextWriter tw = new StreamWriter(destFile, true))
    {
        foreach (string filePath in fileAry)
        {
            using (TextReader tr = new StreamReader(filePath))
            {
                tw.WriteLine(tr.ReadToEnd());
                tr.Close();
                tr.Dispose();
            }
            Console.WriteLine("File Processed : " + filePath);
        }

        tw.Close();
        tw.Dispose();
    }
}

J'ai besoin d'optimiser cela car son extrêmement lent: prend 3 minutes pour 45 fichiers de taille moyenne 40 - 50 Mb fichier XML.

Remarque: 45 fichiers d'une moyenne de 45 Mo ne sont qu'un exemple, il peut s'agir de n nombre de fichiers de m taille, où n est en milliers & m peut être en moyenne de 128 Ko. Bref, cela peut varier.

Pourriez-vous s'il vous plaît fournir des vues sur l'optimisation?

17
Pratik

Pourquoi ne pas simplement utiliser la méthode Stream.CopyTo() ?

private static void CombineMultipleFilesIntoSingleFile(string inputDirectoryPath, string inputFileNamePattern, string outputFilePath)
{
    string[] inputFilePaths = Directory.GetFiles(inputDirectoryPath, inputFileNamePattern);
    Console.WriteLine("Number of files: {0}.", inputFilePaths.Length);
    using (var outputStream = File.Create(outputFilePath))
    {
        foreach (var inputFilePath in inputFilePaths)
        {
            using (var inputStream = File.OpenRead(inputFilePath))
            {
                // Buffer size can be passed as the second argument.
                inputStream.CopyTo(outputStream);
            }
            Console.WriteLine("The file {0} has been processed.", inputFilePath);
        }
    }
}
38
Sergey Brunov

Plusieurs choses que vous pouvez faire:

  • D'après mon expérience, les tailles de mémoire tampon par défaut peuvent être augmentées avec des avantages notables jusqu'à environ 120 Ko, je soupçonne que la définition d'une grande mémoire tampon sur tous les flux sera le booster de performance le plus simple et le plus visible:

    new System.IO.FileStream("File.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 150000);
    
  • Utilisez la classe Stream, pas la classe StreamReader.

  • Lisez le contenu dans un grand tampon, videz-le dans le flux de sortie à la fois - cela accélérera les opérations sur les petits fichiers.
  • Pas besoin de la fermeture/suppression redondante: vous avez l'instruction using.
2
Sten Petrov

Une option consiste à utiliser la commande copy et à la laisser faire ce qui est bien.

Quelque chose comme:

static void MultipleFilesToSingleFile(string dirPath, string filePattern, string destFile)
{
    var cmd = new ProcessStartInfo("cmd.exe", 
        String.Format("/c copy {0} {1}", filePattern, destFile));
    cmd.WorkingDirectory = dirPath;
    cmd.UseShellExecute = false;
    Process.Start(cmd);
}
2
Eren Ersönmez

J'utiliserais un BlockingCollection pour lire afin que vous puissiez lire et écrire simultanément.
Il est clair que vous devez écrire sur un disque physique séparé pour éviter les conflits matériels. Ce code préservera l'ordre.
La lecture sera plus rapide que l'écriture, donc pas besoin de lecture parallèle.
Encore une fois, puisque la lecture va être plus rapide, limitez la taille de la collection afin que la lecture ne soit pas plus en avance sur l'écriture que nécessaire.
Une tâche simple pour lire le single suivant en parallèle pendant l'écriture du courant a le problème de différentes tailles de fichiers - écrire un petit fichier est plus rapide que lire un gros.

J'utilise ce modèle pour lire et analyser du texte sur T1, puis insérer dans SQL sur T2.

public void WriteFiles()
{
    using (BlockingCollection<string> bc = new BlockingCollection<string>(10))
    {
        // play with 10 if you have several small files then a big file
        // write can get ahead of read if not enough are queued

        TextWriter tw = new StreamWriter(@"c:\temp\alltext.text", true);
        // clearly you want to write to a different phyical disk 
        // ideally write to solid state even if you move the files to regular disk when done
        // Spin up a Task to populate the BlockingCollection
        using (Task t1 = Task.Factory.StartNew(() =>
        {
            string dir = @"c:\temp\";
            string fileText;      
            int minSize = 100000; // play with this
            StringBuilder sb = new StringBuilder(minSize);
            string[] fileAry = Directory.GetFiles(dir, @"*.txt");
            foreach (string fi in fileAry)
            {
                Debug.WriteLine("Add " + fi);
                fileText = File.ReadAllText(fi);
                //bc.Add(fi);  for testing just add filepath
                if (fileText.Length > minSize)
                {
                    if (sb.Length > 0)
                    { 
                       bc.Add(sb.ToString());
                       sb.Clear();
                    }
                    bc.Add(fileText);  // could be really big so don't hit sb
                }
                else
                {
                    sb.Append(fileText);
                    if (sb.Length > minSize)
                    {
                        bc.Add(sb.ToString());
                        sb.Clear();
                    }
                }
            }
            if (sb.Length > 0)
            {
                bc.Add(sb.ToString());
                sb.Clear();
            }
            bc.CompleteAdding();
        }))
        {

            // Spin up a Task to consume the BlockingCollection
            using (Task t2 = Task.Factory.StartNew(() =>
            {
                string text;
                try
                {
                    while (true)
                    {
                        text = bc.Take();
                        Debug.WriteLine("Take " + text);
                        tw.WriteLine(text);                  
                    }
                }
                catch (InvalidOperationException)
                {
                    // An InvalidOperationException means that Take() was called on a completed collection
                    Debug.WriteLine("That's All!");
                    tw.Close();
                    tw.Dispose();
                }
            }))

                Task.WaitAll(t1, t2);
        }
    }
}

Classe BlockingCollection

2
paparazzo
    // Binary File Copy
    public static void mergeFiles(string strFileIn1, string strFileIn2, string strFileOut, out string strError)
    {
        strError = String.Empty;
        try
        {
            using (FileStream streamIn1 = File.OpenRead(strFileIn1))
            using (FileStream streamIn2 = File.OpenRead(strFileIn2))
            using (FileStream writeStream = File.OpenWrite(strFileOut))
            {
                BinaryReader reader = new BinaryReader(streamIn1);
                BinaryWriter writer = new BinaryWriter(writeStream);

                // create a buffer to hold the bytes. Might be bigger.
                byte[] buffer = new Byte[1024];
                int bytesRead;

                // while the read method returns bytes keep writing them to the output stream
                while ((bytesRead =
                        streamIn1.Read(buffer, 0, 1024)) > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                }
                while ((bytesRead =
                        streamIn2.Read(buffer, 0, 1024)) > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                }
            }
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
    }
0
Miguelito

Solution testée publiée par sergey-brunov pour fusionner un fichier de 2 Go. Le système a pris environ 2 Go de RAM pour ce travail. J'ai apporté quelques modifications pour plus d'optimisation et il faut maintenant 350 Mo RAM pour fusionner un fichier de 2 Go.).

private static void CombineMultipleFilesIntoSingleFile(string inputDirectoryPath, string inputFileNamePattern, string outputFilePath)
        {
            string[] inputFilePaths = Directory.GetFiles(inputDirectoryPath, inputFileNamePattern);
            Console.WriteLine("Number of files: {0}.", inputFilePaths.Length);
            foreach (var inputFilePath in inputFilePaths)
            {
                using (var outputStream = File.AppendText(outputFilePath))
                {
                    // Buffer size can be passed as the second argument.
                    outputStream.WriteLine(File.ReadAllText(inputFilePath));
                    Console.WriteLine("The file {0} has been processed.", inputFilePath);

                }
            }
        }
0
kashified