Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a zip file over 7 GB in C# using SharpZipLib? [closed]

Tags:

c#

zip

I want to create a zip file in C# that will include almost 8 GB of data. I am using the following code:

using (var zipStream = new ZipOutputStream(System.IO.File.Create(outPath)))
{
    zipStream.SetLevel(9); // 0-9, 9 being the highest level of compression

    var buffer = new byte[1024*1024];

    foreach (var file in filenames)
    {
        var entry = new ZipEntry(Path.GetFileName(file)) { DateTime = DateTime.Now };

        zipStream.PutNextEntry(entry);

        var bufferSize = BufferedSize;
        using (var fs = new BufferedStream(System.IO.File.OpenRead(file), bufferSize))
        {
            int sourceBytes;
            do
            {
                 sourceBytes = fs.Read(buffer, 0, buffer.Length);
                 zipStream.Write(buffer, 0, sourceBytes);
             } while (sourceBytes > 0);
         }
     }

     zipStream.Finish();
     zipStream.Close();
 }

This code works for small files under 1 GB, but it throws an exception when the data reaches 7-8 GB.

like image 521
Gaurav Gupta Avatar asked Dec 05 '25 20:12

Gaurav Gupta


1 Answers

As others have pointed out, the actual exception would have helped a lot in answering this. However, if you want an easier way to create zip-files I would suggest you try the DotNetZip-library available at http://dotnetzip.codeplex.com/. I know it has support for Zip64 (ie larger entries then 4.2gb and more then 65535 entries) so it might be able to solve your problem. It is also a lot easer to use then working with filestreams and bytearrays yourself.

using (ZipFile zip = new ZipFile()) {
    zip.CompressionLevel = CompressionLevel.BestCompression;
    zip.UseZip64WhenSaving = Zip64Option.Always;
    zip.BufferSize = 65536*8; // Set the buffersize to 512k for better efficiency with large files

    foreach (var file in filenames) {
        zip.AddFile(file);
    }
    zip.Save(outpath);
}
like image 105
Karl-Johan Sjögren Avatar answered Dec 08 '25 10:12

Karl-Johan Sjögren