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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With