我正在使用DotNetZip
库,我想执行以下步骤:
1将相同的文件添加到ZipFile
对象
2将存档保存到MemoryMappedViewStream
流
3从流中读取存档部件并将其(单独)保存在HDD上
以下代码在归档中添加文件并在MemoryMappedViewStream
对象
var zip = new ZipFile();
int SegmentsCreated = 0;
var archieveVolumSize = 600 * 1024; // 600k segments
zip.MaxOutputSegmentSize = archieveVolumSize;
long maximumSize = 0;
foreach (var file in df.GetFiles().Take(5))
{
zip.AddFile(file.Name);
}
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
MemoryStream ms = new MemoryStream();
zip.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
ms.Flush();
SegmentsCreated = zip.NumberOfSegmentsForMostRecentSave;
zip.Dispose();
var achieveLength = ms.Length;
MemoryMappedFile mmf = MemoryMappedFile.CreateNew("ArhiveZip", ms.Length);
MemoryMappedViewStream stream = mmf.CreateViewStream();
stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
stream.Flush();
stream.Close();
stream.Dispose();
ms.Close();
ms.Dispose();
在将存档保存在Stream对象之前,我将其保存在HDD上,然后我可以从中提取文件。
使用以下代码我尝试在HDD上单独保存卷存档
stream = mmf.CreateViewStream();
var iterations = Math.Ceiling(achieveLength * 1.0 / archieveVolumSize);
long positionStart = 0;
for (int i = 1; i < iterations; i++)
{
byte[] bytes = new byte[archieveVolumSize];
stream.Read(bytes, 0, archieveVolumSize);
File.WriteAllBytes("ZipArhive.z0" + (i), bytes);
positionStart += archieveVolumSize;
}
var archiveBytes = new byte[achieveLength - positionStart];
stream.Read(archiveBytes, 0, archiveBytes.Length);
File.WriteAllBytes("ZipArhive.zip", archiveBytes);
stream.Dispose();
mmf.Dispose();
问题是,在我单独保存卷后,我无法从生成的存档中提取文件。
我在运行时没有异常,但如果我点击Extract here(使用WinRar程序),它什么都不做。
提前谢谢