DotNetZip read archive from stream

Sep 12, 2014 at 7:54 AM
I am using DotNetZip library and I want to do the following steps:

1 Add same files into ZipFile object

2 Save the archive into MemoryMappedViewStream stream

3 Read archive parts from stream and save them (individually) on the HDD

The following code add files in archive and save archive on MemoryMappedViewStream object

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.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
    MemoryStream ms = new MemoryStream();
    ms.Seek(0, SeekOrigin.Begin);
    SegmentsCreated = zip.NumberOfSegmentsForMostRecentSave;
    var achieveLength = ms.Length;
    MemoryMappedFile mmf = MemoryMappedFile.CreateNew("ArhiveZip", ms.Length);
    MemoryMappedViewStream stream = mmf.CreateViewStream();
    stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
Before to save the archive in a Stream object I saved it on HDD and I could extract files from it.

With the following code I tried to save individually volume archive on 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("", archiveBytes);
The problem is that after I saved individually volume I cannot extract files from the resulted archive.

I had no exception on runtime but if I click Extract here (using WinRar program) it doesn't do anything.

You can find this question on stackoverflow at this link
Thank you in advance