我想在C#中创建一个包含近8 GB数据的压缩文件。我使用了以下代码:
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时会抛出异常。
发布于 2012-06-26 21:28:10
正如其他人指出的那样,实际的异常在回答这个问题时会有很大帮助。但是,如果你想要一种更简单的方法来创建压缩文件,我建议你尝试一下http://dotnetzip.codeplex.com/上提供的DotNetZip-library。我知道它支持Zip64 (即大于4.2 ie的条目和超过65535 ie的条目),因此它可能能够解决您的问题。它也比你自己处理文件流和字节数组更容易使用。
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);
}发布于 2012-06-26 21:08:15
你用的是SharpZipLib,对吧?我不确定这是否是一个有效的解决方案,因为我不知道它抛出了什么异常,但基于this post和this post,它可能是Zip64的问题。或者使用类似如下的代码启用它(来自第二个链接的帖子):
UseZip64 = ICSharpCode.SharpZipLib.Zip.UseZip64.Off或者,基于第一篇文章,在创建归档时指定归档的大小,这将自动解决Zip64问题。直接来自第一个链接帖子的示例代码:
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);
}
}
}https://stackoverflow.com/questions/11207760
复制相似问题