如何使用SharpZipLib在C#中创建超过7 GB的zip文件?

Gau*_*pta 3 c# zip

我想在C#中创建一个包含近8 GB数据的zip文件.我使用以下代码:

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();
 }
Run Code Online (Sandbox Code Playgroud)

此代码适用于1 GB以下的小文件,但在数据达到7-8 GB时会引发异常.

Kar*_*ren 5

正如其他人所指出的那样,实际的例外将有助于解决这个问题.但是,如果您想要一种更简单的方法来创建zip文件,我建议您尝试使用http://dotnetzip.codeplex.com/上提供的DotNetZip库.我知道它支持Zip64(即更大的条目,然后4.2gb和更多的65535条目),所以它可能能够解决您的问题.使用它然后自己使用文件流和字节数组也更容易.

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);
}
Run Code Online (Sandbox Code Playgroud)