How to create a ZIP Archive on a Non seekable but Writable Stream

0 votes
asked Jun 7, 2012 by Selventhran (250 points)
edited Jun 7, 2012

I tried to create an archive on a FTP Stream which is Writable but not seekable. It throws an error stating that, 'The Stream is not seekable'. Is it necessary that the FTP stream should seekable for creating the zip archive?

1 Answer

0 votes
answered Jun 7, 2012 by Lukas Matyska (54,470 points)
edited Jun 7, 2012
 
Best answer

The ZipArchive can work with seekable streams only. But you can use e.g. MemoryStream to prepare a ZIP archive in memory, then copy it to a non-seekable stream as follows:

MemoryStream ms = new MemoryStream();

// create ZipArchive in memory
// use LeaveOpen argument to keep MemoryStream usable after closing the ZipArchive
using (ZipArchive zip = new ZipArchive(ms, ArchiveStreamCloseMode.LeaveOpen))
{
    // work with the ZipArchive ...
    zip.Add("C:/temp/data");
}

// copy MemoryStream to any writable stream (not necessary seekable)
using (FileStream fs = new FileStream("C:/temp/data.zip", FileMode.Create))
{
    ms.WriteTo(fs);
}
...