Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compress Large Files C#

Tags:

c#

compression

I am using this method to compress files and it works great until I get to a file that is 2.4 GB then it gives me an overflow error:

 void CompressThis (string inFile, string compressedFileName)
 {

        FileStream sourceFile = File.OpenRead(inFile);
        FileStream destinationFile = File.Create(compressedFileName);


        byte[] buffer = new byte[sourceFile.Length];
        sourceFile.Read(buffer, 0, buffer.Length);

        using (GZipStream output = new GZipStream(destinationFile,
            CompressionMode.Compress))
        {
            output.Write(buffer, 0, buffer.Length);
        }

        // Close the files.
        sourceFile.Close();
        destinationFile.Close();
  }

What can I do to compress huge files?

like image 912
Missy Avatar asked Oct 24 '25 17:10

Missy


1 Answers

You should not to write the whole file to into the memory. Use Stream.CopyTo instead. This method reads the bytes from the current stream and writes them to another stream using a specified buffer size (81920 bytes by default).

Also you don't need to close Stream objects if use using keyword.

void CompressThis (string inFile, string compressedFileName)
{
    using (FileStream sourceFile = File.OpenRead(inFile))
    using (FileStream destinationFile = File.Create(compressedFileName))
    using (GZipStream output = new GZipStream(destinationFile, CompressionMode.Compress))
    {
        sourceFile.CopyTo(output);
    }
}

You can find a more complete example on Microsoft Docs (formerly MSDN).

like image 109
Vadim Martynov Avatar answered Oct 26 '25 08:10

Vadim Martynov