首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用SharpZipLib在C#中创建超过7 GB的压缩文件?

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

Stack Overflow用户
提问于 2012-06-26 20:54:47
回答 2查看 8.1K关注 0票数 3

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

代码语言:javascript
复制
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();
 }

此代码适用于1 GB以下的小文件,但当数据达到7-8 GB时会抛出异常。

EN

回答 2

Stack Overflow用户

发布于 2012-06-26 21:28:10

正如其他人指出的那样,实际的异常在回答这个问题时会有很大帮助。但是,如果你想要一种更简单的方法来创建压缩文件,我建议你尝试一下http://dotnetzip.codeplex.com/上提供的DotNetZip-library。我知道它支持Zip64 (即大于4.2 ie的条目和超过65535 ie的条目),因此它可能能够解决您的问题。它也比你自己处理文件流和字节数组更容易使用。

代码语言:javascript
复制
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);
}
票数 5
EN

Stack Overflow用户

发布于 2012-06-26 21:08:15

你用的是SharpZipLib,对吧?我不确定这是否是一个有效的解决方案,因为我不知道它抛出了什么异常,但基于this postthis post,它可能是Zip64的问题。或者使用类似如下的代码启用它(来自第二个链接的帖子):

代码语言:javascript
复制
UseZip64 = ICSharpCode.SharpZipLib.Zip.UseZip64.Off

或者,基于第一篇文章,在创建归档时指定归档的大小,这将自动解决Zip64问题。直接来自第一个链接帖子的示例代码:

代码语言:javascript
复制
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath)))
{
 //Compression level 0-9 (9 is highest)
 zipStream.SetLevel(GetCompressionLevel());

 //Add an entry to our zip file
 ZipEntry entry = new ZipEntry(Path.GetFileName(sourceFilePath));
 entry.DateTime = DateTime.Now;
 /* 
 * By specifying a size, SharpZipLib will turn on/off UseZip64 based on the file sizes. If Zip64 is ON
 * some legacy zip utilities (ie. Windows XP) who can't read Zip64 will be unable to unpack the archive.
 * If Zip64 is OFF, zip archives will be unable to support files larger than 4GB.
 */
 entry.Size = new FileInfo(sourceFilePath).Length;
 zipStream.PutNextEntry(entry);

 byte[] buffer = new byte[4096];
 int byteCount = 0;

 using (FileStream inputStream = File.OpenRead(sourceFilePath))
 {
     byteCount = inputStream.Read(buffer, 0, buffer.Length);
     while (byteCount > 0)
     {
         zipStream.Write(buffer, 0, byteCount);
         byteCount = inputStream.Read(buffer, 0, buffer.Length);
     }
 }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11207760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档